Skip to content

perf(check): ctx_dead searches the scope in place instead of copying it first - #1965

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
sebastianbreguel:perf/ctx-dead-in-place
Nov 2, 2026
Merged

nicolas-abril merged 1 commit into
bendlang:mainfrom
sebastianbreguel:perf/ctx-dead-in-place

Conversation

@sebastianbreguel

@sebastianbreguel sebastianbreguel commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1955.

ctx_dead copied the whole scope into an array with pmap_to_array before it looked at the first variable. A scope with n local empty matches (\{}) calls it n times, so the copies grew as O(n² log n) while the search itself stopped at the first variable.

Change (bend2/bend.ts only): pmap_any walks the tree in place, next to pmap_to_array, and ctx_dead uses it. It visits the same nodes in the same order (value, then left, then right) and stops at the same first empty type, so term_wnf and book_adt run on the same variables in the same order. No cache and no memo: the search only skips the copy (#993, #1075).

Measured on the issue's program with n x = {\{} : Unit -> Quant} lines, --check-only:

n entries copied in ctx_dead, before after wall time, before after
16 393 0 20 ms 20 ms
1024 4,383,927 0 66 ms 36 ms
4096 not counted 0 820 ms 160 ms
8192 not counted 0 3,124 ms 530 ms

Copies were counted with a temporary counter, since removed. Times are medians of 7 runs, before and after alternated.

None of the checker benches calls ctx_dead, and their medians of 5 alternated runs stay within noise: compute_1600 508 → 505 ms, defs_12800 297 → 297, generics_3200 382 → 384, proofs_3200 823 → 833, trees_400 803 → 798. So this only matters for scopes with many empty matches.

On Linux, medians of 7: n = 1024 77 → 43 ms, 4096 982 → 199 ms, 8192 4,042 → 609 ms; checker benches compute_1600 609 → 613, defs_12800 361 → 368, generics_3200 458 → 455, proofs_3200 922 → 929, trees_400 1142 → 1140.

Verification (macOS arm64, Bun 1.4.2; Linux arm64 (Docker, Debian 13, Bun 1.4.2, clang 19))

  • Copies in ctx_dead, n = 1 to 1024, macOS: the table above
  • Wall time, issue program n = 16 to 8192, and the 5 checker benches, macOS and Linux: above
  • All 1796 tests under tests/, interpreter, before vs after, macOS and Linux: no difference (on macOS proof/app/main, which greets until killed, is not comparable; on Linux its output is capped at 1 MB and matches)
  • gates/repo.ts: 54/54 (bend.ts 44,566 / 48,000 ttok)
  • tsc -p bend2/pack/tsconfig.json: the same 7 errors before and after, all already on main
  • gates/test.ts, gates/perf.ts: run on the minis
  • Metal, CUDA: not run; the change is in the checker

@YidaWeng

YidaWeng commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Verified against current main (00d29f44): this merges cleanly with #1967 and is a strict improvement for ctx_dead. No correctness concerns found.

Sequencing: #1966's author recommends landing this before #1966, and I agree. Suggest #1965 -> #1967 -> rebase/reduce #1966.

nicolas-abril added a commit that referenced this pull request Nov 2, 2026
Level order changed which @unsafe binder type a program hangs on, and the
batched var columns saved about 0.02 s while dropping the error that guarded
them. #1965 fixes #1955 and #1967 fixes the call spine and body_flatten, so
this is now the fields #1956 still names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril
nicolas-abril merged commit 394d3ed into bendlang:main Nov 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[complexity, checker] O(n^2 log n) — An empty match copies all variables before inspecting the first

3 participants