Skip to content

refactor(comp): simplify comp.ts with the same behavior (-137 ttok) - #1891

Merged
Lorenzobattistela merged 2 commits into
bendlang:mainfrom
nicolas-abril:ai/comp-room
Oct 28, 2026
Merged

Lorenzobattistela merged 2 commits into
bendlang:mainfrom
nicolas-abril:ai/comp-room

Conversation

@nicolas-abril

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

Copy link
Copy Markdown
Collaborator

Structural simplifications to bend2/comp.ts, taking it from 65,982 to 65,845 ttok on main 757180e so #1880 can restore the comments it removed and still fit under the 66k cap. No names are shortened, no comments removed, nothing golfed; each change removes a concept or a duplicate of an existing helper.

Change ttok
emit_put drops the bindings left at a body's end, since every emit_put ends a body: emit_spin loses its tail mode, and emit_body / emit_match their own bind_dead before emit_put. −27
val_new is the one place that decides which words keep a root; four callers stop filtering by box themselves. −35
js_match sends Nat rows through the literal rows, as emit_match does for C. −24
CUDA's gpu_map zeroes the managed memory it maps, instead of corpus_setup doing it under #if BEND_CUDA. −23
JS closures build their wrapper with block(), as js_def does. −18
unit_emit resets FL.unit to null after the unit emits, instead of saving and restoring it: units never nest and it starts out null, so a use outside a unit still fails. −10

Output

All 1,762 tests, and the 72 .bend files under demos/ and bench/, built to C and JS with main and with this branch:

  • C differs only by the CUDA zeroing moving from corpus_setup into gpu_map (compiled out on Metal and CPU).
  • JS differs only where a Nat test was x === N and is now x == N, the same as word literals. A Nat in JS is always a number, so the two are equivalent.
  • Build logs (including expected refusals) are identical.
  • The 771 JS outputs that changed print the same under both builds.

bun gates/repo.ts: 54 / 54. tsc: no errors in comp.ts. Compiling the 17 runtime benches: 1.605 s on main, 1.606 s here (hyperfine, 8 runs).

Gates on the cluster (bun gates/_run.ts) at ccbe933:

repo  PASS: 54 / 54
ping  PASS: 80 / 81
test  PASS: 1752 / 1752
perf  PASS: 124 / 124

The one ping miss (a hyphenated entry, lic_bom--name.bend, refused by the localhost hub) fails the same way on main.

The emit_put change (ccbe933) was checked separately against fe97354: all 1,837 .bend files under tests/, demos/ and bench/ build to byte-identical C and JS, with identical logs, and compiling the runtime benches takes the same time (1.596 s vs 1.602 s).

Not verified: the CUDA path (no CUDA toolkit on this Mac or the minis).

🤖 Generated with Claude Code

@nicolas-abril nicolas-abril changed the title refactor(comp): simplify comp.ts with the same behavior (-365 ttok) refactor(comp): simplify comp.ts with the same behavior (-178 ttok) Oct 27, 2026
@nicolas-abril nicolas-abril changed the title refactor(comp): simplify comp.ts with the same behavior (-178 ttok) refactor(comp): simplify comp.ts with the same behavior (-172 ttok) Oct 27, 2026
@Lorenzobattistela Lorenzobattistela self-assigned this Oct 27, 2026
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator

Thanks, gates are green here too, merged onto 9a4dfe3: repo 54/54, test 1750/1750, ping 81/81, safe 0 false rejects. perf is 123/124, and the miss is merkle's compiler time, which misses on main the same way.

Five of the six changes look good to me, and the C/JS output matches main apart from the CUDA move and the Nat ==. The one I'd drop is term_same.

mat_lits calls term_same(cov(w), inst(w)) once per bit level of a U32 literal match, on whole arm bodies. The old Lam/App/Mat walk returned at the first difference. The new version lowers and stringifies both full terms on every call. None of the runtime benches has a large literal match, so the perf gate and the bench timing don't show it, but a sparse literal match with a big default arm does:

import Base
law f:
  for n: U32
  U32
def f(n):
  match n:
    case 0: 0
    case 7: 7
    ...                      # every 7th literal up to 399
    case _: U32.add(U32.mul(...U32.add(U32.mul(n, 3), 0)..., 32), 29)   # 30 deep

