stream: reduce per-chunk overhead in streams and webstreams#64312
stream: reduce per-chunk overhead in streams and webstreams#64312anonrig wants to merge 2 commits into
Conversation
|
Review requested:
|
43e5e91 to
d4e4cf5
Compare
|
CI status summary for the last few runs, since the red X is misleading (each failure is a different platform, none reproduce, and none are stream-related):
Zero overlap between failing platforms across runs, and nodejs/reliability shows a 25.56% CI green rate with repo-wide infra errors ( Rather than burning another full 2h matrix on the lottery, could someone with Jenkins access resume-build just the failed |
|
@anonrig ... the commits, PR description, and responses to review comments here all have the appearance of being AI generated. However, there's no acknowledgement of that I can find (e.g. no |
@jasnell Ah yes. You're right to think that. Is that a new thing that I completely missed? Is it only required for pr descriptions or also for commits too? |
|
Ultimately, tho, it's not so much about defined policy as it is best practice. It's good to know when we're dealing with AI agent responses vs. an actual person. |
|
Will update commits now. Thanks for the explanation @jasnell |
9031bc2 to
e578439
Compare
fromList() re-read state.length and each chunk's length several times per call and re-loaded buffer[idx] around every copy, and read() loaded state.length three times in its read(0) check; the engine cannot fold these loads across the intervening copy and slice calls. Cache them in locals instead. Ported from Bun's fork of the same functions (src/js/internal/streams/readable.ts fromList/read), which carries these hoists on top of the shared readable-stream lineage. benchmark/compare.js against the unmodified baseline (60-run capture plus an independent 30-run repeat, Welch t-test): streams/readable-unevenread +0.65% (p=4.3e-4) / +0.59% (p=5.9e-3) and streams/pipe.js +0.74% (p=2.0e-4) / +0.52% (p=8.8e-3), with no significant regression across the captured streams benchmarks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/streams/readable.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build)
The [[queue]] backing every default readable/writable controller was a
plain array of { value, size } wrappers consumed with
ArrayPrototypeShift, so each buffered chunk allocated a wrapper object
and each dequeue moved (or forced the engine to re-linearize) the
remaining elements; the byte controller queue paid the same shift cost
for its chunk descriptor records.
Replace the array with a power-of-two ring buffer. Default controller
queues store each entry as (value, size) in two consecutive slots, so
the per-chunk wrapper allocation disappears; the byte controller keeps
its descriptor records (they are mutated in place at the head) in
single slots. Controllers start from (and are reset to) a shared
immutable empty queue, so constructing a stream allocates no queue
storage until a chunk is actually buffered. Enqueues measured by the
internal default size algorithm (never observable by user code, always
returns 1, cannot throw) skip the algorithm call and its try/catch
entirely.
The layout mirrors what Bun/WebKit use for the same spec structure:
[[queue]] as a ring-buffer deque (WTF::Deque in Bun's
src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring
buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm
bypass in
src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp.
benchmark/compare.js against the unmodified baseline (30-run capture
plus an independent 15-run repeat, Welch t-test, all p < 1e-5):
webstreams/pipe-to.js +12-17% across all sixteen high-water-mark
configurations, readable-read-buffered +20% (bufferSize=1) to +49%
(bufferSize=1000), readable-async-iterator +21%. No stable significant
regression across the rest of the webstreams suite: the creation.js and
readable-read.js deltas seen in the full-suite capture disappear in
isolated 60-run rechecks.
Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts
Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
Assisted-by: Grok (Grok Build)
e578439 to
9d0477c
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #64312 +/- ##
==========================================
+ Coverage 90.23% 90.26% +0.02%
==========================================
Files 741 741
Lines 240979 241143 +164
Branches 45401 45421 +20
==========================================
+ Hits 217449 217656 +207
+ Misses 15112 15074 -38
+ Partials 8418 8413 -5
🚀 New features to boost your workflow:
|
|
Landed in 3fc183f...1912893 |
fromList() re-read state.length and each chunk's length several times per call and re-loaded buffer[idx] around every copy, and read() loaded state.length three times in its read(0) check; the engine cannot fold these loads across the intervening copy and slice calls. Cache them in locals instead. Ported from Bun's fork of the same functions (src/js/internal/streams/readable.ts fromList/read), which carries these hoists on top of the shared readable-stream lineage. benchmark/compare.js against the unmodified baseline (60-run capture plus an independent 30-run repeat, Welch t-test): streams/readable-unevenread +0.65% (p=4.3e-4) / +0.59% (p=5.9e-3) and streams/pipe.js +0.74% (p=2.0e-4) / +0.52% (p=8.8e-3), with no significant regression across the captured streams benchmarks. Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/streams/readable.ts Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok (Grok Build) PR-URL: #64312 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
The [[queue]] backing every default readable/writable controller was a
plain array of { value, size } wrappers consumed with
ArrayPrototypeShift, so each buffered chunk allocated a wrapper object
and each dequeue moved (or forced the engine to re-linearize) the
remaining elements; the byte controller queue paid the same shift cost
for its chunk descriptor records.
Replace the array with a power-of-two ring buffer. Default controller
queues store each entry as (value, size) in two consecutive slots, so
the per-chunk wrapper allocation disappears; the byte controller keeps
its descriptor records (they are mutated in place at the head) in
single slots. Controllers start from (and are reset to) a shared
immutable empty queue, so constructing a stream allocates no queue
storage until a chunk is actually buffered. Enqueues measured by the
internal default size algorithm (never observable by user code, always
returns 1, cannot throw) skip the algorithm call and its try/catch
entirely.
The layout mirrors what Bun/WebKit use for the same spec structure:
[[queue]] as a ring-buffer deque (WTF::Deque in Bun's
src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring
buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm
bypass in
src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp.
benchmark/compare.js against the unmodified baseline (30-run capture
plus an independent 15-run repeat, Welch t-test, all p < 1e-5):
webstreams/pipe-to.js +12-17% across all sixteen high-water-mark
configurations, readable-read-buffered +20% (bufferSize=1) to +49%
(bufferSize=1000), readable-async-iterator +21%. No stable significant
regression across the rest of the webstreams suite: the creation.js and
readable-read.js deltas seen in the full-suite capture disappear in
isolated 60-run rechecks.
Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts
Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
Assisted-by: Grok (Grok Build)
PR-URL: #64312
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
The [[queue]] backing the controllers became a ring buffer, but the read and write request queues (readRequests, readIntoRequests, writeRequests) were left as plain arrays consumed with ArrayPrototypeShift, which is O(n) and, even for the single pending request of the await-each regime, far slower than an indexed head advance. Back them with the same Queue, materialized lazily from the shared empty queue so acquiring a reader or constructing a writer allocates no request storage until a read or write actually parks. pipe-to: +4.7% to +9.4% (all 16 configs, ***) readable-read type=byob: +2.3% (**) parked reader.read() loop: +15%, writer.write() loop: +11% (local harness) Follow-up to nodejs#64312. Signed-off-by: Matteo Collina <hello@matteocollina.com>
The [[queue]] backing the controllers became a ring buffer, but the read and write request queues (readRequests, readIntoRequests, writeRequests) were left as plain arrays consumed with ArrayPrototypeShift, which is O(n) and, even for the single pending request of the await-each regime, far slower than an indexed head advance. Back them with the same Queue, materialized lazily from the shared empty queue so acquiring a reader or constructing a writer allocates no request storage until a read or write actually parks. pipe-to: +4.7% to +9.4% (all 16 configs, ***) readable-read type=byob: +2.3% (**) parked read loop: +15%, write loop: +11% (local harness) Follow-up to nodejs#64312. Signed-off-by: Matteo Collina <hello@matteocollina.com>
Two independent, behavior-preserving performance changes to the streams
implementations, one per commit (intended to land separately via
commit-queue-rebase):1.
stream: hoist repeated loads in readable pathsfromList()re-readstate.lengthand each chunk'slengthseveral times percall and re-loaded
buffer[idx]around every copy, andread()loadedstate.lengththree times in itsread(0)check. The engine cannot fold theseloads across the intervening copy/slice calls, so they are cached in locals.
This mirrors what Bun's fork of the same functions carries on top of the shared
readable-stream lineage (
bun/src/js/internal/streams/readable.ts).2.
stream: use ring buffer for WHATWG stream queuesThe
[[queue]]backing every default readable/writable controller was a plainarray of
{ value, size }wrappers consumed withArrayPrototypeShift: onewrapper allocation per buffered chunk, and element movement (or engine
re-linearization) per dequeue. The byte controller queue paid the same shift
cost for its chunk-descriptor records.
This replaces the array with a power-of-two ring buffer:
(value, size)in twoconsecutive slots — the per-chunk wrapper allocation disappears;
head) in single slots;
constructing a stream allocates no queue storage until a chunk is actually
buffered;
by user code, always returns
1, cannot throw) skip the algorithm call andits
try/catch.The layout mirrors what Bun/WebKit use for the same spec structure
(
WTF::Dequein WebCore'sStreamQueue.h, the pure-JS ring buffer in Bun'ssrc/js/internal/fifo.ts).Benchmark results
benchmark/compare.jsagainst an unmodified-HEAD baseline binary on anotherwise idle machine, Welch t-test (Rscript unavailable, so computed from
the CSV; happy to share the raw captures). Full webstreams suite at 30 runs
plus an independent 15-run repeat; streams set at 60 runs plus a 30-run
repeat. Improvements below are stable-sign across both captures.
Ring buffer (webstreams), all p < 1e-5:
webstreams/pipe-to.js(all 16 HWM configs)webstreams/readable-read-buffered.js bufferSize=1webstreams/readable-read-buffered.js bufferSize=10/100webstreams/readable-read-buffered.js bufferSize=1000webstreams/readable-async-iterator.jsReadable hoists (node:stream):
streams/readable-unevenread.jsstreams/pipe.jsNo statistically significant regression across the rest of the captured
streams/webstreams benchmarks (creation, js_transfer, readable-read,
boundaryread, bigread, uint8array, pipe-object-mode); two apparent full-suite
deltas (
creation kind='ReadableStream.tee',readable-read type='normal')disappear in isolated 60-run re-checks (p=0.50 / p=0.95) and are attributable
to thermal drift in the suite-ordered run.
Correctness
state behind
kState.test/parallel/test-stream*,test-whatwg-*stream*,test-webstream*, andtest/wpt/test-streams.jsall pass; fulltest/sequentialpasses.test-webstreams-queue-wraparound.jsdrives ringgrowth/wrap-around, drain-rewind/shrink, fractional/zero
size()accounting, BYOB partial in-place head consumption, and writable
backpressure drain through the public API only (it also passes on the
unpatched binary).
AI assistance disclosure
This is an AI-assisted pull request. The survey of Bun's implementation,
the ports/implementation, the benchmark methodology (baseline builds,
benchmark/compare.jscaptures, Welch t-test analysis, isolated re-runs forsuspected noise), the regression test, and drafts of the review-thread
responses were produced by an AI agent (Grok, xAI) operating under my
direction. I reviewed and take responsibility for every change; all landing
decisions, approvals, and force-pushes were human-authorized. Raw benchmark
CSVs and the analysis are available on request.