Skip to content

Research injecting a constructor to avoid reflection in NSObject.AllocateNSObject in the managed static registrar #18357

@rolfbjarne

Description

@rolfbjarne

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
.method public hidebysig specialname rtspecialname 
        instance void  .ctor(valuetype Handle32 handle) cil managed
{
  // Store the handle first
  // This should be legal. It's a violation of CLS rules but not a problem for correctness or even verifiability
  ldarg.1
  ldarg.0
  stfld NSObject.Handle
  // Chain to the constructor we wanted to call
  // Load any parameter if there's any
  ldarg.0
  call       instance void Blah::.ctor()
  ret
}

This could potentially make it much faster to create NSObjects in the MSR trampolines.

Ref: dotnet/runtime#86649 (comment)

Metadata

Metadata

Assignees

Labels

enhancementThe issue or pull request is an enhancementperformanceIf an issue or pull request is related to performance

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions