perf: inline numeric fast path in array comparison#693
Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Open
perf: inline numeric fast path in array comparison#693He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
Inline Val.Num type check in the array comparison while loop to avoid polymorphic recursive compare() dispatch per element. For numeric array comparisons (e.g. 1M elements), this eliminates 1M recursive method calls with 5-branch pattern matching overhead. Uses asDouble (not raw extraction) to preserve NaN error behavior — std.log(-1) etc. can produce NaN in Val.Num, and asDouble correctly throws 'not a number', matching the official C++ jsonnet behavior. Uses java.lang.Double.compare() instead of compareTo() to avoid autoboxing overhead. Upstream: jit branch commit 62437d8
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
Array comparison in Jsonnet compares elements pairwise. When both elements are
Val.Num, the current code path goes through the generic comparison dispatch, adding virtual call and type-check overhead for the most common case.Key Design Decision
Add an inline fast path that directly compares
Val.Numvalues when both operands have the numeric type tag, avoiding the generic dispatch entirely.Modification
Val.Numtype check before generic comparison dispatchBenchmark Results
JMH (JVM, 3 iterations warmup + 3 measurement)
Analysis
The comparison2 benefit (-18.8%) is expected since that benchmark performs many array comparisons with numeric elements. This is a minimal-risk optimization (16 lines, zero semantic change) that adds a type check fast path.
References
Result
All 49 tests pass. All benchmarks positive, no regressions.