Skip to content

perf: skip escape scan for AsciiSafeStr in the char renderer#878

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/ascii-safe-render
Open

perf: skip escape scan for AsciiSafeStr in the char renderer#878
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:perf/ascii-safe-render

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

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

Motivation

The char render 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 0x200x7e, no "/\). The Native ByteRenderer already had this bypass; the char path did not.

Modification

  • Add BaseCharRenderer.visitAsciiSafeString: " + bulk getChars + ", correct even under escapeUnicode since every char is <= 0x7e.
  • Route Val.AsciiSafeStr through it via a Materializer.visitStr helper at the three value-string sites; the ujson.Value AST path falls back to visitString.

Result

  • JMH render-only (string-heavy, 335 KB output): 1.606 → 1.441 ms/op (+10%).
  • Scala Native, isolated + interleaved vs master: manifestJsonEx total_time 30.6 → 29.5 ms (~+3.6%); kube-prometheus materialize_time neutral (no regression). Output byte-identical.

Test plan

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

@He-Pin He-Pin marked this pull request as draft May 30, 2026 12:16
The char render 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.

- 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; the ujson.Value AST path falls back to visitString.

Result:
- JMH render-only (string-heavy, 335 KB): 1.606 -> 1.441 ms/op (+10%).
- Scala Native, isolated and interleaved: manifestJsonEx total_time 30.6 -> 29.5 ms
  (~+3.6%); kube-prometheus materialize_time neutral. Output byte-identical.
@He-Pin He-Pin force-pushed the perf/ascii-safe-render branch from 0aa826f to a538958 Compare May 30, 2026 12:42
@He-Pin He-Pin marked this pull request as ready for review May 30, 2026 12:51
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