Sunday, March 18, 2007

Method inlining

Executing a method has a cost, this cost can be partially avoided by method inlining.
Method inlining means basically moving the method into the callers body for an eg. Take a look at the code

int Calc()
{
return x * y;
}

int num = Calc();

when Calc() is inlined what it basically means is
int num = x * y;

Unlike C++ (as I read) C# does not have inline keyword to explicitly inline methods, this is due to JIT handling it by itself.
JIT uses heuristics to select methods that are to be inlined.
The following are some of the heuristics JIT uses


Function code that produce less then 32 bytes IL

Function code the has simple control flow , this includes if- else if – else statements, functions containing swith and while flows are not inlined

Virtual functions are not inlined

Functions containing exception handling blocks are not inlined , but if the exception is just thrown without handling code it would be inlined.

No comments:

Post a Comment