Skip to content

[generator] Remove temporary creation of NSArray instances in generated bindings #12649

Open

Description

@spouliot

Look for them using

$ git grep NSArray src/generat*.cs

There's a few example that generates them. Here's one from Trampolines.g.cs

Current Code

unsafe void Invoke (nint statusFlag1, nint statusFlag2, global::CoreNFC.EncryptionId encryptionIdentifier, NSData[] nodeKeyVersionListAes, NSData[] nodeKeyVersionListDes, NSError error)
{
	if (nodeKeyVersionListAes is null)
		ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (nodeKeyVersionListAes));
	if (nodeKeyVersionListDes is null)
		ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (nodeKeyVersionListDes));
	var error__handle__ = error.GetHandle ();
	var nsa_nodeKeyVersionListAes = nodeKeyVersionListAes is null ? null : NSArray.FromNSObjects (nodeKeyVersionListAes);
	var nsa_nodeKeyVersionListDes = nodeKeyVersionListDes is null ? null : NSArray.FromNSObjects (nodeKeyVersionListDes);
	invoker (BlockPointer, statusFlag1, statusFlag2, (nint) (Int64) encryptionIdentifier, nsa_nodeKeyVersionListAes is null ? IntPtr.Zero : nsa_nodeKeyVersionListAes.Handle, nsa_nodeKeyVersionListDes is null ? IntPtr.Zero : nsa_nodeKeyVersionListDes.Handle, error__handle__);
	if (nsa_nodeKeyVersionListAes != null)
		nsa_nodeKeyVersionListAes.Dispose ();
	if (nsa_nodeKeyVersionListDes != null)
		nsa_nodeKeyVersionListDes.Dispose ();
}

Better Code

unsafe void Invoke (nint statusFlag1, nint statusFlag2, global::CoreNFC.EncryptionId encryptionIdentifier, NSData[] nodeKeyVersionListAes, NSData[] nodeKeyVersionListDes, NSError error)
{
	if (nodeKeyVersionListAes is null)
		ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (nodeKeyVersionListAes));
	if (nodeKeyVersionListDes is null)
		ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (nodeKeyVersionListDes));
	var error__handle__ = error.GetHandle ();
	var nodeKeyVersionListAes__handle__ = CFArray.Create (nodeKeyVersionListAes);
	var nodeKeyVersionListDes__handle__ = CFArray.Create (nodeKeyVersionListDes);
	invoker (BlockPointer, statusFlag1, statusFlag2, (nint) (Int64) encryptionIdentifier, nodeKeyVersionListAes__handle__, nodeKeyVersionListDes__handle__, error__handle__);
	CFObject.ReleaseNative (nodeKeyVersionListAes__handle__);
	CFObject.ReleaseNative (nodeKeyVersionListDes__handle__);
}

Changes

  • remove duplicate null checks - it's done on the argument (non null) and before creating the NSArray
  • remove temporary managed NSArray creation - it's only created to get an Handle

Notes

The same pattern also exists in manual bindings - but there's more "bang in a buck" to fix it first inside the generator.

Activity

  1. added
    enhancement
    macOS
    iOS
    on Sep 7, 2021
  2. added this to the Future milestone on Sep 7, 2021
  3. spouliot commented on Sep 8, 2021

    @spouliot
    ContributorAuthor

    Better Code v2

    CFArray.Create, which would need to become public, already checks for null so...

    unsafe void Invoke (nint statusFlag1, nint statusFlag2, global::CoreNFC.EncryptionId encryptionIdentifier, NSData[] nodeKeyVersionListAes, NSData[] nodeKeyVersionListDes, NSError error)
    {
    	var nodeKeyVersionListAes__handle__ = CFArray.Create (nodeKeyVersionListAes);
    	var nodeKeyVersionListDes__handle__ = CFArray.Create (nodeKeyVersionListDes);
    	var error__handle__ = error.GetHandle ();
    	invoker (BlockPointer, statusFlag1, statusFlag2, (nint) (Int64) encryptionIdentifier, nodeKeyVersionListAes__handle__, nodeKeyVersionListDes__handle__, error__handle__);
    	CFObject.ReleaseNative (nodeKeyVersionListAes__handle__);
    	CFObject.ReleaseNative (nodeKeyVersionListDes__handle__);
    }
  4. added
    performance
    generator
    on Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgeneratoriOSmacOSperformance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions