← Back to Pull requests

perf: stream a generator's output to the generators that read it #1238

Closed
unknown closed this pull request without merging on Nov 13, 2026 16:45 UTC
View on GitHub
unknown unknown commented on Nov 03, 2026 19:34 UTC
Author

<!--
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.consume collected 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.

  • A generator can set streamsInput: true to 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.

  • The cache runs a streamed generator once, with createTee, which splits it into any number of readers like ReadableStream.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.md has a new Streaming Input section.
  • Why it's built this way

  • What's the tee? An async generator can only be iterated once, but a generator's output can have several readers: across the stack, metadata's is read by section-pages, json, orama-db and llms-txt. createTee in tee.mjs runs 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 from main's collect, which ran the generator once into one shared array: consume now collects one of the tee's readers, and stream hands one out. Earlier revisions of this PR called it the replay.
  • Why opt in with streamsInput? Streaming changes what input is, 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, html and html-all.
  • Why run a streamed generator once for all its readers? main already 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.
  • Why run eagerly instead of pulling, like 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 real createCache, 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 with share(). 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.

  • When does a chunk go? Only the link before a chunk holds it. Until every expected reader has asked for the result, the cache keeps the first link, so a reader that starts late still reads from chunk 0. After that, 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.
  • Why a linked list? It's Aviv's idea from 9d78f0d, a promise per step, but run eagerly and with chunks let go. It took the tee from 79 to 43 lines of code: no reader indexes to trim, and no signal to wake readers. It also fixes the edge kgeminicdev found: in the index version, a reader that was created but never iterated stayed at index 0, so no chunk could go until the generator ended. Now such a reader only keeps chunks alive while it is reachable itself.
  • Why not inline the tee into 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.
  • Why can the input be read only once? An async generator returns itself from Symbol.asyncIterator, so a second for await over the same input silently gets no chunks. With share() 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.
  • Why does one thread still get the whole input? Streaming has nothing to overlap with there. It would only interleave the stages and keep each stage's data alive at the same time. It's also why #1253 can't see this stack: with threads: 1 streaming is off, and CodSpeed's simulation mode counts instructions, not wall time, so it couldn't see overlap anyway. With -p 1 under --max-old-space-size=512, the stack up to #1242 passed all 11 site builds at ~1000MB peak. main hit 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.
  • Why is this PR flat on time? No generator sets streamsInput yet. 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 than main at 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 for main'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 for runGenerators (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 to main's.

    Related Issues

    Refs: #1205

    Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.

  • I have run node --run test and all tests passed.

  • I have check code formatting with node --run format:check & node --run lint.

  • I've covered new added functionality with unit tests if necessary.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: avivkeller Review duration: 14231m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%
    Repository