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
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
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 concatenating a number with a string (
num + strorstr + num), the evaluator callsMaterializer.stringify()which performs a full type-dispatch pattern match on theValjust to extract the double for rendering. ForVal.Num, this dispatch is unnecessary — we can callRenderUtils.renderDouble()directly.This is particularly impactful for string template operations that concatenate many numbers with strings, such as building JSON-like output via string interpolation.
Key Design Decision
Add specific match cases for
(Val.Num, Val.Str)and(Val.Str, Val.Num)in theOP_+dispatch that callRenderUtils.renderDouble(n.asDouble)directly instead of going throughMaterializer.stringify().Uses
n.asDouble(not the raw destructured double from pattern match) to preserve the NaN guard inVal.Num.asDouble— ensuring identical error behavior toMaterializer.stringify()for edge cases like(0 % 0) + "text".Modification
Evaluator.scala— Added 2 lines inOP_+dispatch:Placed between
(Str, Str)and generic(Str, any)cases to match numbers before the more expensiveMaterializer.stringify()fallback.Benchmark Results
JMH (35 benchmarks, single-threaded)
Zero regressions across all 35 benchmarks.
Native (hyperfine, 10 runs) 🔥
Analysis
large_string_template— the gap with jrsonnet narrowed from 3.20x to 2.12xn.asDoubleadds negligible overhead (oneisNaNcheck on a hot-path double) while preserving correctnessReferences
4b1cd032(OP_+ Num stringify fast-path)Result
All 140 tests pass. Zero regressions. NaN error semantics preserved via
asDoubleguard.