xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.44k stars 506 forks source link

Emit specialized native method for methods in a closed subclass over a generic type #14899

Open Therzok opened 2 years ago

Therzok commented 2 years ago

Steps to Reproduce

This is used as a node for an NSOutlineView, but it's possible to reproduce by just calling the hash selector.

class RecursiveType<T> where T:RecursiveType
{
    public T MyValue;

    public override nuint GetNativeHash()
    {
        return MyValue.GetHashCode();
    }
}

sealed class ClosedView : RecursiveType<ClosedView>
{
}

Expected Behavior

The callpaths should be quick for the sealed ClosedView. The type has all the information needed to resolve without doing runtime checks.

Actual Behavior

The methods are resolved at runtime via generic APIs.

See https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1533115/ and https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1532127

Environment

VSMac, dotnet6 xamarin-macios, 6.0.201.

cc @rolfbjarne Does this need more information?

rolfbjarne commented 2 years ago

No, this is enough.

For future self: one idea to improve the performance would be to create a specialized native method for every exported managed method from a generic class whenever that class has a closed subclass. This should probably be opt-in, since it will increase app size (maybe significantly).