Skip to content

A native build lets go of the compiler's memory before clang runs, so its peak is the larger of the two, not their sum - #1385

Merged
Lorenzobattistela merged 1 commit into
bendlang:mainfrom
Iann29:free-heap-before-clang
Oct 7, 2026
Merged

Lorenzobattistela merged 1 commit into
bendlang:mainfrom
Iann29:free-heap-before-clang

Conversation

@Iann29

@Iann29 Iann29 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

bend x.bend -o x writes the C, then waits on clang with spawnSync. All that time the compiled book's File (FL: every segment's lines, the memo tables, the facts) stayed reachable, and the waiting process allocates nothing, so nothing collected it: bend's emission heap sat beside clang for the whole C compile.

compile_book now leaves FL as a fresh File once the C is built, and cli_emit collects (Bun.gc(true)) before cli_build. The C and the binary are unchanged.

Memory

AMAGE Eco's CPU reference renderer (Chromi/examples/eco/reference.bend from amage-si: 1,759 segments, 4.1 MB of C), bend reference.bend -o ref, RSS sampled every 0.2 s, on a Ryzen 7 5800H (32 GB, Linux) with Bun 1.4.2 and clang 22:

main this PR
bend while clang runs 1.69 GiB 0.80 GiB
bend and clang together, peak 2.37 GiB 1.68 GiB (bend's own, before clang)
wall 47.3 s 47.9 s

With only Bun.gc(true), bend held 1.47 GiB during clang: most of what it kept was FL. Adding Bun.shrink() after the collection changed nothing measurable.

Same output

  • The binaries built by main and by this branch are byte-identical (same output name; the temporary C's name ends up in the binary).
  • compile_book's result is untouched: for all 1,668 .bend files under tests/, demos/ and bench/, the emitted .c, .js and .mjs and the exit status and stderr of every file are the same as main's.
  • gates/test.ts's shard script run locally (Linux, no display, no CUDA), on the previous main (eebc18c): 1579 / 1581 with all three PRs of this work applied, and the same on main: the misses are io_chan_try_late [c] (a timing race on this machine) and io_process_run [interp, js] (its inherited-pipe cases, under the local runner).
  • comp.ts is 63,972 ttok (main: 63,940) and main.ts 10,285 (main: 10,280). tsc -p bend2/pack reports the same 12 errors as main, and gates/repo.ts passes, 50 / 50.

Found while building AMAGE Eco, a UI toolkit written in Bend 2 (amage-si).

bend x.bend -o x waits on clang with spawnSync while the compiled book's
File (every segment's lines, the memo tables, the facts) stays reachable,
so the emission's heap sat beside clang for the whole C compile.
compile_book now leaves FL as a fresh File once the C is built, and
cli_emit collects before cli_build. On AMAGE Eco's reference renderer
bend holds 0.80 GiB while clang runs instead of 1.69 GiB, and the build
peaks at 1.68 GiB instead of 2.37 GiB. The C and the binary are the same.
@Lorenzobattistela
Lorenzobattistela merged commit f763ec6 into bendlang:main Oct 7, 2026
2 checks passed
Iann29 added a commit to Iann29/bend that referenced this pull request Oct 7, 2026
…d too

bendlang#1385 brought main's comp.ts to 63,975 ttok, which put this branch at
64,015. These rewrites take 30 back, with the same output: the File,
Scope, Seg and Spine types list several fields per line; term_const,
fun_of and intr_of read lit_call's result, h and tld.i as truthiness,
since each is an object or an array when present. The branch is at
63,985, and merged with bendlang#1387 (which takes its own share) at 63,989.
Iann29 added a commit to Iann29/bend that referenced this pull request Oct 7, 2026
…d too

bendlang#1385 brought main's comp.ts to 63,975 ttok, which put this branch at
64,017. These rewrites take 38 back, with the same output: js_host moves
into js_lib, its one caller, with the js_marshal calls in the same
order; six OPERATIONS entries with a short C template take two lines,
not four; lay_box, val_box and val_unbox read lay.arms as truthiness,
since it is a record when present. The branch is at 63,979, and merged
with bendlang#1386 (which takes its own share) at 63,989.
@Lorenzobattistela Lorenzobattistela mentioned this pull request Oct 13, 2026
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