一个方法preventing JIT内联 [英] Preventing JIT inlining on a method

查看:160
本文介绍了一个方法preventing JIT内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有那种独特的情况。我一直对开源库用于发送电子邮件。在这个图书馆,我需要一个可靠的方式来获得调用方法。我通过分析它里面的的StackFrame 对象与堆栈跟踪做到了这一点。此工程没有问题,在调试模式下的项目,其中的优化都被关闭。

I've got sort of a unique situation. I've been working on an open source library for sending email. In this library, I need a reliable way to get the calling method. I've done this with a StackTrace by analyzing the StackFrame objects inside it. This works without issue in a debug-mode project where optimizations are turned off.

当我切换到释放哪里做优化的开启方式出现问题。堆栈跟踪看起来是这样的:

The problem occurs when I switch to release mode where optimizations are turned on. The stack trace looks like this:

> FindActionName at offset 66 in file:line:column <filename unknown>:0:0
> Email at offset 296 in file:line:column <filename unknown>:0:0
> CallingEmailFromRealControllerShouldFindMailersActionName at offset 184
     in file:line:column <filename unknown>:0:0
> _InvokeMethodFast at offset 0 in file:line:column <filename unknown>:0:0
> InvokeMethodFast at offset 152 in file:line:column <filename unknown>:0:0
...

这是从一个失败的单元测试服用。在此跟踪的第3行,我看到一个名为 testemail子这是在其他地方定义的方法,但我相信抖动是内联的。我读过,你可以通过一个虚拟的方法prevent内联,但这不起作用。有谁知道preventing方法内联一个可靠的方法,使你的方法将堆栈跟踪显示?

This is taken from a failing unit test. In line 3 of this trace, I should see a method called TestEmail which is defined elsewhere, but I believe the JITter is inlining it. I've read that you can prevent inlining by making a method virtual, but this doesn't work. Does anyone know of a reliable method for preventing method inlining so your method will show up in a stack trace?

推荐答案

您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimplattribute.aspx\"><$c$c>MethodImplAttribute并指定<一个href=\"http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions.aspx\"><$c$c>MethodImplOptions.NoInlining.

[MethodImpl(MethodImplOptions.NoInlining)]
void YourMethod()
{
    // do something
}

请注意,这仍然不能保证您可以在实际调用的方法获得尽可能源$ C ​​$ C看到。你的方法不会被内联,但你的方法的调用者会内联到自己的调用者,等等等等。

Note that this still doesn't guarantee that you can get at the actual calling method as seen in the source code. Your method won't be inlined, but your method's caller could be inlined into its own caller, etc etc.

这篇关于一个方法preventing JIT内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