Skip to content

safe.ts and comp.ts stop reducing types they never read or already reduced, and safe_pass dedups book.order in one pass - #1362

Merged
Lorenzobattistela merged 4 commits into
bendlang:mainfrom
nicolas-abril:verdict-skip-dead-wnf
Oct 6, 2026

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

--verdict's elaborator spent most of its time on big books in two places it didn't need to. The compiler reduced a computed type once per annotation that mentions it, instead of once per def signature.

safe.ts

  • Order dedup: safe_pass found each root with book.order.lastIndexOf(k) === i, which is O(n²). On the checker benches book.order holds 42–54k entries, and this one line was 55–80% of elaboration. It now builds one Map of last indices.
  • Unused whnfs: reductions whose result is never read, which can re-run a proof by computation bend2 already checked:
    • arg_term and tree skip all_of for a term bend2 never types as a function (NOT_FN: types, quantities, constructors, literals, {==}).
    • ctr_term skips the goal's whnf for a family with no parameters.
    • root_cols no longer reduces the return type past the last parameter.
    • arm builds its template map only for a def with ~ parameters.

comp.ts

  • term_spine skips tele_unbind on a node with no applications. It was reducing a leaf's annotation type it never reads.
  • A def's signature is reduced once, in its telescope. term_wnf has no per-object result cache (sharing lives in argument cells, built fresh per call), so every fresh copy of T(ev(…)) was reduced again.
    • tele_unbind, already memoized per def in FL.teles, now stores each domain reduced.
    • fun_of takes a fully applied def's return type from it.
    • file_book reads the datatypes of a def's own λs and body, and of a call's head, off that reduced telescope (tele_adts), instead of reducing each annotation's copy of the signature. They come out in the order type_adts walks a ∀ (domains, then return type), so constructor ids keep their numbering.
    • Other annotations, such as an App's or a Ctr's per-site type, are scanned as before.
  • An earlier revision of this PR memoized ty_wnf by structural key instead. That was unsound: @Lorenzobattistela's tests/reg/wnf_memo_bound_key.bend shows a bound X sharing a key with a free X, so a count was lost and memory freed early. The memo is gone; the test stays as a regression guard and passes natively, in JS, and in the interpreter.

Timings (median of 3, Apple M4):

--check-only -o .bendtt main → branch --verdict main → branch
defs_12800 0.32 s 2.78 → 0.61 s 7.09 → 5.05 s
generics_3200 0.40 s 4.38 → 0.85 s 7.82 → 4.24 s
proofs_3200 0.84 s 3.65 → 1.59 s 9.04 → 6.98 s
app_win_is_bug_2d/PROOF 0.30 s 0.71 → 0.31 s 1.24 → 0.82 s

Most of what's left in --verdict on the benches is the kernel: Book.index is a linear scan, run once per def and once per live call. That fix needs the proofs updated and will come as a separate PR.

Where comp.ts speeds up: nothing in the corpus has a computed type in live code, so its compile times don't move (.c 98.6 → 98.4 s, .js 91.5 → 91.4 s over the whole corpus). On a program built for it, live p(x: Nat) -> T(ev(200000n)) and q(x: Nat, u: T(ev(200000n))) called from main, with a 0.35 s check, -o .c goes 1.55 → 0.84 s and -o .js 1.59 → 0.75 s. In safe.ts, the same kind of return type takes -o .bendtt from 0.78 → 0.36 s.

Checked:

  • Over all .bend files in tests/, demos/ and bench/, the output is byte-identical to main's: -o .bendtt for all 1,154 files that emit it, -o .c for all 762 that build, and -o .js for all 786.
  • tsc reports no new errors.
  • Merged onto main 7ed3424 (repo, test and ttok on f9012d5; the rest on a90ef7d, since f9012d5 only drops comments):
    • The output is still byte-identical to main's for every file that builds on both: 1,152 .bendtt, 764 .c and 788 .js. The new test is the only extra file.
    • repo 50 / 50, ping 47 / 47, test 1576 / 1576.
    • safe (tests): 1017 agree, 74 u, 491 bend2 rejects, 4 out of scope, 1 false reject (agree_32, also on main). Main has 1016 agree; the new test is the extra one.
    • perf 123 / 124: raytrace's COMPILER cell is 1.16x. Main misses one cell in the same session too (mandelbrot GPU 1.16x), with raytrace's COMPILER at 1.14x.
    • safe.ts 22,019 → 22,117 ttok, comp.ts 63,553 → 63,838.

