Skip to content

[performance] Remove per-call char boxing in SKFourByteTag/Tag ToString - #4752

Merged
mattleibow merged 1 commit into
mainfrom
dev/perf-fourbytetag-tostring-2276849d3fc5a4d6
Aug 20, 2026
Merged

mattleibow merged 1 commit into
mainfrom
dev/perf-fourbytetag-tostring-2276849d3fc5a4d6

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Changes

SKFourByteTag.ToString() and the general path of HarfBuzzSharp.Tag.ToString() formatted their 4-character tag with string.Concat((char),(char),(char),(char)). There is no 4-char string.Concat overload, so the call bound to string.Concat(object, object, object, object) and boxed each char — four throwaway allocations per call on top of the result string.

Both now write the four chars into a stackalloc buffer and materialize the string once via new string(char*, 0, 4). This constructor exists on every TFM (net10.0 / netstandard2.0 / net4x), so there is no #if. Body-only change — no public signature changed (ABI-safe).

Invariant that keeps it correct: each output char is exactly (char)(byte)(value >> shift) in the same big-endian order and the string is exactly 4 chars long — identical to the original Concat. The HarfBuzz None/Max/MaxSigned named special-cases are untouched and still take their early-return path.

Required skia PR

None.

Areas Affected

  • SkiaSharp (SKFourByteTag)
  • HarfBuzzSharp (Tag)

Proof — faster

In-process New-vs-Old on net10.0 / Linux x64, batch of 12 OpenType tags:

Metric Old (string.Concat) New (stackalloc)
Time ~76 ns/op ~13 ns/op (~6x)
Allocated 280 bytes/op 32 bytes/op

Runs: 5.91x / 6.17x / 5.84x; allocations 280->32 bytes/op (the 4 char boxes removed). A committed BenchmarkDotNet harness reproduces this:

dotnet run -c Release --project benchmarks/SkiaSharp.Benchmarks -- --filter '*SKFourByteTagToString*'

Proof — identical

New ToString equivalence tests compare the optimized output against the verbatim original string.Concat oracle:

  • tests/Tests/SkiaSharp/SKFourByteTagTest.cs — happy path, space-padding ("a ", "ab "), NUL, control chars (\t\r\n), >0xFF low-byte truncation, all-0xFF; plus a deliberately-wrong-result guard.
  • tests/Tests/HarfBuzzSharp/HBTagTest.cs — same coverage, plus the None/Max/MaxSigned named special-cases must still round-trip, plus a wrong-result guard.

Cross-TFM compilation verified on net10.0, netstandard2.0, and net462.

Testing

  • Standalone parity + allocation harness (bit-identical output; 280->32 bytes/op).
  • Equivalence tests added (above). The full dotnet test gate and BenchmarkDotNet harness could not run in this sandbox (network/execution restrictions blocked externals-download); the change is pure managed char/int math with no P/Invoke, so native binaries are not required to measure or validate it. Please run the full suite in CI.

Produced by the performance-fixer agentic workflow + performance-fixer skill. Empirically measured: time + allocations; statically reasoned: cross-runtime bit-exactness. No ABI impact (method bodies only). Label perf/allocations reflects the dominant measured driver (removed char boxing).

Fixes #4751

Generated by Fixer - Performance · opus48 · 472.9 AIC · ⌖ 36.8 AIC · ⊞ 13K · ◷

string.Concat(char,char,char,char) binds to Concat(object,...) and boxes
every char (4 allocations/call). Write into a stackalloc buffer and
materialize once via new string(char*,0,4) instead: ~6x faster, 280->32
bytes/op, bit-identical output on all TFMs.

Co-authored-by: Copilot <[email protected]>
@github-actions github-actions Bot added partner/agentic-workflows Issues and PRs created by SkiaSharp agentic workflows. perf/allocations Excessive managed allocations or per-frame GC/heap churn. Implies tenet/performance. tenet/performance Performance related issues labels Aug 12, 2026
@mattleibow
mattleibow marked this pull request as ready for review August 20, 2026 01:08
@mattleibow
mattleibow merged commit 3b2bff1 into main Aug 20, 2026
109 of 112 checks passed
@mattleibow
mattleibow deleted the dev/perf-fourbytetag-tostring-2276849d3fc5a4d6 branch August 20, 2026 01:08
@github-actions

