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..7eaae0cc9 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; do { was = facts(); [FL.lend, FL.spun, FL.clos, FL.tabs, FL.lits, FL.consts, FL.brws] @@ -2798,6 +2799,11 @@ 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 (process.memoryUsage().heapUsed > pressure) { + globalThis.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)) {