🤖 Generated with Claude Code

nicolas-abril and others added 2 commits October 6, 2026 14:52
…s dedups book.order in one pass

- safe_pass found each root with book.order.lastIndexOf, O(n²) on a
  book's ~50k order entries: now one Map of last indices.
- safe.ts skips a whnf whose result is never used: arg_term and tree at
  a term bend2 never types as a function (NOT_FN), ctr_term for a family
  with no parameters, root_cols past the last parameter, and arm's
  template map for a def with no ~ parameters.
- comp.ts: term_spine skips tele_unbind on a node with no applications,
  and ty_wnf memoizes a call-headed type by its structural key, so a
  computed type in live code is reduced once, not once per annotation.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… so a bound X never shares a key with a free X

ty_wnf keyed a call-headed type by term_lower at depth 0, so a binder at
depth i keyed the same as a free variable at level or probe i with its
name: pick(X => b => X) and pick(X => b => Y) at Y := X shared one
whnf. A Data let in a generic def then read U32, a word, kept no count
for the boxed value it shared, and the native run read freed cells.
tests/reg/wnf_memo_bound_key.bend is the witness.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@Lorenzobattistela Lorenzobattistela self-assigned this Oct 6, 2026
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

@nicolas-abril I pushed ffd5bc3 to this branch: a fix for a key collision in the new ty_wnf memo, with a regression test.

The key lowered the type from depth 0, so a binder at depth i keyed the same as a free variable at level i with the same name. Main's closed pick(X => b => X) (which reduces to U32) and gen's pick(X => b => Y) at Y := X (which reduces to X) shared one key. gen then read U32 for its +z, bind_uses kept no count for the boxed value it shares, and the native run read freed cells.

tests/reg/wnf_memo_bound_key.bend prints 846393204 instead of 100 without the fix. The fix lowers from 2 ** 30, past every probe and level, and leaves the C of the witness byte-identical to main. The JS output never differed.

On this branch merged onto main af6a54f, every gate passes: repo 50 / 50, ping 47 / 47, test 1575 / 1575, perf 124 / 124, and safe 1016 agree with the same single false reject as main (agree_32). The description has the details, and it now gives the literal_readback counts the right way round (branch 14, main 20).

…memoizing ty_wnf

Drops the structural ty_wnf memo (an implicit sharing layer that changed
literal_readback's print descriptor). Instead:

- tele_unbind stores a def's domains reduced, so fun_of's layouts and
  file_book's datatype scan read one reduced object per domain.
- fun_of takes a fully applied def's return type from that telescope.
- file_book reads the datatypes of a def's own λs and body, and of a
  call's head, off the signature's reduced telescope (tele_adts), in the
  order type_adts walks it, so constructor ids keep their numbering.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril nicolas-abril changed the title safe.ts and comp.ts stop reducing types they never read, and safe_pass dedups book.order in one pass safe.ts and comp.ts stop reducing types they never read or already reduced, and safe_pass dedups book.order in one pass Oct 6, 2026

@Lorenzobattistela Lorenzobattistela left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at f9012d5. Merged onto main 7ed3424, the output is byte-identical to main's across the corpus, and every gate matches main. Details are in the description.

@Lorenzobattistela
Lorenzobattistela merged commit ebd9e90 into bendlang:main Oct 6, 2026
1 check passed
nicolas-abril added a commit that referenced this pull request Oct 6, 2026
… added code

#1362 merged before this pass. Drops what no program in the repo needed:
comp.ts's signature-once machinery (the reduced telescope, tele_adts,
fun_of's return type, file_book's spine map), which only sped up a
synthetic live def with a computed type; NOT_FN and the tree leaf skip,
since a constructor argument alone gives app_win's saving; and the order
dedup's Map, now one line. Output is byte-identical to main's.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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.

2 participants