开发者

Checking Method Inlining in C#

Is there a开发者_运维问答ny way I can check (not force) if a given method or property getter is being inlined in a release build?


No - because it doesn't happen at build time; it happens at JIT time. The C# compiler won't perform any inlining; it's up to the CLR that the code ends up running on.

You can discover this using cordbg with all JIT optimizations turned on, but you'll need to dig through the assembly code. 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.)


They're never inlined by the C# compiler. Only const fields are.

You can take a look at the C# compiler optimizations here.

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. If you don't see the CALL statement then it got inlined. 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". Which ensures the jitter behaves as it does without the debugger, methods won't be inlined when the optimizer is turned off.


Add code within the method body to examine the stack trace using StackFrame. In my experience, inlined methods are excluded from this stack trace.


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. This is probaly the easiest way, because inlining happens at jit-compilation time.

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)


It's possible without looking at the assembly code:

http://blogs.msdn.com/b/clrcodegeneration/archive/2009/05/11/jit-etw-tracing-in-net-framework-4.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