Skip to content

refactor(comp): shed 90 tokens with no output change - #2027

Merged
Lorenzobattistela merged 3 commits into
mainfrom
comp-shed-tokens-1601
Nov 3, 2026
Merged

Lorenzobattistela merged 3 commits into
mainfrom
comp-shed-tokens-1601

Conversation

@Lorenzobattistela

@Lorenzobattistela Lorenzobattistela commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Trims bend2/comp.ts from 65,995 to 65,905 ttok (cap 66,000) without changing what the compiler emits.

What

  • Locals read once, outside any loop, are inlined, and lines that fit in 80 columns are joined.
  • term_use(term_uses(x), p) becomes rest_use([x], p) where it doesn't run per element.
  • val_brw is inlined (2 uses), and the two WL_CONT/WL_IDX pushes in emit_fork are one file_push.

Following #1922's review, type declarations, object literals, if/else statements, for loops, explicit checks and comments are kept as they are. Following review here, no local that a loop or callback reads is inlined, emit_chain keeps its FL.js guard, calls with side effects (emit_native, seg_name, host, c_ids) run in main's order, and main_pure keeps the nullable Bend.tele_open.

Verified (on main e2a7f60; comp.ts is unchanged on main since)

  • C (-o x.c) and JS (-o x.js) for every .bend under tests/, demos/ and bench/runtime/: 1,911 files, 922 C and 974 JS outputs, with stdout, stderr and exit codes byte-identical to main.
  • comp.ts type-checks with --noUnusedLocals (no errors in comp.ts).
  • Gates (on 05096d3 merged onto main 7443846): repo 54/54, ping 81/81, test 1826/1826, safe 1162 agree / 0 false reject. Repo is 54/54 on the head too.

🤖 Generated with Claude Code

… change

Inlines single-use locals of pure or memoized calls, joins lines that fit
in 80 columns, reuses rest_use, ty_all and seg_text where they apply, and
drops val_brw, the JS early return in emit_chain (its regex never matches
JS) and the copy emit_native made of its destination.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator Author

Clanker made too much code golfing...

@Lorenzobattistela
Lorenzobattistela deleted the comp-shed-tokens-1601 branch November 3, 2026 11:19

@nicolas-abril nicolas-abril 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.

I traced all the inlined locals and found no change to the emitted output. Nice. A few of the inlines move work that used to run once into a per-element loop, though, and several of those are on the JS emit path, which the PR's timing (C emit only) didn't cover. Comments inline. Restoring the hoisted locals (and the FL.js guard) costs a few of the saved tokens and keeps the output identical.

Comment thread bend2/comp.ts Resolved
Comment thread bend2/comp.ts Resolved
Comment thread bend2/comp.ts Resolved
Comment thread bend2/comp.ts Resolved
Comment thread bend2/comp.ts Resolved
Comment thread bend2/comp.ts Resolved
Comment thread bend2/comp.ts Resolved
Comment thread bend2/comp.ts Resolved
Comment thread bend2/comp.ts Resolved
@Lorenzobattistela
Lorenzobattistela restored the comp-shed-tokens-1601 branch November 3, 2026 11:22
Restores the locals that the previous commit inlined into per-element
callbacks (ws in emit_ctr, fs in js_expr, the ctr_flds and let_live of
anf, k in emit_tab, add in term_uses, wide in fun_of, done in brw_of,
local in facts_ctr, ks in ty_holds, doms in emit_unfold, edges in
compile_book, u in let_live), and the FL.js guard of emit_chain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Lorenzobattistela Lorenzobattistela changed the title refactor(comp): shed 219 tokens with no output change refactor(comp): shed 120 tokens with no output change Nov 3, 2026
@Lorenzobattistela

Copy link
Copy Markdown
Collaborator Author

Thanks, all nine are right: each of those inlines moved work into a per-element loop or callback (or dropped the FL.js guard) to save a few tokens, and the C-only timing missed the JS ones. 05096d3 puts them back as on main, along with the same pattern in brw_of (done), facts_ctr (local), ty_holds (ks), emit_unfold (doms) and let_live (u). What's left only inlines locals read once, joins lines and drops val_brw.

comp.ts is now 65,875 (-120 instead of -219). C and JS are still byte-identical to main on all 1,911 programs, and repo is 54/54.

@Lorenzobattistela Lorenzobattistela self-assigned this Nov 3, 2026
anf reads its hoisted spine again (m was left unused), emit_intr keeps
its lays local, main_pure's field walk goes back to Bend.tele_open,
whose null ends it, and emit_spin, emit_clo and js_lib call emit_native,
seg_name, host and c_ids in main's order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Lorenzobattistela Lorenzobattistela changed the title refactor(comp): shed 120 tokens with no output change refactor(comp): shed 90 tokens with no output change Nov 3, 2026
@Lorenzobattistela
Lorenzobattistela merged commit 0ba4e5e into main Nov 3, 2026
1 check passed
@Lorenzobattistela
Lorenzobattistela deleted the comp-shed-tokens-1601 branch November 3, 2026 12:17
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