dotnet / dotnet/macios

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

Open
#12,649 1 comment 2 reactions 0 assignees View on GitHub
enhancement generator iOS macOS performance
Dominant language
C#
Stars
2.9k
Forks
576
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Look for them using

```bash
$ git grep NSArray src/generat*.cs
```

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

## Current Code

```csharp
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

```csharp
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.