perf: defer Position alloc in exprSuffix2 to the matching branch#877
Merged
stephenamar-db merged 1 commit intoJun 2, 2026
Merged
Conversation
2 tasks
2 tasks
Motivation:
exprSuffix2 was `Pos.flatMapX { i => CharIn(".[({")... }`, which allocated a
Position on EVERY attempt — including the failing attempt that terminates
`exprSuffix2.rep` after each expression. Most subexpressions have no suffix, so
that trailing failed attempt (one per expression) allocated a Position that was
immediately discarded.
Modification:
- Match the suffix char first; allocate `new Position(fileScope, ctx.index - 1)`
only inside the matching branch. No suffix -> CharIn fails fast, no Position.
Also drops the `.map(_(0))` Char step. Parse output (positions/errors) is
unchanged.
Result:
JMH ParserBenchmark (-f0, same-session): 1.560 -> 1.530 ms/op (+1.9%). Native
parse_time on kube-prometheus: non-regressing, min/p25 ~2% lower (noise-limited
on a loaded machine). Output byte-identical. 517/517 tests pass.
061c6ce to
572d45c
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
exprSuffix2wasPos.flatMapX { i => CharIn(".[({") ... }, which allocated aPositionon every attempt — including the failing attempt that terminatesexprSuffix2.repafter each expression. Most subexpressions have no suffix, so that trailing failed attempt allocates aPositionthat is immediately discarded.Modification
new Position(fileScope, ctx.index - 1)only inside the matching branch. No suffix →CharInfails fast, noPosition. Also drops the intermediate.map(_(0))step. Parse output (positions / error offsets) is unchanged.Result
ParserBenchmark(JVM, over the test suite): 1.560 → 1.530 ms/op (+1.9%, non-overlapping bands).--debug-stats parse_timeon kube-prometheus (interleaved, cooled): neutral-to-slightly-faster (min 89.1 vs 95.9 ms; mean 107.7 vs 110.0 ms — within noise on this loaded run, no regression).Test plan
./mill __.reformat./mill 'sjsonnet.jvm[3.3.7]'.test— 518/518 pass