perf: avoid Tuple2 allocation in OP_%, visitLookup, and OP_in dispatch#692
Merged
stephenamar-db merged 1 commit intodatabricks:masterfrom Apr 9, 2026
Merged
Conversation
He-Pin
commented
Apr 5, 2026
| ) | ||
| } | ||
|
|
||
| // Nested match avoids Tuple2 allocation from (visitExpr(...), visitExpr(...)) match |
Contributor
Author
There was a problem hiding this comment.
this is ok in Scala 3.8.x but not for older scala versions, so this is needed.
e3121ae to
5e9c296
Compare
Rewrite OP_%, visitLookup, and OP_in from tuple pattern match to nested instanceof match, eliminating Tuple2 allocation per binary operation. This follows the same pattern used by comparison operators. OP_+ is intentionally left unchanged as controlled benchmarks showed its more complex nested dispatch caused JIT regression on object-heavy workloads. Changes: - visitLookup: nested match with per-type fallback error messages - OP_%: nested match using case class extraction for raw doubles - OP_in: nested match for (Str, Obj) dispatch Upstream: jit branch commits 2530390, 671454c
5e9c296 to
b52b1f5
Compare
stephenamar-db
approved these changes
Apr 9, 2026
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
Scala's tuple pattern matching (
(visitExpr(a), visitExpr(b)) match { case (X, Y) => ... }) allocates aTuple2object on every dispatch. In hot evaluator paths likevisitLookup,OP_%,OP_in,OP_==, andOP_!=, this creates millions of short-lived allocations per evaluation. On JVM, the JIT often eliminates these via escape analysis, but on Scala Native, the allocation is real.Key Design Decision
Replace tuple-based pattern matching with nested
matchstatements:Modification
sjsonnet/src/sjsonnet/Evaluator.scala:visitLookup: Replaced tuple match on(value, index)with nested match. Checkslhstype first (Arr/Str/Obj), thenrhstype (Num/Str).OP_%: Replaced(l, r) matchwith nested match. Checks Num first (most common), then Str for format strings.OP_in: Replaced(l, r) matchwith nested match.OP_==/OP_!=: Replaced tuple match with nested match.All error messages preserved exactly.
Benchmark Results
JMH — Full Suite (35 benchmarks, @fork(1) @WarmUp(1) @measurement(1))
No regressions across all 35 benchmarks.
Analysis
References
Result
Zero-allocation operator dispatch via nested match. No regressions. Eliminates Tuple2 allocation in hot evaluator paths.