perf: compact compare() method for better JIT inlining#725
Merged
stephenamar-db merged 1 commit intodatabricks:masterfrom Apr 10, 2026
Merged
perf: compact compare() method for better JIT inlining#725stephenamar-db merged 1 commit intodatabricks:masterfrom
stephenamar-db merged 1 commit intodatabricks:masterfrom
Conversation
Refactor compare() from nested match to tuple match with extracted error helper. Scala 2.13+ pattern matcher lowers tuple patterns to direct instanceof/checkcast without Tuple2 allocation (verified via javap). The compact method body improves JIT inlining decisions, especially in full-suite scenarios where many hot methods compete for inlining budget. Key changes: - Tuple match for compact bytecode (no Tuple2 at bytecode level) - Extract compareTypeMismatch() to keep happy path small - Reorder cases: Num/Str/Arr first (most common in benchmarks) - Preserve post-force reference equality and numeric fast path in array loop
Contributor
Author
|
@stephenamar-db This works better on 2.13.x and the tuple2 allocation is been fixed in newer scala 3 release too @noti0na1 |
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
The
compare()method was changed to nested match in #691 to avoid Tuple2 allocation. However, this made the method body significantly larger, reducing JIT inlining effectiveness — especially in full-suite benchmark scenarios where many hot methods compete for the JVM inlining budget.Key Design Decision
Revert to tuple match for the outer dispatch. Verified via
javapthat Scala 2.13+ pattern matcher lowers(x, y) match { case (X, Y) => ... }to directinstanceof/checkcastwithout anyTuple2allocation at bytecode level. This gives us the same zero-allocation benefit as nested match, but with a much more compact method body that the JIT can inline and optimize better.Modification
compare()dispatch uses(x, y) matchpattern (no Tuple2 at bytecode level, verified)compareTypeMismatch()helper — moves error path out of the hot method to reduce bytecode sizeBenchmark Results
JMH Full-Suite Results (Apple M4, JDK 24, @fork(1) @WarmUp(1) @measurement(1))
Key benchmarks showing improvement:
No regressions observed across the full suite (35 benchmarks).
Full JMH Results (After)
Analysis
The improvement is primarily from reduced JIT profile pollution. When the full benchmark suite runs, HotSpot must compile and optimize many different methods. A smaller
compare()method body gives the JIT compiler more flexibility to inline and optimize it, even after many other methods have been compiled.The improvement is most visible in:
compare()or related array operations frequentlyEvaluatorBytecode verification confirms zero Tuple2 allocation:
References
Result
./mill sjsonnet.jvm[3.3.7].test)./mill sjsonnet.jvm[3.3.7].checkFormat)