Skip to content

Optimize ASCII-safe string rendering#831

Open
He-Pin wants to merge 2 commits intodatabricks:masterfrom
He-Pin:perf/ascii-safe-render-copy
Open

Optimize ASCII-safe string rendering#831
He-Pin wants to merge 2 commits intodatabricks:masterfrom
He-Pin:perf/ascii-safe-render-copy

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

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

Summary

  • Optimize rendering for strings that are already proven ASCII-safe.
  • JVM/Native use String#getBytes(srcBegin, srcEnd, dst, dstBegin) to copy low bytes directly into the renderer buffer without allocation.
  • Mark ASCII-safe std.base64Decode results so decoded ASCII text can use the same renderer fast path.

Motivation

The docs base64 benchmarks fold into long ASCII-safe strings, then spend visible time rendering those strings. This keeps the same ASCII-safe precondition but moves JVM/Native to a platform copy path that is friendlier to JIT and GC, and extends the same tagging to decoded base64 text when the decoded bytes are JSON-safe ASCII.

Benchmarks

Baseline is upstream/master at 8b67cb1e.

JMH, docs regression cases:

case upstream this PR delta
go_suite/base64.jsonnet 0.168 ms/op 0.109 ms/op 35.1% faster
go_suite/base64Decode.jsonnet 0.124 ms/op 0.076 ms/op 38.7% faster
go_suite/base64_byte_array.jsonnet 0.915 ms/op 0.782 ms/op 14.5% faster

Native CLI hyperfine against latest source-built jrsonnet master 5b43fa8 (jrsonnet 0.5.0-pre98):

case upstream this PR latest jrsonnet highlight
go_suite/base64.jsonnet 5.3 +/- 2.2 ms 4.8 +/- 0.4 ms 2.7 +/- 0.6 ms this PR narrows the gap from 1.94x to 1.77x slower; CLI timings are short/noisy

Validation

  • git diff --check
  • ./mill -i __.checkFormat
  • ./mill -i 'sjsonnet.jvm[3.3.7].test'
  • ./mill -i 'sjsonnet.js[3.3.7].test'
  • ./mill -i 'sjsonnet.native[3.3.7].test'
  • ./mill -i 'sjsonnet.jvm[2.12.21].compile' 'sjsonnet.jvm[2.13.18].compile' 'sjsonnet.native[2.13.18].compile' 'sjsonnet.js[2.13.18].compile'
  • ./mill -i 'sjsonnet.native[3.3.7].nativeLink'
  • ./mill -i bench.runRegressions bench/resources/go_suite/base64Decode.jsonnet bench/resources/go_suite/base64.jsonnet bench/resources/go_suite/base64_byte_array.jsonnet
  • hyperfine --warmup 10 --min-runs 50
  • Confirmed JDK 17 still exposes the deprecated String#getBytes(int, int, byte[], int) method used here.

References

  • Head: ba10f26a71bacd88ce0a476f9157a3f56c0f3c1d
  • Latest source-built jrsonnet: 5b43fa88b8c43856dd5a2daa9c5c251153c5e14d

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