perf: inline numeric fast path in array comparison#693
Closed
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Closed
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
stephenamar-db
approved these changes
Apr 8, 2026
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
a77e81e to
d31b232
Compare
stephenamar-db
approved these changes
Apr 9, 2026
Collaborator
|
This PR now has a merge conflict after recent merges to master. Tests and formatting pass otherwise. Please rebase on master and force-push to resolve the conflict. |
Contributor
Author
|
Closing: this optimization (inline numeric fast path in array comparison) is now included in the merged PR #691 which combined nested-match dispatch with the inline numeric fast path. |
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 (
<,<=,>,>=) dispatches each element through a polymorphiccompare()method that handles 5 value types via pattern matching. For numeric arrays (the most common case in Jsonnet comparisons), this polymorphic dispatch adds overhead from type checks across all branches.Key Design Decision
Inline a numeric fast path directly in the array comparison loop: check if both elements are
Val.Numfirst, and compare theirasDoublevalues directly. Only fall through to the genericcompare()for non-numeric elements.Modification
sjsonnet/src/sjsonnet/Evaluator.scala— array comparison incompare():Val.Numtype check before the genericcompare()calljava.lang.Double.comparefor numeric elementsBenchmark Results
JMH — Isolated Targeted (JVM, 5 runs each, median)
Stability Improvement
The baseline shows occasional GC spikes (29–32ms) on
comparison, while PR #693 runs are consistently stable (19.8–20.5ms). The monomorphic fast path produces more predictable JIT compilation.JMH — Full Suite (35 benchmarks, 1+1 warmup)
No regressions detected. All benchmarks within noise margin.
Note
On the JVM, the JIT compiler eventually specializes polymorphic call sites through profile-guided optimization. The inline fast path helps by:
compare()handles 5 types — the JIT may keep the call site bimorphic/megamorphic. The inline check makes the numeric path monomorphic.Analysis
java.lang.Double.compareis the same comparison used insidecompare()forVal.Num. Semantics identical.isInstanceOf[Val.Num]check per element — negligible compared to the saved polymorphic dispatch.References
Evaluator.compare()handles:Num,Str,Bool,Null,Arr(recursive)java.lang.Double.comparefor IEEE 754 total orderingResult
Inline numeric fast path in array comparison. ~1% improvement on JVM with improved stability. Primarily benefits Scala Native via direct dispatch elimination.