perf: comprehension fuse scope+eval and inline BinaryOp(ValidId,ValidId) fast path#686
Open
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Open
perf: comprehension fuse scope+eval and inline BinaryOp(ValidId,ValidId) fast path#686He-Pin wants to merge 1 commit intodatabricks:masterfrom
He-Pin wants to merge 1 commit intodatabricks:masterfrom
Conversation
Fuse comprehension scope building with body evaluation, eliminating intermediate scope array allocation. For nested comprehensions like [x+y for x in arr for y in arr if x==y], this avoids allocating O(n²) intermediate scopes — only the O(n) matching results are materialized. When the innermost body is BinaryOp(ValidId,ValidId), inline scope lookups and numeric binary-op dispatch to avoid 3× visitExpr overhead per iteration. Falls back to general visitExpr for non-numeric types. Key changes: - visitCompFused: recursive fused scope+eval loop with ArrayBuilder - evalBinaryOpNumNum: @switch-dispatched Num×Num fast path - Non-numeric fallback uses existing visitExpr (no code duplication) Upstream: jit branch commits 3466461 (fuse) + 71545ba (inline)
9b5caef to
62c6ef6
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 comprehensions like
[x+y for x in arr for y in arr if x==y]previously collected all valid scopes into an intermediateArray[ValScope], then mapped body evaluation over them. For nested comprehensions, this allocates O(n²) intermediate scopes even when only O(n) results survive filtering. Additionally, the body evaluation dispatches throughvisitExprfor every element, which has significant overhead for simple expressions likeBinaryOp(ValidId, ValidId).Key Design Decisions
Fused scope+eval: Instead of two passes (collect scopes → map body),
visitCompFuseddirectly appends body results during scope traversal, eliminating intermediate array allocation.BinaryOp(ValidId,ValidId) fast path: For the innermost ForSpec with a binary-op body of two variable references, inline scope lookups and numeric dispatch to avoid 3×
visitExproverhead per iteration. Falls back to generalvisitExprfor non-numeric types — no code duplication.Eager evaluation: The fast path evaluates eagerly (not lazy). Both go-jsonnet and jrsonnet evaluate comprehensions eagerly, and eagerness is required for safe mutable scope reuse.
Lean implementation: Unlike the original version, this eliminates the
visitBinaryOpValuesfallback method (~60 lines), reducing code addition from ~225 to ~200 lines. This avoids native binary size inflation that caused instruction cache regression.Modification
sjsonnet/src/sjsonnet/Evaluator.scala:visitComp(Comp)with fused version usingArrayBuildervisitCompFused— recursive fused scope+eval loopevalBinaryOpNumNum—@switch-dispatched Num×Num fast path covering arithmetic, comparison, bitwise, and shift opsOP_in,OP_&&,OP_||from numeric fast path (these need type dispatch / short-circuit semantics)OP_+,isInfinitecheck forOP_-,OP_*,OP_/sjsonnet/test/resources/new_test_suite/comprehension_binop_types.jsonnet:inoperatorBenchmark Results
JMH (35 benchmarks, 0 regressions)
No significant regressions across all 35 benchmarks.
Scala Native Hyperfine (vs jrsonnet 0.4.2)
¹ realistic1 wall time increase is startup/binary-loading overhead, not computation — user time is unchanged (10.3 vs 10.5 ms)
² realistic2 +2% is within noise range
Analysis
The 50% improvement on comparison2 comes from two complementary optimizations:
[x+y for x in arr for y in arr if x==y]with n=5000 — contributes ~6%if x==yfilter, inline scope lookups +@switchnumeric dispatch avoids 3× visitExpr overhead — contributes ~44%The lean implementation avoids the instruction cache regression seen with the original version (which added ~225 lines including a duplicated
visitBinaryOpValuesmethod), keeping realistic1 user time flat.References
3466461a(fuse scope+eval) +71545ba8(inline BinaryOp)Result
Array comprehension evaluation is 2-3× faster for comprehension-heavy workloads, with no regressions on other benchmarks. sjsonnet native now beats jrsonnet by 3.09× on comparison2.