Skip to content

perf: use unsynchronized StringBuilderWriter in TomlRenderer#875

Draft
He-Pin wants to merge 2 commits into
databricks:masterfrom
He-Pin:perf/toml-stringbuilder-writer
Draft

perf: use unsynchronized StringBuilderWriter in TomlRenderer#875
He-Pin wants to merge 2 commits into
databricks:masterfrom
He-Pin:perf/toml-stringbuilder-writer

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

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

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 manifest renderer already uses the unsynchronized StringBuilderWriter; 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).

Result

Scala Native hyperfine, TOML-heavy workload (~1.8 MB output), interleaved and cooled:

mean min
master 55.4 ± 9.5 ms 45.6 ms
this PR 48.7 ± 5.8 ms 42.8 ms

1.14 ± 0.24× faster; output byte-identical.

Test plan

  • ./mill __.reformat
  • ./mill 'sjsonnet.jvm[3.3.7]'.test — 518/518 pass

Stacked on #879 (which introduces StringBuilderWriter). Until #879 merges, this PR shows 2 commits; it will be rebased to a single commit once #879 lands.

He-Pin added 2 commits May 28, 2026 13:32
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.
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. Switch TomlRenderer and the manifestTomlEx render path in
ManifestModule to the unsynchronized package-private StringBuilderWriter (the
same writer the JSON manifest renderer uses). Output is byte-identical;
std.deepJoin keeps StringWriter (separate concern).

Result (Scala Native hyperfine, TOML-heavy workload, ~1.8 MB output):
after ran 1.11 ± 0.07x faster than before (~10%); output byte-identical.
@He-Pin He-Pin marked this pull request as draft May 30, 2026 12:48
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