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
Comprehension operations (array/object comprehensions) are the most performance-critical loops in Jsonnet evaluation. Every iteration currently involves:
ValScopefor each iteration to bind the loop variablevisitExprdispatch for the body, even when the body is a simple binary operation on two local variablesFor workloads like
comparison2(which runs millions of comprehension iterations with simple comparison bodies), these overheads dominate execution time.Key Design Decision
Two complementary optimizations target the comprehension inner loop:
Scope+Eval Fusion: Instead of first building a scope (
extendBy) and then evaluating the body as separate steps, fuse them into a single operation. This eliminates one intermediate method call and allows the optimizer to keep variables in registers.Inline BinaryOp(ValidId, ValidId) Fast Path: When the comprehension body is a binary operation on two local variables (e.g.,
x > y,a + b), bypassvisitExprentirely and directly:This eliminates all expression dispatch overhead for the most common comprehension pattern.
Modification
Evaluator.scala: AddedvisitCompInlinemethod with pattern matching on body expression:BinaryOp(ValidId(lhsIdx), ValidId(rhsIdx), op)→ direct scope read + op dispatchvisitExprfor other body patternsTest: Added
comprehension_binop_types.jsonnetcovering:+,-,*,/,%<,>,<=,>=,==,!=&&,||+on stringsBenchmark Results
JMH (JVM, 3 iterations)
Hyperfine (Scala Native, 10 runs, vs master)
Hyperfine (Scala Native, vs jrsonnet)
Analysis
References
he-pin/sjsonnetjit branch commits71545ba8,230ae9d1Result
Massive performance improvement for comprehension-heavy workloads with simple bodies (comparisons, arithmetic). comparison2 goes from 2.14x slower to 2.54x faster than jrsonnet.