perf: extend comprehension scope reuse to builtins, select, lookup#783
Merged
stephenamar-db merged 1 commit intodatabricks:masterfrom Apr 21, 2026
Merged
Conversation
Motivation: The mutable scope reuse path in comprehension evaluation avoids O(n) ValScope allocations (Arrays.copyOf per iteration), but was gated by isNonCapturingBody which only covered ValidId, BinaryOp, UnaryOp, And, Or, IfElse, and Literal. Common patterns like [std.length(x) for x in arr] or [x.field for x in arr] fell through to the per-iteration scope allocation path. Modification: Extend isNonCapturingBody to cover ApplyBuiltin0-4, Select, and Lookup with recursive argument checks. These produce eager values via visitExpr and don't create closures that capture the comprehension scope. The same expression types are already handled by isInvariantExpr. Result: More comprehension patterns benefit from mutable scope reuse, eliminating per-iteration ValScope allocation + Arrays.copyOf overhead.
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.
Summary
isNonCapturingBodyto recognize more expression types that produce eager values without capturing the comprehensionValScope[std.length(x) for x in arr]or[x.field for x in arr], avoiding per-iterationValScopeallocation viascope.extendSimple()+Arrays.copyOfNew expression types covered
ApplyBuiltin0ApplyBuiltin1isNonCapturingBody(a1)ApplyBuiltin2ApplyBuiltin3ApplyBuiltin4SelectisNonCapturingBody(value)LookupSafety argument
The mutable scope reuse path evaluates the body with
visitExpr(eager). The returnedValdoesn't capture the comprehension scope. InternalvisitAsLazycalls within builtins receive non-capturing sub-expressions, sovisitAsLazyreturns eagerly viatryEagerEvalor direct binding lookup — noLazyExprcreation, no stale scope capture.NOT extended to:
Apply0-3(user-defined function calls may create closures that capture scope),Function(createsVal.Funccapturing scope),ObjBody.MemberList(createsVal.Objwith lazy fields capturing scope).Test plan
./mill 'sjsonnet.jvm[3.3.7]'.test— all tests pass./mill 'sjsonnet.jvm[3.3.7]'.compile— compiles cleanisInvariantExpr(line 478+) already covers the same expression types, confirming this pattern is established