perf: array comparison reference equality + numeric fast path#674
Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Open
perf: array comparison reference equality + numeric fast path#674He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
Add reference equality check (xi eq yi) in the array comparison loop before type dispatch. When arrays share elements (e.g., long_array + [1] < long_array + [2] where long_array has 1M shared elements), this skips type dispatch for all identical references. Also inline numeric fast path (double compare without polymorphic compare() dispatch) for the common case where both elements are Num. Upstream: jit branch commits f7ad961, 62437d8
fa49ccf to
b9d479c
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
Array comparisons always compare element-by-element, even when two arrays are the same object reference. Adding a reference equality short-circuit and numeric fast path reduces unnecessary comparison work.
Key Design Decision
Check reference equality first (if two arrays are the same object, they are equal). For element comparison, add a fast path for numeric values that avoids the generic comparison dispatch.
Modification
Added reference equality check in array comparison code in Evaluator.scala. Added numeric fast path for element-wise comparisons.
Benchmark Results
JMH Regression Suite (1 fork, 3 warmup, 1 measurement)
All other benchmarks within noise margin.
Scala Native Hyperfine (
-N -w4 -m20)Note: jrsonnet fails on the comparison.jsonnet benchmark (non-zero exit). Native benchmarks compare sjsonnet master vs this PR only.
Analysis
The 5% improvement is consistent and statistically significant on the comparison benchmark. The optimization is most effective when the same array objects are compared or when arrays contain mostly numeric values.
References
Upstream jit branch exploration at he-pin/sjsonnet@jit
Result
Consistent 5% improvement on array comparison operations.