Checking Method Inlining in C#
Asked Answered
M

6

9

Is there any way I can check (not force) if a given method or property getter is being inlined in a release build?

Marcosmarcotte answered 21/3, 2011 at 19:54 Comment(0)
G
7

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.)

Gaius answered 21/3, 2011 at 19:59 Comment(0)
L
5

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)]
Lethargic answered 21/3, 2011 at 19:59 Comment(0)
S
3

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.

Sensibility answered 21/3, 2011 at 20:5 Comment(0)
S
2

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

Signification answered 21/3, 2011 at 20:7 Comment(0)
A
2

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)

Alible answered 3/4, 2018 at 14:45 Comment(0)
M
0

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

Mundane answered 23/5, 2012 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.