Skip to content

perf: stream page writes - #63

Merged
lukeed merged 24 commits into
mainfrom
stream
Oct 15, 2026
Merged

lukeed merged 24 commits into
mainfrom
stream

Conversation

@lukeed

@lukeed lukeed commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Closes #56.

bongo build now writes each page as soon as it renders, so it no longer holds every page in memory. Dev and narrow rebuilds still hold every page, so a failed rebuild leaves the last good build serving.

  • Walk 2 writes each page, with its placeholders, through a pool of 64 writes, and drops each page body once the page renders. After the bundler flush, bongo reads each page again, puts in the asset URLs, runs $hooks.transform, and writes the page again. Rereading and rewriting 10k pages costs about 0.4 s; a second render pass would cost about 1.3 s, and would run each template twice.
  • bundler.placeholders() copies each match. A regex match shares the memory of the whole string that it came from, so the placeholders of each page kept the HTML of that page alive until the build ended. On 10k pages, that was 180 MB of the 253 MB live heap after rendering.
  • Walk 1 reads and loads 16 content files at a time, not every file at once. Each file is read into a view, not a copy.
  • A failed bongo build waits for every write that it started, then empties public/, so it leaves no page with placeholders in it. utils.settle (wait for every promise, then throw the first failure) replaces Promise.all where a failure could leave writes running.
  • New utils: pool(limit), limit(size), settle(list).
  • --debug names a new stage, listing sizes, for the size report after "done".

Benchmark

bun run bench (a new bench/ workspace) builds a generated 10,000-page Markdown site and the docs site. Each run is a new bongo build process. It reports wall, user, and sys time, peak RSS, and a sha256 of public/. A stage profile adds, at the start of each stage: RSS, the memory that mimalloc has committed and has in use, the JS heap, and the live heap after a full GC.

Interleaved A/B on an M1 Max with Bun 1.4.3: two rounds each, median of 5 runs per round. "Before" is the build code of main, with the same bench.

10k-page site before this PR change
wall 6.22 s, 6.26 s 5.02 s, 5.04 s −19%
user 3.52 s, 3.54 s 3.88 s, 3.88 s +10%
sys 20.9 s, 21.3 s 13.2 s, 13.3 s −37%
peak RSS 1159 MB, 1164 MB 595 MB, 572 MB −50%

Both sites write the same bytes as before: the sha256 of public/ does not change. On the docs site (109 files), the wall time does not change, and peak RSS is about 3 MB higher.

The live heap now peaks at about 125 MB. Most of the gap to RSS is garbage that JSC has not collected yet: BUN_JSC_forceRAMSize=1073741824 lowers peak RSS by about 230 MB. Some is memory that mimalloc keeps: MIMALLOC_PURGE_DELAY=0 lowers it by about 100 MB. These are diagnostics, not defaults.

Follow-ups: #59 (the last build stays in memory after build() returns), #60 (emptying public/ takes 1–2 s), #61 and #62 (stacked PRs on this branch).

Notes

  • In bongo build, $hooks.transform now runs at most 64 at a time, and gets the page text as read back from disk. Thus a lone surrogate arrives as U+FFFD. The written bytes do not change.
  • bun run bench rebuilds the packages first, so it always measures the current source.

Tests: 14 new tests, for streamed writes, failure cleanup, pool, settle, limit, and placeholder memory. All 806 pass, tsc -p . and tsc -p build are clean, and bun run build passes.

lukeed added 24 commits October 14, 2026 17:46
The dev server sent text files, such as .md, .txt, and .xml, with no
charset, so browsers read their UTF-8 as Windows-1252: an em dash
showed as '—'. Text, JSON, XML, and JavaScript now say UTF-8.
A layout names other versions of a page with <bongo:alternate>, which
becomes a <link rel="alternate">. For text/markdown, bongo also writes
the page's source, under its title and summary, at the route plus .md
($utils.url.ext): read from disk when written, lazily in dev. Output
files now render after every page, so they and data modules see each
page's alternates in $pages. Passed-through element attributes are
no longer escaped twice.
Each docs page gets a Copy page button, with Copy as Markdown and
View as Markdown, from its <bongo:alternate> Markdown copy. The site
also writes /llms.txt, grouped as in the sidebar.
A page with no Markdown source failed the build when its layout named
a Markdown copy. A template cannot tell which pages are Markdown, so
one layout could not serve a mixed site. Such a page now gets no copy
and no <link>.
A guide for Markdown copies and llms.txt, the <bongo:alternate> and
url.ext references, alternates in $pages, and output files that
render after every page.
A client navigation replaced the whole body, so a scrolled sidebar
went back to the top. An element with transition:persist now moves
into the new page in place of the element with the same key, and
keeps its scroll, focus, and state.
The sidebar persists across navigations and marks the new page
itself. On a phone, opening the menu centres the current page.
The llms.txt guide is now three: Markdown copies (with the copy
buttons), Alternates (feeds, translations, and $pages), and Create an
llms.txt.
How to build a site in more than one language with frontmatter and
$pages: hreflang links, <html lang>, layout text in data/, lists in
one language, and a sitemap with each version. Alternates now points
there for translations.
On a phone the open menu covered the Menu button, so it could not be
closed. The menu now opens below the bar, the button shows a close
icon, and the menu and the page outline close each other.
'Render dynamic images' is now 'Dynamic images'.
The CLI tests picked a random port and expected dev to bind exactly
that one. A port that another program held, such as 7000 for macOS
AirPlay, made dev move to the next port and the test fail. The tests
now ask the OS for a free port.
`bun run bench` builds a generated 10,000-page Markdown site and the
docs site. Each run is a new `bongo build` process, so the peak RSS of
each run is its own; mitata times work in one process and cannot get
this number. It reports wall, user, and sys time and peak RSS, and a
sha256 of public/ to prove that two refs write the same bytes.

