Comment

Comment by taspeotis

Static dispatch vs. dynamic dispatch.

Assuming the functions and types are known in advance, no virtuals etc: given something like `Main` calling `DoSomething` calling `GetPropertyValue` which is

    public int GetPropertyValue(MyObject myObject) { return myObject.PropertyValue; }
When RyuJit compiles your application, it can see these methods and how they're called. `PropertyValue` can be inlined into a field access, `GetPropertyValue` can be inlined into `DoSomething` which in turn can be inlined into `Main`.

When you do something like the article:

    Func<TestClass, string> getDelegate = 
        (Func<TestClass, string>)Delegate.CreateDelegate(
                 typeof(Func<TestClass, string>), 
                 property.GetGetMethod(nonPublic: true));
    
    [Benchmark]
    public string GetViaDelegate()
    {
        return getDelegate(testClass);
    }
RyuJit can't see that `getDelegate` is the equivalent of `return myObject.PropertyValue`. It's a reference to some function. RyuJit has to compile calling an arbitrary method vs. calling `GetPropertyValue`.
parent taspeotis 10 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton