c# can you force a function to NOT be inlined?

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

Can I force a function to NEVER be inlined?

I want the call stack returned by:
System.Diagnostics.StackTrace callStack = new
System.Diagnostics.StackTrace();
to be such that:
System.Diagnostics.StackFrame frame = callStack.GetFrame(0);
is the function itself, NOT the function's caller (due to a release
build making the function inlined into its caller)!

thanks!

Zytan
 
You have to apply the MethodImpl attribute, with
MethodImplOptions.NoInlining specified.
 
Hello Zytan,

Just to add to Nicholas, another inlining prerequisites are:
- methods are small (32bytes of IL),
- no virtual,
- simple control flow
- no try/catch
- no stuct for arguments or local.

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

Z> Can I force a function to NEVER be inlined?
Z>
Z> I want the call stack returned by:
Z> System.Diagnostics.StackTrace callStack = new
Z> System.Diagnostics.StackTrace();
Z> to be such that:
Z> System.Diagnostics.StackFrame frame = callStack.GetFrame(0);
Z> is the function itself, NOT the function's caller (due to a release
Z> build making the function inlined into its caller)!
Z> thanks!
Z>
Z> Zytan
Z>
 
Just to add to Nicholas, another inlining prerequisites are:
- methods are small (32bytes of IL),
- no virtual,
- simple control flow
- no try/catch
- no stuct for arguments or local.

Ok, so I could use any one of these and it wouldn't be inlined. But,
it's likely best to just specify the attribute. Thanks, Michael

Zytan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top