One more run goes through bench/stages.ts, which prints each build
stage, while `ps` samples RSS, to show which stage raises the peak.

The docs site stays a second target, not the large input: it scales
badly as a copy, because each page lists every docs page and renders
an Open Graph card.

Refs #56
The profile adds a second run through bench/stages.ts with `--heap`:
at each stage line it runs a full GC and reads the live JS heap. It is
a separate run, because its GCs lower the RSS that the plain run
samples.

Refs #56
`pool(limit)` runs jobs alongside other work, at most `limit` at once.
A failed job is kept until `add` or `drain` throws it, so it is never
an unhandled rejection, and `drain` waits for every job first.

Refs #56
A regex match shares the memory of the string it came from, in JSC
and in V8. The placeholders of each page live in the `bundles` map
until the build ends, so each one kept the HTML of its page alive.
On the 10k-page bench, the live heap after rendering was 253 MB, of
which about 180 MB was page HTML held this way.

Refs #56
`bongo build` writes each page as it renders, through a pool of 64
writes, and drops each page body once it renders. After the bundler
flush, it reads each page again, puts in the asset URLs, runs
`$hooks.transform`, and writes it. Dev and narrow builds still hold
every page, so a failed rebuild leaves the last good build.

A failed `bongo build` waits for its writes, then empties `public/`,
so it leaves no page with placeholders in it.

10k-page bench, median of 5: peak RSS 1198 MB -> 814 MB, wall
6.43 s -> 5.79 s. The output is the same, byte for byte.

Refs #56
`add` reserved its slot only after an await, so adds that waited at
once all started: `pool(1)` ran 4 jobs together. The pool now hands a
freed slot to one waiting add, as `images.slot` does. A job that
throws at once fails the pool like one that rejects, and a limit that
is not a whole number above 0 throws.
`Promise.all` rejects at the first failure while its other writes go
on. A failed `bongo build` empties public/, so such a write could land
after the cleanup. `utils.settle` waits for every promise before it
throws the first failure. `fs.list` and the write groups of a build
use it.

Refs #56
`toLocaleDateString` with options builds a new formatter for each of
the ~20,000 dates that the bench site renders, which cost ~0.4 s. One
`Intl.DateTimeFormat` gives the same text.
The size list after "done" reads and gzips every output file. On the
10k-page bench it takes ~1.1 s and raises peak RSS by ~40 MB, but it
ran inside the "writing pages" stage. Its own stage shows it in
`--debug` and in the bench. #61 tracks the cost itself.
At the start of each stage, the profile now reads the RSS, the bytes
that mimalloc has committed and has in use, and the JS heap, and the
live heap after a full GC in a second run. Thus a gap between RSS
and live data shows if it is garbage that the GC has not collected,
memory that the allocator keeps, or memory outside mimalloc. The
profile also prints the stderr of its run, for allocator dumps.

Refs #56
`limit(size)` runs each job once fewer than `size` jobs run, and gives
back the job's result. Unlike `pool`, a call ends when its job ends, so
a caller such as `fs.list` still waits for the work itself.
`fs.list` started the read and the loader of every content file at
once. A build now runs 16 at a time. It also reads each file into a
view and not a copy, and takes the frontmatter signature before the
loader runs, so the decoded source is not kept across it.

10k-page bench, 3 runs each (wall / sys / max RSS in "reading
content/"): no limit 5.22 s / 15.2 s / 387 MB; 64: 5.05 / 13.5 / 330;
32: 4.87 / 12.9 / 313; 16: 4.97 / 13.0 / 299; 8: 4.93 / 13.1 / 323.
Then 16 against 32, 5 runs, two rounds each: same wall time, and
median peak RSS 605 and 602 MB against 629 and 675 MB.

Refs #56
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 merged commit b7960e4 into main Oct 15, 2026
1 of 3 checks passed
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 deleted the stream branch October 15, 2026 18:47
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: stream page writes instead of holding every page in memory

1 participant