Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can .NET JIT optimization inline this method

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?

like image 856
Darrell Avatar asked Jul 23 '26 02:07

Darrell


1 Answers

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.

like image 171
Darrell Avatar answered Jul 24 '26 15:07

Darrell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!