[performance] Avoid intermediate string allocation in GetEncodedText null-terminated path - #4498
Merged
mattleibow merged 1 commit intoJul 22, 2026
Conversation
…d path Encode text directly into a byte[byteCount + nullBytes] buffer instead of concatenating a '\0' onto the source string before encoding. The trailing zero bytes match the encoding of a '\0' exactly (UTF-8=1, UTF-16=2, UTF-32=4 bytes), so output is byte-identical while removing one managed string allocation and copy per call on the native-marshalling hot path. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
mattleibow
marked this pull request as ready for review
July 22, 2026 21:48
This was referenced Jul 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
StringUtilities.GetEncodedText(text, encoding, addNull: true)previously appended the NUL terminator withtext += "\0", allocating a whole new managed string (heap object + char copy) before encoding. This PR encodes the original text directly into abyte[byteCount + nullBytes]buffer and leaves the trailing bytes zero.Invariant that keeps it correct: a
'\0'char encodes to exactlyGetCharacterByteSizezero bytes in each encoding (UTF‐8 = 1, UTF‐16 = 2, UTF‐32 = 4), and encoding the text without the trailing'\0'produces the identical prefix bytes. So the result is byte-for-byte identical, with one fewer allocation and one fewer copy. Empty/nullinput still returns an empty array (no terminator), matching the original!string.IsNullOrEmptyguard.Files:
binding/SkiaSharp/Util.cs— the fix (body-only, ABI-safe; unusedNullTerminatorconst removed).tests/Tests/SkiaSharp/StringUtilitiesTest.cs— equivalence test.benchmarks/SkiaSharp.Benchmarks/Benchmarks/GetEncodedTextBenchmark.cs— New-vs-Old benchmark.Proof — faster
net10.0, AMD EPYC 7763, BenchmarkDotNet 0.15.8, UTF‐8 with
addNull, two runs agree:ArialSegoe UI Symbol~30 % faster, ~55–60 % fewer bytes allocated, no allocation regression.
Proof — identical
StringUtilitiesTestcompares the new path byte-for-byte against the originaltext + '\0'semantics across UTF‐8/16/32 and edge inputs:null, empty, embedded\0, valid\uD83D\uDE00and lone surrogates, BOM, and a 1000-char string. It also asserts theaddNullvs non-addNulloutputs differ by exactly one encoded null character, so the test catches a deliberately-wrong result.Scope note
Managed-C#-only,
binding/SkiaSharp/Util.cs; no public signature change (method body only), ABI-safe. Numbers are empirically measured on the named hardware/TFM. Repo NuGet feeds (Azure DevOps) were firewall-blocked in the workflow environment, so both proofs were run with standalone projects containing the verbatim Old/New bodies; the committed test + benchmark run in CI. Behaviour is unchanged — SkiaSharp still renders identically.Produced by the performance-fixer agentic workflow +
performance-fixerskill.Fixes #4497