Files
aji/docs/design/control-depth-performance-audit.md
T

121 lines
6.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Control-depth performance audit (Q-063)
## Scope and reproduction (2026-10-14)
Q-063 has several independent causes. Repeated liveness queries dominate the
mutable-match case, textual incoming-edge searches dominate loop lowering,
and shield relations dominate verification. This profiling change does not
optimize production code or close Q-063.
The measured binary is a fresh Debug `zig build` at
`17cbe3eb4ea9cf7ec2d811613ebb1b9e3affb976`, Zig 0.17.0, SHA-256
`527dbce1cdd80d48ed3e5c14c6c6a814aa016e2421fb5e7ffa5260545e5b8216`.
Generated fixtures and raw evidence are in
`/tmp/aji-q063-profile-20261014-jeo0_cfr/`: `provenance.json`,
`timings.jsonl`, `summary.json`, generated `.aji`/`.air`, and five
`*.profile.json`/`*.gdb.log` pairs. These local artifacts are not versioned.
The harness is retained as [`tools/profile_control_depth.py`](../../tools/profile_control_depth.py):
```sh
zig build
python3 tools/profile_control_depth.py --depths 32 64 128 256 512 --repeats 3
```
The default maximum depth is 256 to keep reproduction practical. It generates
scope/shield nesting, `while false`, loops with a reachable `break` at every
level, and integer matches with one nested arm and one empty default arm.
The `*_live` variants assign `x = x + 1u64` at the leaf and return `x` after
the outer construct. Other variants have one immutable leaf binding and return
zero. These are generated public-language probes, unrelated to evaluation tasks.
Each fixture is lowered, its AIR checked, and its source checked, with three
repetitions per command: 315 commands, all exit 0 with empty diagnostics.
Repeated lowerings are byte-identical. The retained harness additionally passes
a 42-command smoke run at depths 2 and 4, one repetition, all seven shapes.
No runtime execution, fuel consumption or scheduler behavior is measured.
No wall-clock timeout terminates a command. Timings include process startup,
allocator cleanup, and command-specific I/O; the AIR check includes AIR parsing.
They are medians on one machine, not portable limits or exact complexity proofs.
## Stage timings
Seconds, shown as **128 / 256 / 512 levels**:
| Shape | Source lowering | AIR parsing + verification | Full source check |
|---|---|---|---|
| scope | 0.039 / 0.060 / 0.091 | 0.029 / 0.029 / 0.031 | 0.046 / 0.062 / 0.098 |
| shield | 0.041 / 0.069 / 0.092 | 0.093 / 0.363 / 1.991 | 0.109 / 0.412 / 2.034 |
| while | 0.086 / 0.208 / 0.628 | 0.036 / 0.064 / 0.164 | 0.104 / 0.248 / 0.749 |
| loop | 0.039 / 0.062 / 0.141 | 0.027 / 0.044 / 0.079 | 0.045 / 0.080 / 0.190 |
| match | 0.060 / 0.099 / 0.178 | 0.044 / 0.062 / 0.161 | 0.072 / 0.135 / 0.321 |
| while_live | 0.130 / 0.373 / 1.235 | 0.041 / 0.072 / 0.177 | 0.147 / 0.413 / 1.398 |
| match_live | 0.558 / 3.973 / 31.827 | 0.035 / 0.068 / 0.171 | 0.587 / 4.083 / 31.819 |
The mutable-match lowering grows by about 7.1× and 8.0× for successive depth
doublings, consistent with cubic work in this fixture. The no-mutation control
is much cheaper. Shield lowering stays close to scope lowering while checking
the generated AIR reproduces almost the whole source-check cost.
## Sampled stacks and source inspection
GDB launches each target as its child, interrupts at roughly 20 ms intervals,
records up to 41 frames and resumes until normal exit. All five targets and
their debuggers exit normally. These are inclusive stack-presence counts,
not additive CPU percentages; debugger overhead and the small samples limit
precision. Profiling is separate from the timing matrix.
| Target | Samples | Observed path |
|---|---|---|
| match_live, 256, lower | 195 | `liveAfter` in 186; `liveness.Workspace.query` in 188 |
| while, 512, lower | 30 | `finishDriveLoop` → `reaches` in 23 |
| while_live, 512, lower | 60 | `reaches` in 27; liveness query in 22 |
| loop, 512, lower | 6 | `reaches` in 3; too few for a precise proportion |
| shield, 512, check | 101 | `borrow.checkFunction` in 96; `provenance.State.kill` in 49 |
**Mutable matches:** `prepareDriveValueMatch` calls `carriedLive`, which calls
`liveAfter` for assigned mutable bindings. `liveAfter` walks enclosing rest
frames and searches their containing statement/expression with a fresh `After`
cursor. Nested matches therefore revisit ancestor subtrees at many levels.
The stacks and mutation control support this as the principal cause of the
measured cubic behavior. A cache keyed only by variable name would be invalid:
construct position, rest-frame context, rebinding/kills and loop reads matter.
**Loops:** `finishDriveLoop` calls `reaches` for every exit block. This helper
does not traverse a reachability graph: it searches the text of all emitted
instructions for a reference to the target block name. Repeating this for
nested exits produces quadratic worst-case scanning independently of liveness.
The stacks show it dominates the measured no-mutation while lowering. Mutable
while lowering additionally spends substantial time on liveness queries.
**Shields:** `borrow.step` records an `.inner` relation for each new shield
against every live outer shield: n(n−1)/2 facts at maximum depth. Both event
replay and the checking walk apply those relations. `provenance.State.kill`
scans the whole fact list when a shield ends. For simple nested shields, a
full pop sequence scans lists whose lengths sum to (n³−n)/6 per complete
application of that sequence (22,369,536 fact visits at n=512). This is a
source-derived count, not an instrumented measurement. Relation insertion,
hash-table growth, access checks and replay add cost. A lowerer liveness cache
cannot remove this verifier cost.
## Follow-up order
1. Replace repeated textual incoming-reference searches for loop exits with
explicit incoming-edge metadata. This is the smallest isolated candidate;
preserve the helper's actual reference-existence behavior, including
references from blocks whose execution is unreachable.
2. Design context-correct reuse or indexing for liveness searches, starting
with mutable-match ancestor rescans. Preserve pruned SSA sets, first-use
kills, rebinding barriers, diagnostics, host-OOM propagation and publication.
3. Separately optimize shield relation representation or removal. This changes
verifier implementation and needs review of exact shield-order joins,
resource handoff, E3002/E4305 priority and deterministic behavior.
Each optimization needs independent positive/negative coverage and exact
old/new lowering, diagnostics and observable-runtime comparisons appropriate
to its scope. The present work changes only a profiling tool and documentation:
no verifier/runtime rule, language feature, accepted-depth limit, diagnostic,
instruction, agent bundle or benchmark claim changes. Residual AIR verification
growth for while/match is visible but not assigned a specific cause by these
profiles. Q-063 remains open for implementation decisions and those remaining
measurements; Q-060's composite closure and the B1 gate on P7/P8 are independent.