Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[corefoundation] Cache kCFNull to avoid native calls #15146

Merged
merged 1 commit into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/CoreFoundation/CFArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ namespace CoreFoundation {
// interesting bits: https://github.com/opensource-apple/CF/blob/master/CFArray.c
public partial class CFArray : NativeObject {

// this cache the handle instead of issuing a native call
internal static NativeHandle CFNullHandle = _CFNullHandle;

#if !NET
internal CFArray (NativeHandle handle)
: base (handle, false)
Expand Down
2 changes: 1 addition & 1 deletion src/corefoundation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface CFAllocator {
interface CFArray {

[Internal][Field ("kCFNull")]
IntPtr /* CFNullRef */ CFNullHandle { get; }
IntPtr /* CFNullRef */ _CFNullHandle { get; }
}

[Partial]
Expand Down
12 changes: 12 additions & 0 deletions tests/perftest/NativeArrayPerf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,17 @@ public void Create ()
var native = CFArray.Create (array);
CFString.ReleaseNative (native); // that's a `CFObject.CFRelease` with a null-check
}

int error;

[Benchmark]
public void ArrayFromHandleFunc ()
{
var native = CFArray.Create (array);
var managed = CFArray.ArrayFromHandle<NSNumber> (native);
if (managed.Length != array.Length)
error++;
CFString.ReleaseNative (native); // that's a `CFObject.CFRelease` with a null-check
}
}
}