You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Research injecting a constructor to avoid reflection in NSObject.AllocateNSObject in the managed static registrar #18357
Problem: we need to set the handle and flags fields before reaching the NSObject ctor.
Solution:
// Handle32 is an enum - its only purpose is to be unique// Alternatively, we could use IntPtr and put a custom modifier on the parameter// The custom modifier also guarantees a unique signature that cannot possibly conflict with user constructors.methodpublichidebysigspecialnamertspecialnameinstancevoid.ctor(valuetype Handle32 handle) cilmanaged
{
// Store the handle first// This should be legal. It's a violation of CLS rules but not a problem for correctness or even verifiabilityldarg.1ldarg.0stfld NSObject.Handle
// Chain to the constructor we wanted to call// Load any parameter if there's anyldarg.0callinstancevoid Blah::.ctor()
ret
}
This could potentially make it much faster to create NSObjects in the MSR trampolines.
Based on the discussion in dotnet/runtime#81741 (comment) I think we should be to implement this without this IL hack using the new UnsafeAccessor attribute. I'm not sure if it would work for private-nested classes but we're already changing visibility of classes in the custom linker steps anyway.
@rolfbjarne I inspected the code we generate and I can't locate where we invoke the constructor via reflection. I believe the call to the .ctor doesn't involve reflection:
Are there cases where we would generate different code to call the constructor? I thought that it could be in the case when we create an instance of classes with generic arguments, but those can't be instantiated this way and the UCO throws an exception.
Activity
simonrozsival commentedon Jun 28, 2023
Based on the discussion in dotnet/runtime#81741 (comment) I think we should be to implement this without this IL hack using the new
UnsafeAccessorattribute. I'm not sure if it would work for private-nested classes but we're already changing visibility of classes in the custom linker steps anyway.@rolfbjarne I inspected the code we generate and I can't locate where we invoke the constructor via reflection. I believe the call to the .ctor doesn't involve reflection:

Are there cases where we would generate different code to call the constructor? I thought that it could be in the case when we create an instance of classes with generic arguments, but those can't be instantiated this way and the UCO throws an exception.
rolfbjarne commentedon Jun 28, 2023
NSObject.AllocateNSObject calls RuntimeHelpers.GetUninitializedObject (which uses reflection as far as I know):
https://github.com/xamarin/xamarin-macios/blob/3f6d43c75649a85e47fd23d6d48c8d5a87257d7b/src/Foundation/NSObject2.cs#L236
MSR: Generate additional constructors to reduce usage of reflection (#…