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

5

6

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.

Shwa answered 29/10, 2008 at 23:31 Comment(0)
P
4

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.

Petulant answered 14/12, 2012 at 10:40 Comment(0)
H
10

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

Hachman answered 29/10, 2008 at 23:39 Comment(0)
D
4

I recently benchmarked this:

http://www.gfilter.net/junk/BubblesortBenchmark.jpg (Higher is worse)

As you can tell, the CLR and the JVM are both much better at method inlining than you ever will be.

Defendant answered 29/10, 2008 at 23:43 Comment(0)
P
4

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.

Petulant answered 14/12, 2012 at 10:40 Comment(0)
S
3
if(performance < ACCEPTABLE){
   if(profiler.showsAffectOfMethodCallOverhead() && seriouslyWeDoubleChecked()){
      if(runtimeContext.isReallyStableAndNotProneToChange() && weNeedToGetThisThingBatOutOfHellFast()){
          return thisIsOneOfTheFewTimesWhenANativeExternalFunctionMayBeWorthIt();
      }
   }
 }
 return dontWorryAboutIt();

If you're executing that rare path, break out your C or ASM.

Sanitarian answered 30/10, 2008 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.