Skip to content

perf: speed up manifest JSON rendering#874

Draft
He-Pin wants to merge 3 commits into
databricks:masterfrom
He-Pin:perf/manifest-json-rendering-fastpath
Draft

perf: speed up manifest JSON rendering#874
He-Pin wants to merge 3 commits into
databricks:masterfrom
He-Pin:perf/manifest-json-rendering-fastpath

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented May 28, 2026

Motivation

std.manifestJson, std.manifestJsonMinified, and std.manifestJsonEx still routed through StringWriter, paying StringBuffer synchronization per write and per 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.
  • Fix codepoint comparison for raw surrogate prefixes: equal surrogate UTF-16 code units must be decoded before deciding ordering. UnicodeHandlingTests extended for the prefix-ordering case.

Result

Scala Native hyperfine on kube-prometheus, -N -w 4 -m 20, jrsonnet HEAD 2d7eed05:

Workload (native) Before After Δ
kube-prometheus, sjsonnet 158.4 ± 16.8 ms 143.7 ± 3.2 ms −9.3%
kube-prometheus, jrsonnet 101.2 ± 4.4 ms 97.4 ± 8.6 ms reference
manifestJsonEx, sjsonnet 5.09 ± 1.01 ms new
manifestJsonEx, jrsonnet 4.08 ± 1.40 ms reference

JMH regression post-PR: manifestJsonEx 0.055 ms/op, realistic2 43.6 ms/op, gen_big_object 0.842 ms/op.

Related: #666.

Test plan

  • ./mill __.reformat
  • ./mill -j 1 __.test — 450/450 pass

@He-Pin He-Pin marked this pull request as ready for review May 28, 2026 06:53
@He-Pin He-Pin marked this pull request as draft May 28, 2026 06:57
@He-Pin He-Pin marked this pull request as ready for review May 28, 2026 07:00
@He-Pin He-Pin marked this pull request as draft May 28, 2026 07:12
Motivation:
std.manifestJson* still contributed to the local Scala Native gap versus source-built jrsonnet, especially in real-world object-heavy rendering.

Modification:
Add an internal StringBuilder-backed FastMaterializeJsonRenderer for std.manifestJson, std.manifestJsonMinified, and std.manifestJsonEx while preserving the public MaterializeJsonRenderer StringWriter API. Reuse an in-place codepoint key sorter backed by java.util.Arrays.sort, and fix raw-surrogate prefix ordering in compareStringsByCodepoint.

Result:
Full validation passed: ./mill --no-server --ticker false --color false __.reformat and ./mill --no-server --ticker false --color false -j 1 __.test reported 451/451 tests passing. JMH regressions: manifestJsonEx 0.055 ms/op, realistic2 43.596 ms/op, gen_big_object 0.842 ms/op. Direct hyperfine against source-built jrsonnet: manifestJsonEx sjsonnet-native 5.090 ms vs jrsonnet 4.075 ms; kube-prometheus sjsonnet-native 143.738 ms vs jrsonnet 97.385 ms.
@He-Pin He-Pin force-pushed the perf/manifest-json-rendering-fastpath branch from da92dd1 to c3581e8 Compare May 28, 2026 07:17
@He-Pin He-Pin marked this pull request as ready for review May 28, 2026 07:17
@He-Pin He-Pin marked this pull request as draft May 29, 2026 20:41
Motivation:
The JVM/char render hot 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.

Modification:
- 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; ujson.Value AST path falls back to visitString.
- Add AsciiSafeRenderBenchmark to isolate the render path for A/B.

Result:
JMH render-only, 335KB string-heavy output: 1.606 -> 1.441 ms/op (-10.3%,
non-overlapping error bands). 450/450 tests pass.
@He-Pin He-Pin marked this pull request as ready for review May 29, 2026 21:25
Motivation:
std.manifestTomlEx routed through java.io.StringWriter, whose backing
StringBuffer pays a monitor enter/exit on every write/flush on the hot TOML
manifestation path. The JSON renderer already switched to the unsynchronized
StringBuilderWriter in databricks#874 (-9.3% on kube-prometheus native); TOML did not.

Modification:
- Switch TomlRenderer and the manifestTomlEx render path in ManifestModule from
  java.io.StringWriter to the package-private StringBuilderWriter. Output is
  byte-identical. std.deepJoin keeps StringWriter (separate concern).
- Add TomlRenderBenchmark to A/B the render path.

Result:
Native hyperfine, TOML-heavy workload (1.79MB output): after ran 1.11 ± 0.07x
faster than before (~10%), output byte-identical. JMH (whole-pipeline) showed
AFTER < BEFORE in two independent rounds. 450/450 tests pass.
@He-Pin He-Pin marked this pull request as draft May 29, 2026 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant