perf: skip escape scan for AsciiSafeStr in the char renderer#878
Merged
Conversation
2 tasks
The char render path (BaseCharRenderer.visitNonNullString) ran a CharSWAR.hasEscapeChar scan on every string, even for Val.AsciiSafeStr which is statically known to need no JSON escaping (chars 0x20-0x7e, no quote/backslash). The Native ByteRenderer already had this bypass; the char path did not. - Add BaseCharRenderer.visitAsciiSafeString: quote + bulk getChars + quote, correct even under escapeUnicode since all chars are <= 0x7e. - Route Val.AsciiSafeStr through it via a Materializer.visitStr helper at the three value-string sites; the ujson.Value AST path falls back to visitString. Result: - JMH render-only (string-heavy, 335 KB): 1.606 -> 1.441 ms/op (+10%). - Scala Native, isolated and interleaved: manifestJsonEx total_time 30.6 -> 29.5 ms (~+3.6%); kube-prometheus materialize_time neutral. Output byte-identical.
0aa826f to
a538958
Compare
stephenamar-db
approved these changes
Jun 2, 2026
stephenamar-db
pushed a commit
that referenced
this pull request
Jun 2, 2026
## Motivation `std.manifestJson`, `std.manifestJsonMinified`, and `std.manifestJsonEx` routed through `java.io.StringWriter`, paying `StringBuffer` synchronization per `write`/`flush` on the hot manifestation path. Source-built jrsonnet comparisons showed sjsonnet trailing on object-heavy manifest workloads. ## Modification - Add `StringBuilderWriter`: an unsynchronized `Writer` over a `StringBuilder`. - Add package-private `FastMaterializeJsonRenderer` backed by `StringBuilderWriter`; route the three `std.manifestJson*` builtins through it. Public `MaterializeJsonRenderer` ABI/shape unchanged. - Use an in-place codepoint sort for `sortedVisibleKeyNames` / `maybeSortKeys` (avoids `.sorted` boxing). - Fix codepoint comparison for raw surrogate prefixes; `UnicodeHandlingTests` extended. ## Result Scala Native hyperfine on kube-prometheus, jrsonnet HEAD `2d7eed05`: | Workload (native) | Before | After | Δ | |---|---:|---:|---:| | kube-prometheus, sjsonnet | 158.4 ± 16.8 ms | 143.7 ± 3.2 ms | **−9.3%** | | `manifestJsonEx`, sjsonnet | — | 5.09 ± 1.01 ms | new | ## Test plan - [x] `./mill __.reformat` - [x] `./mill 'sjsonnet.jvm[3.3.7]'.test` — 518/518 pass This PR is the base for the stacked follow-ups #875 (TomlRenderer reuses `StringBuilderWriter`), and the independent #876/#877/#878.
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 char render path (
BaseCharRenderer.visitNonNullString) ran aCharSWAR.hasEscapeCharscan on every string, even forVal.AsciiSafeStr— which is statically known to need no JSON escaping (chars0x20–0x7e, no"/\). The NativeByteRendereralready had this bypass; the char path did not.Modification
BaseCharRenderer.visitAsciiSafeString:"+ bulkgetChars+", correct even underescapeUnicodesince every char is<= 0x7e.Val.AsciiSafeStrthrough it via aMaterializer.visitStrhelper at the three value-string sites; theujson.ValueAST path falls back tovisitString.Result
manifestJsonExtotal_time 30.6 → 29.5 ms (~+3.6%); kube-prometheusmaterialize_timeneutral (no regression). Output byte-identical.Test plan
./mill __.reformat./mill 'sjsonnet.jvm[3.3.7]'.test— 518/518 pass