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.47k stars 514 forks source link

Implement Class.GetHandleIntrinsic #21442

Open rolfbjarne opened 2 weeks ago

rolfbjarne commented 2 weeks ago

Many years ago we added a Class.GetHandleIntrinsic method to speed up fetching class handles:

https://github.com/xamarin/xamarin-macios/blob/209de6f29755c66a80ac3c01961b8f7390886e69/src/ObjCRuntime/Class.cs#L141-L149

However, the comment is incorrect / out-of-date, because the AOT-compiler side of the code is not there anymore.

The idea would be to:

  1. Add support in the AOT compiler for Class.GetHandleIntrinsic to do what the comment says. Mono's AOT compiler already does something similar for Selector.GetHandle: https://github.com/dotnet/runtime/blob/87a35d6bf48fb418bf53630cd346781b349148f3/src/mono/mono/mini/intrinsics.c#L2221-L2261, which can be used as a starting point (the implementation would be different though - the generated native code should be equivalent to the Objective-C code [ObjectiveCType class].
  2. Change our code to use Class.GetHandleIntrinsic instead of Class.GetHandle whenever possible:
    1. Some kind of optimization step during app build time is probably needed, because whether the Class.GetHandle -> Class.GetHandleIntrinsic transformation is safe or not partial depends on build settings (in particular the deployment target).
    2. Modify the generator to emit Class.GetHandleIntrinsic when safe to do so (when the class in question is available in all OS versions we support).
    3. Modify our manual code as well.
    4. Add a (cecil?) test to verify that we got the above changes right (and we don't introduce regressions in the future).