Skip to content

refactor(comp): a round of simplifications, 65,994 -> 65,940 ttok - #2088

Merged
nicolas-abril merged 2 commits into
mainfrom
simp/comp-round1
Nov 7, 2026
Merged

nicolas-abril merged 2 commits into
mainfrom
simp/comp-round1

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

A round of simplifications in bend2/comp.ts: 65,994 → 65,940 ttok (−54; the cap is 66k), in that file only.

The file has been through many rounds already; four independent passes (compiler front, compiler back end, C runtime, JS runtime) found little left that simplifies rather than golfs. Everything here keeps the file's style, and no cache is touched.

What changes

C runtime

  • f32_text writes the fixed form with snprintf("%.*f", p - ex), which rounds at the same place, 10^(ex − p), as the scientific form did; a whole number moves the digits over the point and pads with zeros. This replaces copying the digits into d[9] and rebuilding the string place by place.
  • The host's globals and their device stand-ins (KEEP_WORDS, the f32_show / f32_read stubs, corpus_grow) share Globals' one #if !DEVICE … #else split, instead of three separate ones.

Compiler

  • facts_hot names its forced recursion hot, built only in the branch that uses it, instead of repeating facts_hot(def, x, true, local) three times.
  • nat_is_lt needs no $o substitution.
  • ctr_adt uses the existing tld_of.
  • anf's Lam case binds the instantiated body all.B(DUMMY) once instead of computing it twice.

Verification

  • Emitted programs: bend f -o f.c and -o f.js on all 1,935 test, demo and bench programs, against main (ca77ef2). Every compile error is identical. Every .js is identical to main's, and every .c differs from it by the same single diff, which is exactly the C runtime edits above. So no compiler change alters any output.
  • f32_text: compared bit for bit with the old function on every float32 from 0x33000000 to 0x63000000, both signs (about 1.6 billion values, past both cut-offs), every denormal, inf and nan: 0 differences.
  • Machine code: for all 17 benches, the host CPU objects are byte-identical, the Metal AIR is byte-identical, and the host Metal build differs only in the embedded source's size.
  • Gates:
    • gates/repo.ts: 56 / 56.
    • gates/test.ts: 1,847 / 1,847. An earlier run on the same commit had 4 misses that didn't repeat.
    • gates/perf.ts: 124 / 124 on a rerun. The first run's one miss, queens PAR-CPU at 1.39x, read 1.02x on the rerun, and also 1.02x on main run right after.
  • Not tested: CUDA (no edit touches its code) and Linux. glibc's %f is exact too, so the f32_text argument holds there, but it was run only on macOS.

🤖 Generated with Claude Code

nicolas-abril and others added 2 commits November 7, 2026 14:38
- f32_text writes the fixed form with %.*f, which rounds where the
  scientific form does; a whole number pads the digits with zeros
  (same output on every float32, checked exhaustively)
- the runtime's host globals and their device stand-ins (KEEP_WORDS,
  f32_show, f32_read, corpus_grow) share Globals' one DEVICE split
- facts_hot names its forced recursion hot; nat_is_lt needs no $o;
  ctr_adt and anf use tld_of and ty_all
- io_wait's caller already checks waits is non-empty

Every emitted program differs from main only by these runtime lines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- io_wait keeps its fallback for an empty waits list
- anf's Lam case calls tele_open, which may give null, and binds the
  instantiated body once
- facts_hot builds hot only in the branch that uses it, and passes it
  to forEach directly

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril nicolas-abril changed the title refactor(comp): a round of simplifications, 65,994 -> 65,926 ttok refactor(comp): a round of simplifications, 65,994 -> 65,940 ttok Nov 7, 2026
@nicolas-abril
nicolas-abril merged commit 70607d7 into main Nov 7, 2026
1 check passed
@nicolas-abril
nicolas-abril deleted the simp/comp-round1 branch November 7, 2026 15:52
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.

1 participant