Skip to content

stream: speed up WHATWG web streams - #65273

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:stream-speed-up-webstreams
Nov 29, 2026
Merged

nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:stream-speed-up-webstreams

Conversation

@anonrig

@anonrig anonrig commented Aug 13, 2026 •

Copy link
Copy Markdown
Member

Leftover construction-path work on node:stream/web after #65138 landed the shared hot-path reductions (thenAlgorithmResult, kResolvedPromise, raw pull/write callbacks, parkOnReady). Rebased onto current main, including #65143 (transform backpressure decoupling). That PR's raw defaultTransformAlgorithm / createRawCallback2Params path is kept.

The earlier body described work that is no longer in this tree (pipeTo sync-fill, write-queue drain, native webstreams binding, shared kResolvedPromise on public slots). This is what remains.

What remains

  • Deferred empty ReadableStream controller. new ReadableStream() with no source/strategy skips validateObject and does not allocate a default controller until getReader(), cancel() (only while readable), pipeTo(), tee(), or values(). createReadableStreamState() initializes controller: undefined so empty and non-empty streams share the same hidden class. Interop error() on an empty stream marks it errored without materializing a controller. Subclasses that call those methods after super() still get a controller before the subclass constructor finishes; passing a source leaves this path and creates the controller during super() as usual.
  • Cheaper empty WritableStream / TransformStream construction. Skip validateObject on the shared empty sentinels. The writable AbortController is created lazily and materialized on abort() or first controller.signal access, 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-free kResolvedPromise reaction; objects and functions stay on the maybe-thenable path (no observable .then lookup). There is no native webstreams binding.

Review follow-up (removed)

Addressed #65273 (comment):

  • readableStreamDefaultControllerFillSync is gone (one pull per microtask)
  • pipeTo write-queue drain loop is gone (one write completion per microtask)
  • writer.ready / writer.closed / cancel() mint distinct promises (no shared kResolvedPromise)
  • cancel/close/abort/flush/transform keep async wrappers so thenable results keep the previous microtask count
  • native internalBinding('webstreams') removed; cloneAsUint8Array is still the JS ArrayBuffer.prototype.slice path (detached buffers stay TypeError)

Semver

The earlier semver-major concerns 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 current main still needs to be posted.

Tests

  • test/wpt/test-streams.js
  • test/parallel/test-whatwg-readable*, writable*, transform*, webstreams*, test-webstreams*, test-global-webstreams.js
  • new test/parallel/test-whatwg-webstreams-hotpath.js (public read() / pipeTo, deferred controller / subclass constructors, isNonThenable / Proxy, cloneAsUint8Array edge cases, abort-before-signal, distinct writer.ready / writer.closed / cancel() promises)
  • abort-before-signal case in test-whatwg-writablestream.js

AI assistance

This change was developed with assistance from Grok. I reviewed, tested, and take responsibility for the submitted code.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/gyp
@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Aug 13, 2026
@anonrig
anonrig force-pushed the stream-speed-up-webstreams branch from 6a3f89b to c796760 Compare August 13, 2026 23:23
@codecov

codecov Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.51%. Comparing base (c58ccfa) to head (e1e6759).
⚠️ Report is 38 commits behind head on main.

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     
Files with missing lines Coverage Δ
lib/internal/webstreams/readablestream.js 98.47% <100.00%> (+0.02%) ⬆️
lib/internal/webstreams/transformstream.js 98.83% <100.00%> (+<0.01%) ⬆️
lib/internal/webstreams/util.js 98.08% <100.00%> (+0.03%) ⬆️
lib/internal/webstreams/writablestream.js 99.48% <100.00%> (+<0.01%) ⬆️

... and 25 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Comment thread lib/internal/webstreams/util.js Resolved
Comment thread lib/internal/webstreams/util.js Resolved
Comment thread lib/internal/webstreams/util.js Resolved
Comment thread src/node_webstreams.cc Resolved
@jasnell jasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Aug 14, 2026
@jasnell

jasnell commented Aug 14, 2026

Copy link
Copy Markdown
Member

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.

@anonrig anonrig added the commit-queue-squash PRs the Commit Queue should land as one squashed commit. label Aug 14, 2026
@anonrig
anonrig requested a review from jasnell August 14, 2026 14:08
@anonrig

anonrig commented Aug 14, 2026

Copy link
Copy Markdown
Member Author

@jasnell I believe semver-major is no longer needed.

@github-actions

Copy link
Copy Markdown
Contributor
@anonrig
anonrig force-pushed the stream-speed-up-webstreams branch 2 times, most recently from 27c4cf3 to 1cf7319 Compare August 14, 2026 18:09
@anonrig

