perf: route AsciiSafeStr through ByteRenderer escape-free path#903
Merged
stephenamar-db merged 1 commit intoJun 9, 2026
Merged
Conversation
Collaborator
|
Not sure it's worth it. |
Motivation: The Materializer had an ASCII-safe fast path for BaseCharRenderer (skip escape scanning for Val.AsciiSafeStr), but BaseByteRenderer (used for stdout output) lacked the same optimization. For ASCII-safe strings, ByteRenderer redundantly re-scanned the entire string via Platform.isAsciiJsonSafe before calling renderAsciiSafeString. Modification: Add visitAsciiSafeString to BaseByteRenderer that directly calls renderAsciiSafeString without the redundant scan. Route AsciiSafeStr values through this path in Materializer.visitStr for both BaseCharRenderer and BaseByteRenderer visitors. Result: Native A/B (realistic1): 11.3ms → 10.7ms (-5.4%). sjsonnet now faster than jrsonnet on realistic1 (10.7ms vs 10.8ms).
a1c14f7 to
484cd1c
Compare
Contributor
Author
|
I re-run it with jmh, and the numbers show up. |
stephenamar-db
approved these changes
Jun 9, 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
The Materializer had an ASCII-safe fast path for
BaseCharRenderer(skip escape scanning forVal.AsciiSafeStr), butBaseByteRenderer(used for the stdout byte pipeline) lacked the same optimization. ForAsciiSafeStrvalues,ByteRenderer.visitLongStringredundantly re-scanned the entire string viaPlatform.isAsciiJsonSafe— an O(n) scan — before callingrenderAsciiSafeString. For large ASCII-safe strings (common in Jsonnet config output), this is wasted work.Modification
visitAsciiSafeString(str, index)toBaseByteRendererthat directly callsrenderAsciiSafeStringwithout the redundantisAsciiJsonSafescanMaterializer.visitStr, checkisInstanceOf[Val.AsciiSafeStr]first, then dispatch to the appropriate renderer's fast path for bothBaseCharRendererandBaseByteRendererResult
JVM JMH full regression suite (JDK 21, G1GC, -Xmx4G, @fork(1) @WarmUp(1) @measurement(1)):
46 benchmarks total. No confirmed regressions. String-heavy workloads show consistent 3-19% improvement.
Scala Native hyperfine (Scala Native 0.5.12, macOS arm64):
Large output workloads (153KB-549KB JSON) show 13-23% improvement on Scala Native. The optimization is most effective when ByteRenderer rendering dominates execution time. realistic2 (28MB) renders in ~103ms but evaluation dominates, so rendering savings are negligible.
References