(the file has two such defs and a main that calls both)

bun bend2/main.ts big.bend -o big.c, M4:

default arm   main    PR      PR + main's term_same   check only
30 deep       2.0 s   2.6 s   2.1 s                   0.9 s
90 deep       5.5 s   7.3 s   6.0 s                   2.0 s

Leaving out checking, the C build gets about 50% slower, and putting the old term_same back gives byte-identical C at main's speed. JS is slower too (1.8 s -> 2.1 s on the small one). That's a lot to pay for ~60 ttok, so could you keep the old term_same and land the other five changes?

Two smaller things:

  • The first commit's message still describes the lay_eq / op_name changes and -365 ttok, which the second commit reverted. Please fix the squash message.
  • Nit, not blocking: the JS Nat rows now work through lits_cond thanks to the 64 / 0 bit sentinels, and the last row's (s & 0) == n only works because emit_chain turns the last arm into a bare else. An explicit Nat condition would be less fragile, but I'm fine with it as is.

- node_fields, val_to, val_unbox, emit_open: build the root gate list with
  val_new / map instead of by hand
- unit_emit: one Object.assign for the unit's fresh state, and FL.unit is
  reset after the unit emits
- js_match: Nat rows go through lits_cond like the other literal rows
- JS closures: the nested function/arrow pair is two block() calls instead
  of manual tab and line wrapping
- CUDA: gpu_map zeroes the stack area itself, so corpus_setup no longer
  needs its own #if BEND_CUDA memset

The C and JS outputs of every test, demo and bench match main except the
CUDA memset move and `=== N` -> `== N` on JS Nat rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril nicolas-abril changed the title refactor(comp): simplify comp.ts with the same behavior (-172 ttok) refactor(comp): simplify comp.ts with the same behavior (-110 ttok) Oct 27, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

Thanks, I reproduced it. With your generator (every 7th literal up to 399, two defs) built with -o x.c, 5 runs on an M4:

default arm main PR before PR with main's term_same
30 deep 2.27 s 2.88 s 2.38 s
90 deep 6.05 s 8.38 s 5.88 s

The C is byte-identical between the last two columns. I've dropped the term_same change (along with its comment), so it's main's version again. The branch is now one commit on 757180e with a message that matches what's in it: −110 ttok (65,982 → 65,872).

I'm leaving the JS Nat rows as they are for now, per your nit.

… tail mode

Every emit_put ends a body, so it starts with bind_dead(sc, []) itself:
emit_spin loses its tail flag and emit_body / emit_match their own
bind_dead before emit_put. The C and JS of every test, demo and bench
are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril nicolas-abril changed the title refactor(comp): simplify comp.ts with the same behavior (-110 ttok) refactor(comp): simplify comp.ts with the same behavior (-137 ttok) Oct 27, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator Author

I pushed one more small simplification, ccbe933 (−27 ttok, so the PR is now −137: 65,982 → 65,845).

Every emit_put ends a body, so emit_put now drops the bindings left at the end itself. With that:

  • emit_spin no longer needs its tail mode (the flag and the two tail ? … : … in its bind_dead call go away);
  • emit_body and emit_match no longer need their own bind_dead(sc, []) before calling emit_put.

The other two callers already have no bindings left when they reach it: the closure at the top of emit_body, and the foreign def in c_units. For them the new bind_dead does nothing.

Checked against fe97354:

  • all 1,837 .bend files under tests/, demos/ and bench/ build to byte-identical C and JS, with identical logs;
  • compiling the runtime benches takes the same time (1.596 s vs 1.602 s);
  • gates at ccbe933: repo 54/54, test 1752/1752, perf 124/124, ping 80/81 (the ping miss is the same lic_bom--name.bend hub refusal that main has).

I also went over the rest of comp.ts again (the front end, the emitters and both runtimes) and found nothing else worth taking: what's left either changes behavior, touches hot paths, or is golf.

@Lorenzobattistela
Lorenzobattistela merged commit fad0bb9 into bendlang:main Oct 28, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the ai/comp-room branch October 28, 2026 13:18
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