anonrig commented Aug 16, 2026

Copy link
Copy Markdown
Member Author

@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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay to me? As explained in anonrig's previous comment, the controller is materialized in all those methods:

  • cancel calls ensureEmptyDefaultController directly
  • getReader calls it indirectly through setupReadableStreamDefaultReader
  • pipeTo/tee/values create an internal reader, so they also go through setupReadableStreamDefaultReader

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 jasnell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off-topic, but this should really call the abstract op instead of going through a method lookup.

Suggested change
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay to me? As explained in anonrig's previous comment, the controller is materialized in all those methods:

  • cancel calls ensureEmptyDefaultController directly
  • getReader calls it indirectly through setupReadableStreamDefaultReader
  • pipeTo/tee/values create an internal reader, so they also go through setupReadableStreamDefaultReader

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.

@cursor
cursor Bot force-pushed the stream-speed-up-webstreams branch from ad88fa7 to 08914f5 Compare August 18, 2026 01:26
@jasnell

jasnell commented Aug 18, 2026

Copy link
Copy Markdown
Member

Note: Bots are not permitted to attest with Signed-off-by
image

@MattiasBuelens MattiasBuelens left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cursor
cursor Bot force-pushed the stream-speed-up-webstreams branch 2 times, most recently from 7d5e7f3 to 83a8457 Compare August 21, 2026 00:55
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>
@cursor
cursor Bot force-pushed the stream-speed-up-webstreams branch from 83a8457 to e1e6759 Compare November 28, 2026 01:54

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@anonrig anonrig added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 29, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 29, 2026
@anonrig anonrig added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 29, 2026
@nodejs-github-bot nodejs-github-bot added commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. and removed commit-queue PRs queued for automated landing through the Commit Queue. labels Nov 29, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Commit Queue failed

   ⚠  Could not retrieve the email or name of the PR's author from user's GitHub profile!
   ✖  semver-major requires at least 2 TSC approvals

The pull request was removed from the Commit Queue and labeled commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. . After resolving the failure, remove that label and add commit-queue PRs queued for automated landing through the Commit Queue. to retry.

Full Commit Queue output
�[36m⠋�[39m Loading data for nodejs/node/pull/65273
�[36m⠋�[39m Loading data for nodejs/node/pull/65273
�[36m⠋�[39m Getting collaborator contacts from README of nodejs/node
�[36m⠋�[39m Getting PR from nodejs/node/pull/65273
�[36m⠋�[39m Getting reviews from nodejs/node/pull/65273
�[36m⠋�[39m Getting comments from nodejs/node/pull/65273
�[36m⠋�[39m Getting commits from nodejs/node/pull/65273
✔  Done loading data for nodejs/node/pull/65273
----------------------------------- PR info ------------------------------------
Title      stream: speed up WHATWG web streams (#65273)
   ⚠  Could not retrieve the email or name of the PR's author from user's GitHub profile!
Branch     anonrig:stream-speed-up-webstreams -> nodejs:main
Labels     semver-major, lib / src, needs-ci, commit-queue, commit-queue-squash
Commits    1
 - stream: speed up WHATWG web streams
Committers 1
 - Yagiz Nizipli <yagiz@nizipli.com>
PR-URL: https://github.com/nodejs/node/pull/65273
Reviewed-By: Mattias Buelens <mattias@buelens.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/65273
Reviewed-By: Mattias Buelens <mattias@buelens.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Thu, 13 Aug 2026 23:22:25 GMT
   ✔  Approvals: 2
   ✔  - Mattias Buelens (@MattiasBuelens): https://github.com/nodejs/node/pull/65273#pullrequestreview-4999206618
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/65273#pullrequestreview-6096453168
   ✖  semver-major requires at least 2 TSC approvals
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2026-11-29T04:20:57Z: https://ci.nodejs.org/job/node-test-pull-request/79583/
�[36m⠙�[39m Querying data for job/node-test-pull-request/79583/
�[36m⠙�[39m Querying data for job/node-test-pull-request/79583/
�[36m⠙�[39m Querying API for job/node-test-pull-request/79583/
✔  Build data downloaded
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu

View workflow run

@panva panva added commit-queue PRs queued for automated landing through the Commit Queue. and removed commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. labels Nov 29, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 63c39dc into nodejs:main Nov 29, 2026
72 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 63c39dc

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit-queue-squash PRs the Commit Queue should land as one squashed commit. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. semver-major PRs that contain breaking changes and should be released in the next major version.

6 participants