views:

407

answers:

4

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.

+6  A: 

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

Greg Beech
+3  A: 

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.

FlySwat
+3  A: 
dlamblin
+3  A: 
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.

Larry OBrien