Search code examples
c#optimizationmethodscil

Compiler Optimization for methods that only call another method


I think my question is best asked with an example

Method1(variable var1, variable var2)
{
    Method2([null or default value goes here], var1, var2)
}

Method2(variable newvar, variable var1, variable var2)
{
    //functionality
}

Would the compiler optimise our code by changing all calls to method1 to a call to method 2? I would think that it would inline the method. However, what if method2 calls 3 which calls 4 which calls 5 which calls 6? Does the compiler change method1 calls to a call to method 6 with the values that it will have?


Solution

  • This is completely an implementation detail, and subject to change. However, the CLR team has blogged about when methods are eligible to be inlined (though this is, of course, pre-RyuJIT).

    Note that the C# compiler doesn't ever inline the methods - the inlining happens during the JIT compilation phase. In general, a simple method call, as you described, (without virtual/abstract methods being used) will get inlined at runtime, however.