Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to enforce function inlining in c#?

As far as I know there's no way to hint the c# compiler to inline a particular function and I guess it's like that by design.

I also think that not letting the programmer to specify what to inline and what not is generally a good idea, as it would imply that you think you're smarter than the JIT compiler (my respects to those who actually are), but, what if I wanted to specify that a critical portion of code needs to be extremely fast at any cost, no matter how to achieve it on the target machine? As of yet you can't do such a thing and I wonder if both the c# language and the JIT will ever support this feature.

In my case, I know what the target machine is, and I know that function inlining will help improve the performance. This leaves me thinking that the only way to enforce function inlining is getting to know under what circumstances the JIT will do it but I don't think that's a good idea either,

Any light on the subject would be much appreciated.

Thanks.

like image 680
Trap Avatar asked Oct 29 '08 23:10

Trap


People also ask

What is function inlining in C?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

Can we use inline function in C?

For example, in the C language, the Macro function is used to reduce the program's execution time. Since C++ is an extension of C, it also provides a function called the inline function, which can speed up a program by reducing the execution time.

How do you force a function inline in C++?

The __forceinline keyword forces the compiler to compile a C or C++ function inline. The semantics of __forceinline are exactly the same as those of the C++ inline keyword. The compiler attempts to inline a function qualified as __forceinline , regardless of its characteristics.

How do you check if a function has been inlined?

The most reliable way to see if a function is being inlined or not is to look at the output from the compiler. Most compilers have a switch to output assembler code for your inspection.


2 Answers

Short answer: no

Long answer: http://blogs.msdn.com/ericgu/archive/2004/01/29/64644.aspx

Criteria for inlining: http://blogs.msdn.com/davidnotario/archive/2004/11/01/250398.aspx and http://blogs.msdn.com/ericgu/archive/2004/01/29/64717.aspx

Note that in the last two links about criteria for inlining, the one about structs not being inlines is out-of-date; updated information can be found at: http://blogs.msdn.com/vancem/archive/2008/05/12/what-s-coming-in-net-runtime-performance-in-version-v3-5-sp1.aspx

like image 164
Greg Beech Avatar answered Sep 28 '22 20:09

Greg Beech


The situation has changed a little with the advent of .Net 4.5.

You can now decorate a method with the attribute [MethodImplAttribute(MethodImplOptions.AggressiveInlining)] which will cause it to be inlined by the JIT if at all possible.

See this blog for more details.

like image 27
Samuel Jack Avatar answered Sep 28 '22 20:09

Samuel Jack