Copy link
Copy Markdown
Contributor Author

📊 SkiaSharp benchmarks — PR #4752

⭐ this PR (full source build) vs 🌙 nightly · Linux · Windows · macOS

Informational only — this never blocks the PR. 🟢 faster / less allocation · 🔴 slower / more allocation; moves under 5% are hidden as noise.

⏱️ Times are raw BenchmarkDotNet means, and the ⭐ PR and baseline legs run on separate CI runners, so microbenchmarks can swing run-to-run — treat small time deltas as noise. Allocations are deterministic and the reliable signal. The interactive perf-dashboard (linked below) applies smoothing for the trend view.

Highlights

⏱️ Time — 🔴 30 slower · 🟢 17 faster

  • 🔴 ColorMathBenchmark.ToColor(Colors: 4096) · Windows · 11.93 µs → 17.03 µs (+43%)
  • 🔴 RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) · Windows · 96.45 µs → 127.64 µs (+32%)
  • 🔴 PathBoundsBenchmark.TightBounds(Points: 64) · macOS · 850.3 ns → 1.11 µs (+31%)
  • 🔴 MatrixMapPointsBenchmark.MapPoints(Points: 4096) · macOS · 1.52 µs → 1.96 µs (+29%)
  • 🔴 PathBoundsBenchmark.TightBounds(Points: 64) · Windows · 1.61 µs → 2.04 µs (+27%)
  • …and 42 more (see details below)
Full per-OS benchmark deltas

Linux

⏱️ Time (vs 🌙 nightly 4.152.0-nightly.10)

Benchmark baseline this PR Δ
MatrixMapPointsBenchmark.MapPoints(Points: 4096) 2.62 µs 3.06 µs 🔴 +17%
MatrixMapPointsBenchmark.MapPoints(Points: 256) 209.2 ns 225.6 ns 🔴 +8%
RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) 100.53 µs 108.25 µs 🔴 +8%
RasterImageLifecycleBenchmark.CreateRasterImage(Count: 256) 257.65 µs 274.42 µs 🔴 +7%
ColorMathBenchmark.UnPreMultiply(Colors: 4096) 11.44 µs 12.11 µs 🔴 +6%

Windows

⏱️ Time (vs 🌙 nightly 4.152.0-nightly.10)

Benchmark baseline this PR Δ
ColorMathBenchmark.ToColor(Colors: 4096) 11.93 µs 17.03 µs 🔴 +43%
RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) 96.45 µs 127.64 µs 🔴 +32%
PathBoundsBenchmark.TightBounds(Points: 64) 1.61 µs 2.04 µs 🔴 +27%
PathBoundsBenchmark.TightBounds(Points: 1024) 16.70 µs 20.74 µs 🔴 +24%
RasterImageLifecycleBenchmark.CreateRasterImage(Count: 256) 238.66 µs 284.29 µs 🔴 +19%
CanvasDrawBenchmark.Draw(Shapes: 512) 20.57 ms 23.17 ms 🔴 +13%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 2048) 3.25 ms 2.84 ms 🟢 -13%
MatrixMapPointsBenchmark.MapRadius(Points: 4096) 153.16 µs 169.07 µs 🔴 +10%
ColorMathBenchmark.UnPreMultiply(Colors: 4096) 12.79 µs 11.56 µs 🟢 -10%
CanvasDrawBenchmark.Draw(Shapes: 64) 2.65 ms 2.90 ms 🔴 +10%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 2048) 11.24 ms 10.19 ms 🟢 -9%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 1024) 822.95 µs 746.60 µs 🟢 -9%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 1024) 2.84 ms 2.58 ms 🟢 -9%
MatrixMapPointsBenchmark.MapPoint(Points: 256) 5.42 µs 4.94 µs 🟢 -9%
ColorParseBenchmark.Parse(Iterations: 1000) 77.81 µs 84.70 µs 🔴 +9%
MatrixMapPointsBenchmark.MapPoint(Points: 4096) 86.67 µs 79.30 µs 🟢 -9%
MatrixOpsBenchmark.Invert(Count: 4096) 148.24 µs 136.80 µs 🟢 -8%
RuntimeEffectShaderBenchmark.DrawFrame 1.22 ms 1.13 ms 🟢 -7%
MatrixMapPointsBenchmark.MapPoints(Points: 256) 182.7 ns 170.0 ns 🟢 -7%
MatrixMapPointsBenchmark.MapPoints(Points: 4096) 2.10 µs 2.24 µs 🔴 +7%
ColorMathBenchmark.ToColorF(Colors: 4096) 14.14 µs 13.32 µs 🟢 -6%
MatrixMapPointsBenchmark.MapRect(Points: 256) 8.53 µs 8.06 µs 🟢 -6%

