Branch: perf-trial-20260906-093914-epub (W16 of the perf campaign)
Scope: src/epub.mjs, src/archive-read.mjs, test/ram-thunk.test.mjs (new),
test/epub-integrity.test.mjs (flake fix), perf/zip-thunk-one.mjs (new).
W12 made writeZip stream the zip structure, but archiveReadable still handed it
entry objects holding every decoded payload: pageEntries[].body,
assetEntries[].data, coverEntry.data. For a 300-page magazine that is the entire
decoded book resident in memory during assembly — and it is duplicate data, since
every page is already written to bookDir/pages/*.xhtml, every asset to
bookDir/assets/*, and the cover to bookDir/cover.jpg before assembly starts.
Changes:
- src/epub.mjs — entry
datamay be a zero-argument thunk returning a payload (Buffer, or string for text).encodeEntryresolves it exactly once, when that entry is encoded, so bothzip()(buffer path) andzipChunks/writeZip(streaming path) share the same resolution point; the per-entry CRC is computed from the resolved bytes as before.entriesFornow emits page entries as lazy thunks too (() => wrapPage(pageBodyOf(part), …)), and spine pages accept{path, data: thunk|Buffer|string, viewport}next to the legacy eager{path, body}. Buffer/string entries are untouched — output is byte-identical. - src/archive-read.mjs — payloads are never retained. Each page body lives only
inside its fetch callback (written to disk, asset refs collected, then dropped);
entries become
{path, viewport, data: () => fs.readFileSync(dest), refs}. Assets and the cover likewise readbookDirlazily. Asset refs ride along on the page entries (refs, tiny strings) sowantedAssetskeeps its exact first-seen-page order — collection order would otherwise depend on fetch completion order. Logs, sidecars, skip-on-non-200, and fail-fast are unchanged.
Fresh process per impl over a shared on-disk fixture (same synthetic book as
syntheticEpub(): 120x6KB pages + 100x120KB jpegs + 250KB cover = 12,682,772
archived bytes — byte-identical outputs verified with cmp):
| impl | peakRssMB (3 runs) | note |
|---|---|---|
| buffer | 69, 69, 69 | entries list holds all payloads (pre-W16) |
| thunk | 55, 55, 55 | entries hold closures; one payload at a time |
Context: bare node floor on this machine is ~46–48 MB; after imports ~51–52 MB.
So the thunk path sits ~3 MB above the import floor (one payload + deflate copy +
stream buffers) versus +14 MB of whole-book residency for the buffer path — a
14 MB drop on a 12.7 MB book, scaling with book size. The buffer impl reproduces
the W12 writeZip floor (71 MB cited for this shape).
Bench methodology notes (both visible in the script header):
- Fixture generation runs in its own
prepareprocess; generating payloads inside the measuring process sets an RSS high-water of its own and masks the result. peakRssMBis sampled after a forcedgc()at every entry boundary. Without that, V8 lets uncollected payload garbage pile up until it next feels pressure and macOS never returns freed pages to the OS, so garbage accumulation is indistinguishable from residency (one-shot post-run RSS measured 62–70 MB on the thunk path — meaningless).wallMsincludes that gc time: compare it between the two impls, not withzip-stream-one(uncounted gc). It is noisy (208–351 ms thunk, 230–950 ms buffer); memory is the signal, as in prior RAM waves.
- Sync
readFileSyncper entry during streaming. Each entry does one blocking read +deflateRawSyncon the event loop. Fine for the CLI archiver (assembly is a serial tail phase; nothing else is being starved). An async (createReadStream) payload source would need the writer to accept stream entries — a larger change, noted as a future option, not needed for the win. - A payload file vanishing between write and assembly throws (ENOENT from the
thunk inside
encodeEntry), which rejectswriteZip's pipeline, removes the.part, and fails the archive loudly — a missing payload can never ship silently. The already-writtenpages//assets//sidecars remain inbookDirfor diagnosis (same failure surface as any mid-archive throw). Pinned by "a throwing thunk (payload file vanished) fails the write loudly" in test/ram-thunk.test.mjs. - Thunks must be pure (same bytes on every call): entriesFor page thunks are
pure closures; archive-read thunks close over a fixed
destpath. A reused entry list is encoded twice acrosszip()+writeZip()calls — safe for pure thunks; impure thunks were never supported. entriesForoutput now contains functiondatafor pages: callers who inspect entries (none in-repo;index.mjsonly re-exports) should resolve before use.
node --test: 63/63 pass (59 pre-existing + 4 new in test/ram-thunk.test.mjs), including test/e2e.test.mjs end-to-end (decoded page contents,unzip -ton the streamed EPUB, manifest hashes) and manifest-ref.- New pins: thunk vs buffer byte identity through
zip()ANDwriteZip(); exactly-once, at-encode-time thunk invocation; spine{data: thunk}output identical to{body}; loud failure with no.partleft. - Flake fixed in test/epub-integrity.test.mjs: the writeZip/zip byte-identity pin
built
entriesFor(book())twice;content.opfstampsdcterms:modifiedat whole-second precision, so a clock rollover between the two calls (observed ~1-in-10 runs) flipped the comparison. Both writers now consume one shared entry list — a strictly stronger pin. 30 consecutive full-suite runs green after. perf/zip-thunk-one.mjs buffer|thunkoutputs byte-identical (cmp), and the byte count matches wave2-w13's recordedcurrentBytesfor the same shape.
Merge-order safe: epub.mjs thunk support is purely additive for old callers; archive-read.mjs depends on it, so both commits land together (or epub first). No changes to files owned by other waves.