From 0afe38df9d116a081fd9d2da6ff0628015bf3339 Mon Sep 17 00:00:00 2001 From: oxura Date: Sun, 11 Oct 2026 18:33:16 +0600 Subject: [PATCH 1/2] Collect transient emission garbage on heap growth --- README.md | 3 +++ bend2/comp.ts | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 02737ec3e..19ed8fda3 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,9 @@ Hints: Bend works best on the back-end, on Linux or macOS. +Bun's native code emitter samples heap use every 64 definitions and collects +at 2 GiB, then after each 2 GiB of growth. Live compiler state is not capped. + # Examples ### Syntax == Python + dependent types diff --git a/bend2/comp.ts b/bend2/comp.ts index ff0ca30fb..348235423 100644 --- a/bend2/comp.ts +++ b/bend2/comp.ts @@ -2788,6 +2788,7 @@ export function compile_book(book: Bend.Book): string { ...names.map((c) => Bend.book_fam(FL.book, c))]); const facts = () => FL.own.size + FL.hot.size + FL.stat.size; let was: number; + let pressure = 2 ** 31, emitted = 0; do { was = facts(); [FL.lend, FL.spun, FL.clos, FL.tabs, FL.lits, FL.consts, FL.brws] @@ -2798,6 +2799,12 @@ export function compile_book(book: Bend.Book): string { for (const [k, tld] of [...done_defs().reverse(), ...done_defs(def_foreign)]) { memo_gc(); + // Collect on heap growth, not the host's total RAM budget. + if ((++emitted & 63) === 0 && typeof Bun !== "undefined" && + process.memoryUsage().heapUsed > pressure) { + Bun.gc(true); + pressure = process.memoryUsage().heapUsed + 2 ** 31; + } const [dl, vals] = emit_open(scope_new(), k); FL.segs.push(dl.seg); if (def_foreign(tld)) { From 5342ce14dd9ce6a0e1410018001444a09f92fa5a Mon Sep 17 00:00:00 2001 From: Nicolas Abril Date: Sun, 11 Oct 2026 23:48:12 +0200 Subject: [PATCH 2/2] Emission samples the heap at every def, and the Bun guard is optional chaining process.memoryUsage() costs about 0.6 us in Bun, so the every-64-defs counter saves nothing measurable; globalThis.Bun?.gc keeps non-Bun hosts working. -20 tokens: merged onto main d71f42ee, comp.ts is 63,994. --- bend2/comp.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bend2/comp.ts b/bend2/comp.ts index 348235423..7eaae0cc9 100644 --- a/bend2/comp.ts +++ b/bend2/comp.ts @@ -2788,7 +2788,7 @@ export function compile_book(book: Bend.Book): string { ...names.map((c) => Bend.book_fam(FL.book, c))]); const facts = () => FL.own.size + FL.hot.size + FL.stat.size; let was: number; - let pressure = 2 ** 31, emitted = 0; + let pressure = 2 ** 31; do { was = facts(); [FL.lend, FL.spun, FL.clos, FL.tabs, FL.lits, FL.consts, FL.brws] @@ -2800,9 +2800,8 @@ export function compile_book(book: Bend.Book): string { ...done_defs(def_foreign)]) { memo_gc(); // Collect on heap growth, not the host's total RAM budget. - if ((++emitted & 63) === 0 && typeof Bun !== "undefined" && - process.memoryUsage().heapUsed > pressure) { - Bun.gc(true); + if (process.memoryUsage().heapUsed > pressure) { + globalThis.Bun?.gc(true); pressure = process.memoryUsage().heapUsed + 2 ** 31; } const [dl, vals] = emit_open(scope_new(), k);