perf: escape-free string rendering fast path with bulk copy#678
Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Open
perf: escape-free string rendering fast path with bulk copy#678He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
Add a fast path in visitNonNullString that scans the string for chars needing escaping (control chars, quotes, backslashes). When no escaping is needed (the common case for Jsonnet output), bulk-copy the entire string into the CharBuilder using String.getChars instead of going through upickle's per-character RenderUtils.escapeChar pipeline. Upstream: jit branch commit 1d72a47
ba698a9 to
8ade3a2
Compare
This was referenced Apr 5, 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.
Motivation
Most Jsonnet strings are pure ASCII without escape characters. The current renderer dispatches every character through
RenderUtils.escapeChar, which involves a per-character method call with a match statement to check for control chars, quotes, and backslashes. For strings that need no escaping (the common case), this per-character dispatch is unnecessary overhead.Key Design Decision
Add a pre-scan fast path in
visitNonNullString: quickly check if any character needs escaping. If not, use a singleString.getCharsbulk memcpy with surrounding quotes instead of character-by-character processing. Falls back to the originalRenderUtils.escapeCharpath when any character needs escaping.The optimization targets the JVM's polymorphic dispatch overhead — on Scala Native (AOT-compiled), the impact is minimal since there's no JIT deoptimization from polymorphic call sites.
Modification
BaseCharRenderer.scala—visitNonNullString:c < 32 || c == '"' || c == '\\'ensureLength(len + 2)→appendUnsafe('"')→String.getCharsbulk copy →appendUnsafe('"')RenderUtils.escapeCharfor strings with special charactersStringinstances when!escapeUnicode(the default)Benchmark Results
JMH (JVM, Scala 3.3.7)
All 35 benchmarks checked, zero regressions.
Native (Scala Native, hyperfine --warmup 5 --runs 15 -N)
Native impact is minimal because Scala Native AOT-compiles code — the JVM JIT's polymorphic dispatch overhead for
escapeChardoesn't apply.Analysis
The escape detection scan (
c < 32 || c == '"' || c == '\\') perfectly matchesRenderUtils.escapeChar's handled characters (verified via bytecode inspection of upickle-core 4.4.2). For strings without special characters, the singleString.getCharsmemcpy replaces N individual character reads + method dispatch, giving 30%+ improvement on string-heavy JVM workloads.The optimization is particularly impactful for:
References
Result