I know that Static methods can be inlined by JIT optimization in .Net (and Mono)
My question is, can an instance method, that accesses its own state, be inlined too?
For example:
public class CaseSensitiveLiteralStringMatcher : IStringMatcher
{
private readonly LiteralToken _token;
public CaseSensitiveLiteralStringMatcher(LiteralToken token)
{
_token = token;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsMatch(char containsChar, int position)
{
return containsChar == _token.Value[position];
}
}
Would the above method call be inlined even though its not static and accesses some private member?
I found a great read here regarding this: http://blogs.microsoft.co.il/sasha/2007/02/27/jit-optimizations-inlining-and-interface-method-dispatching-part-1-of-n/
My conclusion is, instance methods can be inlined, but virtual methods can't because the actual method called can change at runtime and can not be established using static analysis of the source code.
For this reason, the method I have shown in my question could be inlined, if it wasn't an interface method - as that means it is virtual in the sense it has to be dispatched via a vtable lookup at runtime.
Saying that, there are JIT optimization techniques that can optimise virtual method inlining for the "common" case, but these come with a fallback for when the inlined method doesn't match the desired method call at runtime, which means certain code paths may benefit more from the inlining that others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With