简体   繁体   English

检查C#中的方法内联

[英]Checking Method Inlining in C#

如果在发布版本中内联给定的方法或属性getter,有什么方法可以检查(而不是强制)?

No - because it doesn't happen at build time; 不 - 因为它不会在构建时发生; it happens at JIT time. 它发生在JIT时间。 The C# compiler won't perform any inlining; C#编译器不会执行任何内联; it's up to the CLR that the code ends up running on. 由代码最终运行的CLR决定。

You can discover this using cordbg with all JIT optimizations turned on, but you'll need to dig through the assembly code. 可以使用cordbg发现这一点,并启用所有JIT优化,但您需要深入了解汇编代码。 I don't know of any way of discovering this within code. 我不知道在代码中发现这种情况的方法。 (It's possible you could do so with the debugger API, although that may well disable some inlining to start with.) (您可以使用调试器API执行此操作,但这可能会禁用一些内联开始。)

They're never inlined by the C# compiler. 它们永远不会被C#编译器内联。 Only const fields are. 只有const字段。

You can take a look at the C# compiler optimizations here . 您可以在此处查看C#编译器优化。

You can make sure that a method or property accessor is never inlined with this attribute applied to it: 可以确保方法或属性访问器永远不会应用此属性进行内联:

[MethodImpl(MethodImplOptions.NoInlining)]

You'd have to look at the machine code. 您必须查看机器代码。 Set a breakpoint on method call and when it hits, right-click and choose Go To Assembly. 在方法调用上设置断点,当它命中时,右键单击并选择Go To Assembly。 If you don't see the CALL statement then it got inlined. 如果您没有看到CALL语句,那么它就会被内联。 You'll have to be up to speed a little on reading machine code to be really sure though, you might see a call that was in the inlined method. 你必须要在读取机器代码时加快速度才能确定,你可能会看到内联方法中的调用。

To make this accurate, you'll have to use Tools + Options, Debugging, General, untick "Suppress JIT optimization on module load". 为了使其准确,您必须使用工具+选项,调试,常规,取消勾选“在模块加载时抑制JIT优化”。 Which ensures the jitter behaves as it does without the debugger, methods won't be inlined when the optimizer is turned off. 这可以确保抖动的行为与没有调试器的情况一样,优化器关闭时不会内联方法。

I know this post is rather old, but you just could print out the stack where you call the function and in the function you call itself. 我知道这篇文章相当陈旧,但你可以打印出你调用函数的堆栈和你自己调用的函数。

If the printed out stack matches you can be sure, that the function was inlined. 如果打印出的堆栈匹配,您可以确定该函数是内联的。

To print out the stack you can use System.Environment.StackTrace or VS Varibles $caller and $callstack ( https://msdn.microsoft.com/en-us/library/5557y8b4.aspx#BKMK_Print_to_the_Output_window_with_tracepoints ) 要打印出堆栈,您可以使用System.Environment.StackTrace或VS Varibles $caller$callstackhttps://msdn.microsoft.com/en-us/library/5557y8b4.aspx#BKMK_Print_to_the_Output_window_with_tracepoints

Add code within the method body to examine the stack trace using StackFrame . 在方法体中添加代码以使用StackFrame检查堆栈跟踪。 In my experience, inlined methods are excluded from this stack trace. 根据我的经验,内联方法被排除在此堆栈跟踪之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM