Skip to content

perf: speed up manifest JSON rendering#879

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/manifest-json-rendering
Open

perf: speed up manifest JSON rendering#879
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/manifest-json-rendering

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented May 30, 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

  • ./mill __.reformat
  • ./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.

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.
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