Skip to content

Commit fd4e39c

Browse files
committed
Mem.settle: one full collection after the boot's thin index is warm (JS only, once)
On the JS target the boot-time thin index build leaves ~0.9 GB of heap capacity behind at the boot peak (3.35 -> 2.44 GB with collections, on a copy of the real log). H.Mem.settle (effects/mem.js: Bun.gc(true) and Bun.shrink, at most once per process; effects/mem.c: nothing, "") runs when ThinWarm arrives (Hub.thin.warmed) and its line (time, RSS before and after) is logged. It goes once store mode keeps the thin index small.
1 parent 1900588 commit fd4e39c

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/server/effects/mem.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Mem.settle (host.bend): nothing to settle in the native build (no GC heap
2+
// holds the thin index's build); the line to log is "".
3+
4+
#ifdef CID(Mem.settle)
5+
6+
Term mem_settle_run(Env e, Term* f, IoWork* w) {
7+
return term_pak(CID(SNil), 0);
8+
}
9+
10+
static void __attribute__((constructor)) mem_settle_use(void) {
11+
io_eff(CID(Mem.settle), mem_settle_run);
12+
}
13+
14+
#endif

‎src/server/effects/mem.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Mem.settle (host.bend): one full collection and a release of free memory,
2+
// at most once per process (after the boot's thin index is warm); the line
3+
// to log with its time and the RSS before and after, "" when nothing was done.
4+
let mem_settled = false;
5+
6+
function mem_settle() {
7+
if (mem_settled || typeof Bun === "undefined" || typeof Bun.gc !== "function") return "";
8+
mem_settled = true;
9+
const r0 = process.memoryUsage().rss;
10+
const t0 = Date.now();
11+
Bun.gc(true);
12+
if (typeof Bun.shrink === "function") Bun.shrink();
13+
const ms = Date.now() - t0;
14+
const r1 = process.memoryUsage().rss;
15+
return "memory settled after the thin index: one full collection in " + ms + " ms, rss " + Math.round(r0 / 1e6) + " MB -> " + Math.round(r1 / 1e6) + " MB";
16+
}
17+
18+
io_eff(CID(Mem.settle), mem_settle);

‎src/server/host.bend‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import Base
66
def Clock.epoch() -> IO(U32):
77
import "./effects/host.js"
88

9+
# One full collection and a release of free memory, at most once per
10+
# process, called when the boot's thin index is warm (server.bend's
11+
# Hub.thin.warmed): on the JS target its build leaves ~0.9 GB of heap
12+
# capacity behind at the boot peak (3.35 GB → 2.44 GB measured on a copy of
13+
# the real log, 2026-10-22). Answers the line to log; "" when nothing was
14+
# done (the C build, or any later call). It goes once store mode keeps the
15+
# thin index small (only the recent threads).
16+
def Mem.settle() -> IO(String):
17+
import "./effects/mem.c"
18+
import "./effects/mem.js"
19+
920
# The event log from byte offset off (decimal text; "0" for all of it,
1021
# a line's start for a tail) to its end, read in one go: the text that
1122
# closes off a torn tail, the offset past the last whole line, then the

‎src/server/server.bend‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11147,11 +11147,26 @@ def Hub.thin.answer(ws: List<&2, ThinWait>, h: Hub) -> IO(Hub):
1114711147
h2 : Hub <- Hub.thin.rpc.now(h, client, o, now)
1114811148
Hub.thin.answer(tail, h2)
1114911149

11150-
def Hub.thin.warmed(h: Hub, i: THIN.Index, m: TMAIL.Cache) -> IO(Hub):
11150+
def Mem.settled.k(none: Bool, +line: String) -> IO(Unit):
11151+
match none:
11152+
case True{}:
11153+
IO.pure(Unit, Unit{})
11154+
case False{}:
11155+
Log.info(line)
11156+
11157+
def Mem.settled(+line: String) -> IO(Unit):
11158+
Mem.settled.k(String.is_empty(line), line)
11159+
11160+
def Hub.thin.warmed.k(h: Hub, i: THIN.Index, m: TMAIL.Cache) -> IO(Hub):
1115111161
match h:
1115211162
case Hub{st, f, ls, cl, as, inbox, +cfg, note}:
1115311163
Hub.thin.answer(List.reverse(&2, ThinWait, Cfg.thin.waiting(cfg)), Hub{st, f, ls, cl, as, inbox, Cfg.thin.warm(cfg, i, m), note})
1115411164

11165+
# the boot's thin index arrived: the JS heap is settled once (H.Mem.settle,
11166+
# until store mode keeps the index small), then the waiting reads answered
11167+
def Hub.thin.warmed(h: Hub, i: THIN.Index, m: TMAIL.Cache) -> IO(Hub):
11168+
IO.bind(String, Hub, H.Mem.settle(), line => IO.bind(Unit, Hub, Mem.settled(line), _ => Hub.thin.warmed.k(h, i, m)))
11169+
1115511170
def Hub.thin.read_guard(valid: Bool, h: Hub, +client: U32, +text: String, +now: Nat) -> IO(Hub):
1115611171
match valid:
1115711172
case False{}: IO.pure(Hub, h)

0 commit comments

Comments
 (0)