Repository navigation
Avoid closure allocations in callback user-data storage - #5518
mattleibow wants to merge 1 commit into
Conversation
📦 Try the packages from this PRWarning Do not run these scripts without first reviewing the code in this PR. Step 1 — Download the packages bash / macOS / Linux: curl -fsSL https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.sh | bash -s -- 5518PowerShell / Windows: iex "& { $(irm https://raw.githubusercontent.com/mono/SkiaSharp/main/scripts/get-skiasharp-pr.ps1) } 5518"Step 2 — Add the local NuGet source dotnet nuget add source ~/.skiasharp/hives/pr-5518/packages --name skiasharp-pr-5518More options
Or download manually from Azure Pipelines — look for the Remove the source when you're done: dotnet nuget remove source skiasharp-pr-5518 |
📊 SkiaSharp benchmarks — PR #5518⭐ this PR (full source build) vs 🌙 nightly · Linux · Windows · macOS
Highlights⏱️ Time — 🔴 40 slower · 🟢 24 faster
Full per-OS benchmark deltasLinux⏱️ Time (vs 🌙 nightly
Windows⏱️ Time (vs 🌙 nightly
macOS⏱️ Time (vs 🌙 nightly
|
…cks (#5479) Use blittable Byte for C bool and Int32 for hb_bool_t delegate callbacks (#5479) Context: dotnet/runtime#136578 Context: #5464 Context: #5518 Match generated unmanaged callback delegates, function pointers, and DelegateProxies implementations to their native C ABI boolean widths by using 1-byte Byte for Skia C/C++ bool and 4-byte Int32 for HarfBuzz hb_bool_t, while keeping public managed delegate and API signatures on bool. ~~ Use 1-byte Byte for Skia C/C++ bool callbacks ~~ SkiaSharpGenerator mapped C bool to Byte for struct fields, but overrode callback parameters and returns back to bool with [MarshalAs(UnmanagedType.I1)]. [UnmanagedCallersOnly] and delegate* unmanaged[Cdecl] ignore [MarshalAs], and the Mono ARM64 interpreter (dotnet/runtime#136578) does not zero-extend bool returns across the 32-bit return register expected by Apple ARM64 C++ callers. Map C bool to Byte in GetManagedFunctionArguments and GetFunctionPointerType (BaseTool.cs), remove [return: MarshalAs(UnmanagedType.I1)] from WriteDelegate and WriteDelegateProxy (Generator.cs), and update the Skia stream, write-stream, and Graphite image-provider proxy implementations to convert between byte and bool. Forward P/Invoke declarations in WriteFunctions keep [MarshalAs(UnmanagedType.I1)] bool. ~~ Use 4-byte Int32 for HarfBuzz hb_bool_t callbacks ~~ HarfBuzz defines typedef int hb_bool_t; (4 bytes), which libHarfBuzzSharp.json maps to "Boolean". Treating "Boolean" the same as C bool generated 1-byte I1 or unmarshaled bool returns instead of 4-byte int callbacks. Map "Boolean" to Int32 for callback delegates and function pointers, update the HarfBuzz font and Unicode proxy implementations to return 1 or 0, and return 0 immediately in FontGetGlyphNameProxyImplementation before copying the name span when GlyphNameDelegate returns false. HBFontFuncsTest and HBUnicodeFuncsTest verify false-returning callbacks. Co-authored-by: Matthew Leibowitz <[email protected]> Co-authored-by: Copilot App <[email protected]> Co-authored-by: Alex Soto <[email protected]>
f23fb2d to
1be958c
Compare
1b4815a to
f213039
Compare
1be958c to
27b1edf
Compare
f213039 to
5d22a24
Compare
27b1edf to
d368194
Compare
5d22a24 to
67e9769
Compare
d368194 to
24d23ad
Compare
2339d6a to
ef410c8
Compare
24d23ad to
e0d0911
Compare
ef410c8 to
eac144a
Compare
e0d0911 to
c83fc51
Compare
eac144a to
3cc669d
Compare
c83fc51 to
73d502f
Compare
3cc669d to
887187f
Compare
73d502f to
25c982d
Compare
Store user data directly in GCHandles and use a MultiUserData container for delegate lookup instead of allocating capturing helper delegates. Preserve weak-reference storage and declared delegate type tokens, including null delegates. Extract only DelegateProxies.shared.cs from the frozen callback work so this managed allocation cleanup can ship independently of the callback ABI and stream safety changes. Co-authored-by: Copilot App <[email protected]>
25c982d to
49358d0
Compare
|
@dalexsoto review |
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the callback-context storage refactor.
No open issues 🎉
What changed in this PR
This PR changes internal callback-context storage shared by SkiaSharp and HarfBuzzSharp to remove capturing helper delegates. It is separate from the stream-correctness work and leaves public signatures and callback ABI unchanged.
Changes:
- Stores user data directly in a
GCHandle, retaining weak-reference unwrapping. - Replaces closure-based multi-delegate lookup with a
MultiUserDatacontainer that preserves declared delegate types and null-delegate lookup behavior.
| File | Description |
|---|---|
| binding/Binding.Shared/DelegateProxies.shared.cs | Replaces closure-based callback storage with direct user-data storage and a typed multi-delegate container. |
Review effort: Balanced
dalexsoto
left a comment
There was a problem hiding this comment.
Reviewed the complete changed delegate-context implementation and affected SkiaSharp/HarfBuzz callback producers, consumers, rooting/release paths, generated entry-point branches, compilation conditions and relevant existing tests, including a separate final search for independent blockers.
The allocation-only extraction preserves the reviewed weak-reference lifetime, declared delegate-type and optional-null-delegate handling, and callback contracts. No high-confidence blocking issue remains in this bounded static review. No build/test execution, generated-output regeneration or native-archive certification is claimed.
Description
Remove capturing helper delegates from callback user-data storage shared by SkiaSharp and HarfBuzzSharp. Store user data directly in its GCHandle, and use a
MultiUserDatacontainer to retain delegates and their declared type tokens instead of closure-based lookup.The removed closures and helper delegates were allocated when creating callback contexts, not on each callback invocation. Multi-value contexts still allocate a
MultiUserDatacontainer, and weak user-data contexts still allocate aWeakReference. This is not a claim that callback creation or invocation is allocation-free. No new benchmark was run, and no measured speedup or allocation-count claim is made here.This PR targets
maindirectly and is independent of the stream-correctness work in #5464. It does not need the former lower stream layers and is not registered in a native PR stack. Onlybinding/Binding.Shared/DelegateProxies.shared.cschanges (+76/-114): the exact implementation extracted from frozen #5479 (533564d4ac2859a025514a4e60831a5786c21dcf), with no redesign or additional changes. Direct GCHandle user-data storage, existing weak-reference semantics, declared delegate type tokens, and null-delegate lookup behavior are preserved as in that reviewed implementation.No callback ABI, public signatures, exception policy, stream lifetime policy, stream correctness code, native pin, generated bindings, tests, or benchmarks are changed by this PR. Independent validation was performed in a fresh
main-based checkout without importing the former stream-layer commits.Related issues
Related to #5479 and #5464. Neither is a prerequisite for this allocation-only change beyond the code already present in
main.Required skia PR
None.
Areas affected
binding/)externals/skia/src/c,include/c)Changes
None. No public API, callback ABI, rendering, exception-policy, or lifetime-policy changes; this PR only changes internal callback context storage to avoid capturing helper delegates.
Testing
Named independently tested snapshot
49358d0a30d162cb0208f4d190930e3dc12ac2b2, based directly onmaincommit7b0b03819c9c1cd22ca8403ced9859f9f5cfe565, passed in a fresh isolated macOS ARM64 checkout. Exactly one allocation commit sits above that baseline; no stream-correctness layer was imported. Exact HEAD/branch and clean-worktree checks passed before and after each command:dotnet build binding/SkiaSharp/SkiaSharp.csproj --verbosity quiet— passed, zero errors and four warnings; all 13 normally configured TFMs, includingnet48, with no framework overrides.dotnet build binding/HarfBuzzSharp/HarfBuzzSharp.csproj --verbosity quiet— passed, zero errors and four warnings; all 13 normally configured TFMs, includingnet48, with no framework overrides.dotnet build tests/SkiaSharp.Tests.Console.slnx -p:TargetFramework=net10.0 -p:TargetFrameworks=net10.0 --no-incremental --verbosity quiet— passed, zero errors and 58 warnings; rebuilt the test hosts before native-copy verification and execution.dotnet test tests/SkiaSharp.Tests.Console.slnx -p:TargetFramework=net10.0 -p:TargetFrameworks=net10.0— passed unfiltered: 6,532 passed, 47 existing platform/capability skips, zero failed. Core: 6,475 passed / 23 skipped; singleton: 1 / 0; gallery: 50 / 0; Vulkan: 4 / 21; Direct3D: 2 / 3.git range-diff --no-color 887187f375a815fa54097e5dcd101cb2b0c68bd0..25c982d11e64c993a166392223b60c0473edac38 7b0b03819c9c1cd22ca8403ced9859f9f5cfe565..49358d0a30d162cb0208f4d190930e3dc12ac2b2— passed: the extracted allocation patch is unchanged. Its committed binary diff is byte-identical to the frozen original one-file patch; resulting source blob37985b79479c40506255834df3a4cf5490ba023a, patch SHA2563e54b2368b0285dd058212375b1138ac44aec7412ceaa3eb4a8e5da02ba9e453.Used source-matched macOS native assets from Azure DevOps main build 1655687, source
b45912c193582b20664445e506e92f773a1aa6e8, Skia pin748bc0c0116c3952f0a0089f5cc53edc55ae08de(m156). Native pins, version inputs, and externals were verified unchanged on the testedmainbaseline. Verified archive SHA256e78c31c215234b264a7c1cde97f6ce082488d076fe7911021baad4ff4a7bdd41, extracted only the two named regular dylibs, and checked their hashes in all five test-host output directories before and after execution. SkiaSharp SHA256:c7ff59007830d44a91f08d24ae3120de1e6e43d921a5b4f3c525346d7648097e; HarfBuzzSharp SHA256:dd6f10d2c051dac3623c8af2adde87307e728569b862b7e1d15d0c75f6ff9e17.The results above refer to the named independent snapshot, not to an old stacked snapshot or a floating head. No tests, benchmark tooling, refactors, or skip-policy changes were added. Existing tests were used to validate the exact extracted implementation. Runtime execution on other platforms/TFMs was not performed. No rendering change or golden-image update is expected.
Checklist
Changesabove lists all public API and behavioral changes (or "None.")///documentation and regenerated bindings when applicablemono/skiaPR linked above and bindings regenerated