perf: propagate ASCII-safety through Format outputs#860
Draft
He-Pin wants to merge 1 commit into
Draft
Conversation
Motivation: After PR databricks#858 added the join-presized + asciiSafe optimization, format outputs (`%`-interpolation, `std.format`) still always created Val.Str with `_asciiSafe = false`. Downstream JSON rendering of format results falls back to the per-char escape scan + UTF-8 encode path even when both the format string and all interpolated values are pure ASCII. Manifest workloads heavy on `%(name)s` interpolation pay this cost on every emitted string. Modification: - Add `literalsAsciiSafe` to RuntimeFormat, computed once at parse time by scanning leading + inter-spec literal segments for printable ASCII with no `"` or `\`. - At format time, AND `literalsAsciiSafe` with each interpolated value's ASCII-safety: strings forward `_asciiSafe`; numerics are ASCII (except `%c` which depends on codepoint); booleans/null are ASCII; complex types (Arr/Obj routed through Renderer) are conservatively non-ASCII. - Refactor `Format.format` (both overloads) and `formatSimpleNamedString` to return `Val.Str` directly so the `_asciiSafe` flag is set at construction. Update the three external callers (Evaluator binop `%`, std.mod, std.format) and `PartialApplyFmt.evalRhs` accordingly. Result: Format outputs now correctly carry `_asciiSafe = true` when all inputs are ASCII-safe, letting ByteRenderer take the fast path during JSON manifestation. Regression test `new_test_suite/format_asciisafe_propagation.jsonnet` covers the simple `%(name)s` fast path, general `%s`/`%d`/`%c`/`%x`/`%o`/`%f` conversions, mixed ASCII/non-ASCII literals and values, and ByteRenderer roundtrip via `std.manifestJson`.
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Format.format(the engine behind%-interpolation,std.format, and themodoperator's string fallback) always returned aVal.Strconstructed via the defaultVal.Str(pos, s)factory, which leaves_asciiSafe = false. This forcedByteRendereronto the slow per-char escape-scan + UTF-8 encode path when format outputs flowed into JSON rendering — even when both the format string literals and every interpolated value were pure ASCII.Manifest workloads heavy on
%(name)s-style templates (Helm/Kubernetes-flavored configs) emit many such ASCII-safe strings that go on to be rendered as JSON, so the cost compounds. This is the second of two main sources of "ASCII-safe-but-flagged-unsafe" strings in those workloads (the first,std.join, is companion PR #858).Modification
sjsonnet/src/sjsonnet/Format.scala:RuntimeFormat.literalsAsciiSafe— new field, computed once at parse time by scanning the leading literal + every inter-spec literal segment viaPlatform.isAsciiJsonSafe. Cached alongside the parsed format, so each format string pays the literal-scan cost exactly once and amortizes across every use of that cachedRuntimeFormat.simpleStringValueAsciiSafe(rawVal)for%(name)ssimple-named-string paths.specOutputAsciiSafe(rawVal, conversion)for the general path: strings forward_asciiSafe; numerics/booleans/null are ASCII (numerics under%cdepend on the codepoint range);Val.Arr/Val.Obj(rendered viaRenderer) are conservatively treated as non-ASCII.Format.formatreturnsVal.Str— both the string-input and pre-parsed-chunks overloads, plusformatSimpleNamedString. The_asciiSafeflag is set at construction viaVal.Str.asciiSafe(pos, s)when literals + all spec outputs are ASCII-safe; otherwise the regularVal.Str(pos, s)constructor is used.Val.Str(pos, ...)wrapper:Evaluator: the%binary operatorMathModule:std.modstring fallbackStringModule:std.formatFormat.PartialApplyFmt: static-folded format closuresjsonnet/test/resources/new_test_suite/format_asciisafe_propagation.jsonnet— regression test covering simple%(name)sfast path, general%s/%d/%x/%o/%c/%.2fconversions, mixed ASCII literals + non-ASCII string values, and astd.manifestJsonroundtrip exercising the ByteRenderer fast-path.Format-time overhead is two boolean ANDs per spec; literal scanning happens once at parse time.
Result
Benchmarked on Apple Silicon, Zulu JDK 21.0.10,
-Xmx4G -XX:+UseG1GC -Xss100m, 3 forks × (3 warmup + 5 measurement) iterations.JMH
bench.runRegressions(averaged over 3 forks, ms/op, lower is better):cpp_suite/large_string_templatejdk17_suite/repeat_formatgo_suite/manifestJsonExJMH
large_string_templatemean is dominated by thermal/GC outliers on Apple Silicon (note Fork 2's last two iterations spiked to 0.857 / 1.481 ms while Forks 1 & 3 ran cleanly around 0.683 ms). The per-fork minimums and the cleanest fork consistently show the PR ahead. Confirmed via hyperfine.hyperfine (30 runs, 5 warmup, full-binary including JVM startup, ms, lower is better):
large_string_templaterepeat_formatmanifestJsonExHyperfine on
manifestJsonExis dominated by JVM startup; JMH (which excludes startup) is the trustworthy signal there and shows ~30%.PR-side variance on
large_string_templateis dramatically tighter (±2.6 ms vs master ±79.6 ms), consistent with eliminating a noisy escape-scan path.References
std.joinpresize + asciiSafe propagation — same idea, applied to join outputs)/tmp/bench-mmrr/master.log,/tmp/bench-mmrr/pr860.log,/tmp/bench-mmrr/hyperfine-*.md(local artifacts)Test plan
new_test_suite/format_asciisafe_propagation.jsonnetcovers:%(name)sfast path with ASCII / non-ASCII literals + values%s/%d/%x/%o/%c/%.2fconversionsstd.manifestJsonroundtrip./mill 'sjsonnet.jvm[3.3.7]'.test— 46 suites pass./mill 'sjsonnet.native[3.3.7]'.compile— passes./mill 'sjsonnet.js[3.3.7]'.compile— passes./mill __.checkFormat— passes