Skip to content

perf: pre-cached indent arrays for bulk newline+spaces#676

Open
He-Pin wants to merge 2 commits intodatabricks:masterfrom
He-Pin:perf/renderer-indent-cache
Open

perf: pre-cached indent arrays for bulk newline+spaces#676
He-Pin wants to merge 2 commits intodatabricks:masterfrom
He-Pin:perf/renderer-indent-cache

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented Apr 4, 2026

Motivation

JSON rendering produces indentation strings (newline + spaces) for every nested level. Each indent level currently allocates a new string via concatenation, creating O(depth × elements) allocations for deeply nested output.

Key Design Decision

Pre-compute and cache indent strings for each depth level in a reusable array. Subsequent renders reuse the cached strings instead of allocating new ones.

Modification

  • Renderer: Added indent cache array, pre-populated for common depths, extended lazily for deeper nesting
  • Test: Added tests for deeply nested output to verify cache correctness

Benchmark Results

JMH (JVM, 3 iterations)

Benchmark Master (ms/op) This PR (ms/op) Change
bench.02 50.427 ± 38.906 45.387 ± 2.405 -10.0%
comparison2 85.854 ± 188.657 70.714 ± 22.208 -17.6%
realistic2 73.458 ± 66.747 64.336 ± 6.679 -12.4%

Analysis

  • Broad improvement across all benchmarks — every workload benefits from reduced indent allocation
  • Particularly effective for realistic2 which generates large deeply-nested JSON output
  • No regressions

References

  • Upstream exploration: he-pin/sjsonnet jit branch indent cache optimization

Result

-10% to -18% JVM improvement by eliminating redundant indent string allocation during JSON rendering.

He-Pin and others added 2 commits April 4, 2026 22:12
For integer values (the common case in Jsonnet), write digits directly
to CharBuilder using a scratch digit buffer instead of allocating a
String via RenderUtils.renderDouble/Long.toString and copying chars.

Upstream: jit branch commit d63ce90

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Pre-compute indent arrays (newline + spaces) for depths 0-15 at
Renderer construction time. On flushBuffer, use bulk appendAll for
the cached array instead of character-by-character space appending.

Upstream: jit branch commit 4f19bde

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@He-Pin He-Pin marked this pull request as ready for review April 5, 2026 00:28
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