perf: fast-path Num stringify in OP_+ string concatenation#684
Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Open
perf: fast-path Num stringify in OP_+ string concatenation#684He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
Collaborator
|
CI check found a scalafmt formatting violation in |
stephenamar-db
approved these changes
Apr 8, 2026
132d361 to
cca73c5
Compare
Collaborator
|
Formatting check is still failing on |
cca73c5 to
554f7c7
Compare
Contributor
Author
|
Fixed — formatting has been corrected after the rebase. |
Add direct RenderUtils.renderDouble() call for Num+Str and Str+Num cases in binary OP_+ to avoid Materializer.stringify() dispatch overhead. stringify() performs a full pattern match on Val type just to extract the double for rendering. The direct call skips this dispatch entirely, which is significant for string template operations that concatenate many numbers with strings. Uses n.asDouble (not raw destructured double) to preserve the NaN guard that exists in Val.Num.asDouble — this ensures consistency with Materializer.stringify() error behavior for not-a-number values. Upstream: jit branch commit 4b1cd03
554f7c7 to
075fa18
Compare
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
When Jsonnet concatenates a number with a string via
+, the evaluator goes throughMaterializer.stringify()which handles allValtypes via pattern matching. For the common case ofNum + StrorStr + Num, this generic dispatch adds unnecessary overhead — we already know the value is aVal.Numfrom the enclosing match.Key Design Decision
Add specialized
(Val.Num, Val.Str)and(Val.Str, Val.Num)match cases before the generic(Val.Str, r)/(l, Val.Str)fallbacks. These cases callRenderUtils.renderDouble(n.asDouble)directly, bypassingMaterializer.stringify's type dispatch.Modification
sjsonnet/src/sjsonnet/Evaluator.scala—OP_+handler:(l, r) matchblock:(Val.Num, Val.Num)and(Val.Str, Val.Str)but before the generic stringify fallbacks.Benchmark Results
JMH — Isolated Targeted (JVM, 5 runs each, median)
JMH — Full Suite (35 benchmarks, 1+1 warmup)
No regressions detected. All benchmarks within noise margin.
Note
On the JVM, the JIT compiler inlines
Materializer.stringifythrough profile-guided optimization, reducing the benefit of this explicit fast path. The primary value is:Num + Strpath is significant.Num + Strconcatenation path explicit in the source.Analysis
RenderUtils.renderDoubleis the same function called byMaterializer.stringifyforVal.Num— semantics are identical.(Num, Num)and(Str, Str)cases, so they don't add overhead to those hot paths.References
Materializer.stringify→RenderUtils.renderDoubleforVal.NumRenderUtils.renderDoublehandles integer-valued doubles (e.g.,42.0→"42")Result
Type-specialized fast path for number-string concatenation. No regressions. Benefits Scala Native and improves code explicitness.