Commit c547cf8
authored
perf: extend comprehension scope reuse to builtins, select, lookup (#783)
## Summary
- Extend `isNonCapturingBody` to recognize more expression types that
produce eager values without capturing the comprehension `ValScope`
- This allows the mutable scope reuse fast path to activate for common
comprehension patterns like `[std.length(x) for x in arr]` or `[x.field
for x in arr]`, avoiding per-iteration `ValScope` allocation via
`scope.extendSimple()` + `Arrays.copyOf`
## New expression types covered
| Expression | Check |
|------------|-------|
| `ApplyBuiltin0` | always true (no args) |
| `ApplyBuiltin1` | `isNonCapturingBody(a1)` |
| `ApplyBuiltin2` | both args non-capturing |
| `ApplyBuiltin3` | all 3 args non-capturing |
| `ApplyBuiltin4` | all 4 args non-capturing |
| `Select` | `isNonCapturingBody(value)` |
| `Lookup` | both value and index non-capturing |
## Safety argument
The mutable scope reuse path evaluates the body with `visitExpr`
(eager). The returned `Val` doesn't capture the comprehension scope.
Internal `visitAsLazy` calls within builtins receive non-capturing
sub-expressions, so `visitAsLazy` returns eagerly via `tryEagerEval` or
direct binding lookup — no `LazyExpr` creation, no stale scope capture.
**NOT extended to:** `Apply0-3` (user-defined function calls may create
closures that capture scope), `Function` (creates `Val.Func` capturing
scope), `ObjBody.MemberList` (creates `Val.Obj` with lazy fields
capturing scope).
## Test plan
- [x] `./mill 'sjsonnet.jvm[3.3.7]'.test` — all tests pass
- [x] `./mill 'sjsonnet.jvm[3.3.7]'.compile` — compiles clean
- [x] `isInvariantExpr` (line 478+) already covers the same expression
types, confirming this pattern is established1 parent 9c6debb commit c547cf8
1 file changed
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
460 | 460 | | |
461 | 461 | | |
462 | 462 | | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
463 | 481 | | |
464 | 482 | | |
465 | 483 | | |
| |||
0 commit comments