Repository navigation
stream: speed up WHATWG web streams - #65273
Conversation
|
Review requested:
|
6a3f89b to
c796760
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65273 +/- ##
==========================================
- Coverage 90.52% 90.51% -0.02%
==========================================
Files 805 805
Lines 285669 285753 +84
Branches 55151 55160 +9
==========================================
+ Hits 258616 258654 +38
- Misses 17173 17222 +49
+ Partials 9880 9877 -3
🚀 New features to boost your workflow:
|
|
Defensively marking this semver-major. If you can show that the optimization does not change observable behavior, that can be dropped, but the change in microtask timing from one pull to the next is likely observable. |
|
@jasnell I believe |
|
Benchmark GHA (webstreams): https://github.com/nodejs/node/actions/runs/31808780686 |
27c4cf3 to
1cf7319
Compare
|
@jasnell can you rereview please |
|
|
||
| // Materialize the deferred default controller for `new ReadableStream()`. | ||
| // started is true immediately: start is a no-op and there is no initial pull. | ||
| function ensureEmptyDefaultController(stream) { |
There was a problem hiding this comment.
Is this correctly handled in subclasses? A subclass could end up calling cancel, getReader, etc before the constructor finishes, causing the controller to be materialized. Worth documenting and tests.
There was a problem hiding this comment.
Looks okay to me? As explained in anonrig's previous comment, the controller is materialized in all those methods:
cancelcallsensureEmptyDefaultControllerdirectlygetReadercalls it indirectly throughsetupReadableStreamDefaultReaderpipeTo/tee/valuescreate an internal reader, so they also go throughsetupReadableStreamDefaultReader
Subclasses don't really affect this: if a subclass wanted to get access to the controller, they'd still need to pass a source object with a start/pull method, which pushes them off the empty-argument path.
jasnell
left a comment
There was a problem hiding this comment.
Some additional review comments. Will review again once merge conflicts are resolved and I'd like @MattiasBuelens to review before this proceeds.
| } | ||
|
|
||
| // Materialize the deferred default controller for `new ReadableStream()`. | ||
| // started is true immediately: start is a no-op and there is no initial pull. |
There was a problem hiding this comment.
I'm actually impressed that this still passes WPT? 😅 I know I've found a lot of subtle issues in the tests where the behavior changed slightly depending on whether or not the test waits for the stream to be started.
| return; | ||
| } | ||
| if (isReadableStreamDefaultController(controller)) | ||
| controller.error(error); |
There was a problem hiding this comment.
Off-topic, but this should really call the abstract op instead of going through a method lookup.
| controller.error(error); | |
| readableStreamDefaultControllerError(controller, error); |
|
|
||
| // Materialize the deferred default controller for `new ReadableStream()`. | ||
| // started is true immediately: start is a no-op and there is no initial pull. | ||
| function ensureEmptyDefaultController(stream) { |
There was a problem hiding this comment.
Looks okay to me? As explained in anonrig's previous comment, the controller is materialized in all those methods:
cancelcallsensureEmptyDefaultControllerdirectlygetReadercalls it indirectly throughsetupReadableStreamDefaultReaderpipeTo/tee/valuescreate an internal reader, so they also go throughsetupReadableStreamDefaultReader
Subclasses don't really affect this: if a subclass wanted to get access to the controller, they'd still need to pass a source object with a start/pull method, which pushes them off the empty-argument path.
ad88fa7 to
08914f5
Compare
08914f5 to
cea5388
Compare
MattiasBuelens
left a comment
There was a problem hiding this comment.
Code looks okay. I don't think this is semver-major anymore.
That said, since the scope of this PR is now reduced, the PR description should reflect that.
7d5e7f3 to
83a8457
Compare
Rebase the remaining construction-path work onto current main. The shared hot-path reductions (raw pull/write callbacks, parked promise results) already landed, and so did the immediate-start fast path for sources without pull(). This keeps what is still left: - Defer the default controller for `new ReadableStream()` until getReader(), cancel(), or an error on a readable stream. - Skip validateObject on empty WritableStream and TransformStream construction, and create the writable AbortController lazily. - Share isNonThenable() for start/pull/write results. Public writer.ready, writer.closed, and cancel() promises stay distinct. Assisted-by: Cursor Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com>
83a8457 to
e1e6759
Compare
Commit Queue failedThe pull request was removed from the Commit Queue and labeled
commit-queue-failed
Full Commit Queue output |
|
Landed in 63c39dc |

Leftover construction-path work on
node:stream/webafter #65138 landed the shared hot-path reductions (thenAlgorithmResult,kResolvedPromise, raw pull/write callbacks,parkOnReady). Rebased onto currentmain, including #65143 (transform backpressure decoupling). That PR's rawdefaultTransformAlgorithm/createRawCallback2Paramspath is kept.The earlier body described work that is no longer in this tree (
pipeTosync-fill, write-queue drain, nativewebstreamsbinding, sharedkResolvedPromiseon public slots). This is what remains.What remains
ReadableStreamcontroller.new ReadableStream()with no source/strategy skipsvalidateObjectand does not allocate a default controller untilgetReader(),cancel()(only whilereadable),pipeTo(),tee(), orvalues().createReadableStreamState()initializescontroller: undefinedso empty and non-empty streams share the same hidden class. Interoperror()on an empty stream marks it errored without materializing a controller. Subclasses that call those methods aftersuper()still get a controller before the subclass constructor finishes; passing a source leaves this path and creates the controller duringsuper()as usual.WritableStream/TransformStreamconstruction. SkipvalidateObjecton the shared empty sentinels. The writableAbortControlleris created lazily and materialized onabort()or firstcontroller.signalaccess, so a signal observed after abort is still aborted with that reason.isNonThenable()stays in JS. Shared helper for start/pull/write results.null/ primitives take the allocation-freekResolvedPromisereaction; objects and functions stay on the maybe-thenable path (no observable.thenlookup). There is no nativewebstreamsbinding.Review follow-up (removed)
Addressed #65273 (comment):
readableStreamDefaultControllerFillSyncis gone (one pull per microtask)writer.ready/writer.closed/cancel()mint distinct promises (no sharedkResolvedPromise)asyncwrappers so thenable results keep the previous microtask countinternalBinding('webstreams')removed;cloneAsUint8Arrayis still the JSArrayBuffer.prototype.slicepath (detached buffers stayTypeError)Semver
The earlier
semver-majorconcerns were the removed fillSync / drain / shared-promise / timing-helper changes. Public constructors, methods, and WHATWG Streams behavior (backpressure, BYOB, pipeTo, tee, errors, transfer) are intended to be unchanged. @MattiasBuelens noted the remaining scope does not look semver-major.Benchmarks
The numbers previously posted here were measured against a base that predates #65138 and included the removed optimizations. They are not this PR's remaining delta. A full
webstreams/compare against currentmainstill needs to be posted.Tests
test/wpt/test-streams.jstest/parallel/test-whatwg-readable*,writable*,transform*,webstreams*,test-webstreams*,test-global-webstreams.jstest/parallel/test-whatwg-webstreams-hotpath.js(publicread()/pipeTo, deferred controller / subclass constructors,isNonThenable/ Proxy,cloneAsUint8Arrayedge cases, abort-before-signal, distinctwriter.ready/writer.closed/cancel()promises)test-whatwg-writablestream.jsAI assistance
This change was developed with assistance from Grok. I reviewed, tested, and take responsibility for the submitted code.