Skip to content

fix: free the last build - #68

Merged
lukeed merged 5 commits into
mainfrom
retain
Oct 15, 2026
Merged

lukeed merged 5 commits into
mainfrom
retain

Conversation

@lukeed

@lukeed lukeed commented Oct 15, 2026 •

Copy link
Copy Markdown
Owner

Closes #59. Stacked on #67.

bongo dev on a large site kept far more memory than its output needs. On the 10,000-page bench, its footprint swung between 460 MB and 1.46 GB, and its RSS grew by ~1 GB with each of the first full rebuilds.

  • The last build stayed alive (build: the last build stays in memory after build() returns #59). Compiled partials keep the template context until the next build, and the $assets of that context was made inside run(), so its closures kept the whole scope of run(). In dev, that held the HTML of every page twice: about 420 MB. $assets now comes from a factory outside run(), which keeps only the assets folder, the render context, and the base path.
  • Stale templates stayed in the caches. templates.collect now starts from empty caches, so a partial or layout that is gone keeps nothing alive, and no stale block stays.
  • Garbage piled up between rebuilds. JSC let dev's heap keep about 400 MB of garbage from each rebuild. Dev now runs a full collection after each rebuild, in a new task. In the same task, a frame of the build still pins its objects on the stack, which JSC scans conservatively, so the collection freed little.
  • The bench measures dev. bun run bench dev runs bongo dev on the generated site through a small runner: the first build, 3 full rebuilds from a layout edit, and 3 page edits. For each step it reports the time that dev logs, RSS, and the footprint and JS heap once dev settles. On macOS, RSS also counts memory that the allocator freed until the system takes it back. The footprint, as Activity Monitor shows it, is the number to watch.

Benchmark

bun run bench dev on an M1 Max with Bun 1.4.3: #63 against this PR.

dev, 10k pages #63 footprint this PR footprint #63 JS heap this PR JS heap
first build 803 MB 255 MB 447 MB 130 MB
full rebuild 1 1,223 MB 264 MB 446 MB 132 MB
full rebuild 2 1,457 MB 192 MB 909 MB 132 MB
full rebuild 3 787 MB 139 MB 515 MB 132 MB
page edit 3 460 MB 138 MB 248 MB 131 MB

RSS after the last step: 2.79 GB → 1.49 GB. A full rebuild still takes 1.8 s, and a page edit about 0.8 s.

Tests: 2 new tests. One checks that a dev build of 200 large pages keeps none of them once it returns: with the old build.ts, it keeps 17.8 MB. The other checks that collect keeps nothing of a deleted partial or layout. All 808 pass, tsc -p . and tsc -p build are clean, and bun run build passes.

Also in this PR

  • test: subprocess heap check: on top of perf: write over public/ #67, the retention test failed in the full suite, though the build kept nothing. After a long test run, JSC's conservative stack scan found a stale pointer to the scope of run() in the test process. The test now builds in a new bun process.
  • docs: bench results: bench/results.md records the bench numbers of the baseline and of each PR from perf: stream page writes #63 to fix: free the last build #68, measured with the same bench in one session, so later changes can be compared against them.

lukeed added a commit that referenced this pull request Oct 15, 2026
The bench numbers of the baseline and of each PR from #63 to #68, all
measured with the same bench in one session, so later changes can be
compared against them.
@lukeed
lukeed changed the base branch from stream to inplace October 15, 2026 18:31
Compiled partials keep the template context until the next build, and
the `$assets` of that context was made inside `run()`, so its closures
kept the whole scope of `run()` alive: in dev on the 10k-page bench,
the HTML of every page twice, ~420 MB. `$assets` now comes from a
factory outside `run()` that keeps only the assets folder, the render
context, and the base path.

`templates.collect` also starts from empty caches, so a partial or
layout that is gone keeps nothing alive, and no stale block stays.

Closes #59
`bun run bench dev` runs `bongo dev` on the generated site: the first
build, 3 full rebuilds from a layout edit, and 3 page edits. For each,
it reports the time that dev logs, the RSS during it and after it, and
the footprint and JS heap that dev reports once it settles. On macOS,
RSS also counts memory that the allocator freed until the system takes
it back, so the footprint is the number to watch.
On the 10k-page bench, the JS heap of dev kept ~400 MB of garbage from
each rebuild, so its footprint swung between 460 MB and 1.46 GB. Dev
now runs a full collection after each rebuild, in a new task: in the
same task, a frame of the build still pins its objects on the stack,
which JSC scans. The footprint now settles at ~138 MB and the JS heap
at ~131 MB, and rebuilds take the same time.
The retention test measured the heap in the test process. After a long
test run, JSC's conservative stack scan found a stale pointer to the
scope of `run()` and kept the build alive, so the test failed only in
the full suite. It now builds in a new `bun` process, where the stack
is shallow, and runs each GC in a new task.
The bench numbers of the baseline and of each PR from #63 to #68, all
measured with the same bench in one session, so later changes can be
compared against them.
@lukeed
lukeed changed the base branch from inplace to main October 15, 2026 18:44
@lukeed
lukeed merged commit ae7d899 into main Oct 15, 2026
1 of 6 checks passed
@lukeed
lukeed deleted the retain branch October 15, 2026 18:47
@lukeed lukeed mentioned this pull request Oct 15, 2026
6 tasks done
lukeed added a commit that referenced this pull request Oct 17, 2026
Full builds, docs, a new table of bongo build after each kind of
edit, and dev with the new steps, next to #68 on the same steps.
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.

build: the last build stays in memory after build() returns

1 participant