macOS

⏱️ Time (vs 🌙 nightly 4.152.0-nightly.10)

Benchmark baseline this PR Δ
PathBoundsBenchmark.TightBounds(Points: 64) 850.3 ns 1.11 µs 🔴 +31%
MatrixMapPointsBenchmark.MapPoints(Points: 4096) 1.52 µs 1.96 µs 🔴 +29%
BitmapDrawBenchmark.DrawScaledTiles(Tiles: 64) 195.71 µs 244.63 µs 🔴 +25%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 2048) 2.69 ms 2.02 ms 🟢 -25%
RasterImageLifecycleBenchmark.CreateRasterImage(Count: 256) 141.56 µs 171.41 µs 🔴 +21%
RasterImageLifecycleBenchmark.CreateDataWithReleaseProc(Count: 256) 66.42 µs 78.83 µs 🔴 +19%
RuntimeEffectShaderBenchmark.DrawFrame 507.38 µs 591.82 µs 🔴 +17%
MatrixOpsBenchmark.Concat(Count: 4096) 111.66 µs 130.05 µs 🔴 +16%
LargeImageScaleBenchmark.UpscaleOpaque(Size: 1024) 671.67 µs 562.36 µs 🟢 -16%
MatrixMapPointsBenchmark.MapRadius(Points: 4096) 97.96 µs 112.40 µs 🔴 +15%
ColorMathBenchmark.ToColor(Colors: 4096) 18.93 µs 16.20 µs 🟢 -14%
MatrixMapPointsBenchmark.MapVector(Points: 256) 2.43 µs 2.77 µs 🔴 +14%
MatrixOpsBenchmark.Invert(Count: 4096) 65.06 µs 74.05 µs 🔴 +14%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 2048) 5.71 ms 4.95 ms 🟢 -13%
LargeImageScaleBenchmark.UpscaleCrossfade(Size: 1024) 1.45 ms 1.26 ms 🟢 -13%
MatrixMapPointsBenchmark.MapPoint(Points: 4096) 18.14 µs 20.46 µs 🔴 +13%
PathBoundsBenchmark.TightBounds(Points: 1024) 8.07 µs 8.96 µs 🔴 +11%
MatrixMapPointsBenchmark.MapVector(Points: 4096) 39.10 µs 42.90 µs 🔴 +10%
MatrixMapPointsBenchmark.MapRadius(Points: 256) 6.17 µs 6.75 µs 🔴 +9%
ColorParseBenchmark.Parse(Iterations: 1000) 63.10 µs 66.63 µs 🔴 +6%

📈 Full interactive perf-dashboard & run details →

@github-actions

Copy link
Copy Markdown
Contributor Author

📖 Documentation Preview

The documentation for this PR has been deployed and is available at:

🔗 View Staging Site
🔗 View Staging Docs
🔗 View Staging Gallery (Blazor)
🔗 View Staging Gallery (Uno Platform)
🔗 View Staging SkiaFiddle

This preview will be updated automatically when you push new commits to this PR.


This comment is automatically updated by the documentation staging workflow.

@github-actions github-actions Bot added this to the 4.152.0-rc.1 milestone Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

partner/agentic-workflows Issues and PRs created by SkiaSharp agentic workflows. perf/allocations Excessive managed allocations or per-frame GC/heap churn. Implies tenet/performance. tenet/performance Performance related issues

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[performance] SKFourByteTag/Tag.ToString() boxes four chars per call via string.Concat

1 participant