perf: stream a generator's output to the generators that read it #1238
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1238"
general
weight: 0.35
avivkeller
<!--
Please read the Code of Conduct and the Contributing Guidelines before opening a pull request.
-->
Description
This PR lets the cache stream a generator's output to the generators that read it, instead of collecting every chunk first. It's the second part of a stack that splits #1205.
Every dependency edge was a full barrier:
cache.consumecollected all of a generator's chunks before its dependent started, so each stage of a build waited for the whole of the one before it.streamsInput: trueto get its dependency's output as an async iterable of chunks, read as they're produced. With one thread nothing runs alongside, so it still gets one array, and a dependency that doesn't stream arrives as a single chunk. No generator sets it yet.createTee, which splits it into any number of readers likeReadableStream.prototype.tee(): it buffers the chunks in a linked list as they come, and each reader reads all of them, from the first, while the generator still runs. Once every reader has started, a chunk is let go as soon as all of them have read it, rather than when the last one is done.docs/creating-generators.mdhas a new Streaming Input section.Why it's built this way
metadata's is read bysection-pages,json,orama-dbandllms-txt.createTeeintee.mjsruns the generator once on the main thread, buffers its chunks as they come, and gives each reader its own position from chunk 0. It isn't a layer on top of the cache. It takes over frommain'scollect, which ran the generator once into one shared array:consumenow collects one of the tee's readers, andstreamhands one out. Earlier revisions of this PR called it the replay.streamsInput? Streaming changes whatinputis, and plenty of generators need all of it before they can do anything. With the opt-in, every existing generator gets the same collected array it gets today, and so does anyone's custom one. Across the whole stack, five generators opt in:metadata,section-pages,jsx-ast,htmlandhtml-all.mainalready runs a generator once and shares its collected array with every dependent. The tee keeps that when some of those readers stream, so no generator runs twice.share()in 9d78f0d?share()keeps a promise for every step in an array that each reader holds, and nothing clears it. So even a single streaming reader keeps its whole input alive until it's done. I measured both on the realcreateCache, with 40 chunks of 4MB and a producer at 2ms per chunk (with the index version of the tee; the linked list lets go of the same chunks):- One reader: at most 1 chunk alive with the tee (4MB), against 40 with
share()(153MB).- A fast and a slow reader: 26 against 40 (99MB against 153MB).
Pulling also doesn't start the upstream until the reader's first
next(), and that only comes after the reader has loaded its Markdown plugins and its worker. With 100ms of setup, the producer finished at 142ms with the tee and at 232ms withshare(). The cost of running eagerly: a reader slower than its producer leaves chunks buffered (25 of 40 at peak with a 5ms reader), and the buffer shrinks again as it catches up.forget()drops it and each chunk goes once the slowest reader has passed it. A reader that collects keeps everything anyway, because that's what collecting does.caching.mjs? It's 43 lines of code, and as its own module it gets its own tests: the ones that check which chunks get garbage collected need nothing of the cache, and the cache's tests only cover how the cache uses it. Most of this PR is tests: 438 of its 703 added lines.Symbol.asyncIterator, so a secondfor awaitover the same input silently gets no chunks. Withshare()the two reads got 40 chunks, then 0. The chunks this reader already read can't be given to it again, so a second read throws instead.threads: 1streaming is off, and CodSpeed's simulation mode counts instructions, not wall time, so it couldn't see overlap anyway. With-p 1under--max-old-space-size=512, the stack up to #1242 passed all 11 site builds at ~1000MB peak.mainhit V8's out-of-memory abort in 1 of 8 runs and the commit before #1237 in 3 of 5, so passing still depends on GC timing, as #1179 found.streamsInputyet. The only change that runs is collected outputs now going through the tee, and the numbers below show nothing measurable on a full build. The gains come once #1240 and #1241 opt in: with #1241, the site build is 17% faster thanmainat 4 threads and 20% at 6, for +26% and +24% peak memory.Benchmarks
Full Node.js docs on Node 26, against
main(which has #1237). Each number is the median of 5 interleaved runs (4 for both builds of this branch at 6 threads, and formain's legacy build at 6). Peak memory is RSS summed over the whole process tree. Site is-t orama-db -t json -t llms-txt -t section-pages; legacy is-t legacy-html-all -t legacy-json-all.| Build | Threads |
main| This PR | Time | Memory ||---|---|---|---|---|---|
| site | 4 | 9.28s, 1739MB | 9.26s, 1749MB | 0% | +1% |
| site | 6 | 8.26s, 2365MB | 8.39s, 2304MB | +2% | -3% |
| legacy | 4 | 4.42s, 1529MB | 4.40s, 1578MB | -1% | +3% |
| legacy | 6 | 4.20s, 1952MB | 4.21s, 2033MB | 0% | +4% |
All of this is within the noise between runs.
main's own peaks ranged from 1505 to 1583MB on the legacy build at 4 threads, from 1932 to 2057MB on legacy at 6, and from 2304 to 2371MB on the site build at 6.The linked list against the old index version, measured on #1241 (before its manifest fix) where something streams, everything else equal:
| Threads | Index version | Linked list | Time | Memory |
|---|---|---|---|---|
| 4 | 8.24s, 2408MB | 8.07s, 2240MB | -2% | -7% |
| 6 | 7.34s, 2755MB | 7.39s, 2752MB | +1% | 0% |
At 6 threads a second round of 7 runs gave the linked list 6.91s, 2758MB against 7.00s, 2734MB, so no change there either.
Validation
Added tests for
createTee(chunks every reader has read let go while it still runs, and the last one once they're done, every reader gets every chunk in order, a chunk let go before a reader read it, a failure, and no unhandled rejection when nothing reads it), for the cache (one run for streaming and collecting readers, a non-streaming result as one chunk, running a stream before it's read and counting it as a read, a stream read only once, and chunks let go once every reader has read them while the generator still runs), with the garbage collection helpers both test files use in a shared fixture, and forrunGenerators(streaming to a generator that streams its input while its source still runs, a failing dependency read as a stream, and giving it the whole input with one thread). Built on 12 of Node.js's API docs at 1 and 4 threads, the output is byte-identical tomain's.Related Issues
Refs: #1205
Check List
node --run testand all tests passed.node --run format:check&node --run lint.