stream: avoid per-chunk promises in webstream adapters - #65548
Open
mcollina wants to merge 1 commit into
Open
Conversation
Readable.fromWeb() and the read side of Duplex.fromWeb() allocated a
promise, a read-result object, and two reaction closures for every
chunk through reader.read(). Only one read is ever in flight, so a
single reused read request delivers chunks through
readableStreamDefaultReaderRead() instead, forwarding each chunk in a
microtask to keep the previous delivery order relative to errors and
destroy.
Writable.fromWeb() and the write side of Duplex.fromWeb() paid two
derived promises off writer.ready plus the writer.write() promise and
a fresh closure pair per chunk. A single shared write request (the
same contract pipeTo uses) now dispatches chunks directly and settles
the node callback, with failures delivered in a microtask because the
callback can destroy the stream while the writable machinery is
mid-transition.
Also add benchmark/webstreams/adapters.js; the suite had no rows for
the adapter paths.
confidence improvement accuracy (*) (**) (***)
webstreams/adapters.js kind='readable-from-web' n=100000 * 5.07 % ±4.84% ±6.44% ±8.39%
webstreams/adapters.js kind='readable-to-web' n=100000 0.92 % ±6.25% ±8.32% ±10.83%
webstreams/adapters.js kind='writable-from-web' n=100000 *** 39.27 % ±7.19% ±9.58% ±12.48%
webstreams/adapters.js kind='writable-to-web' n=100000 -1.74 % ±6.71% ±8.94% ±11.63%
Signed-off-by: Matteo Collina <hello@matteocollina.com>
Collaborator
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65548 +/- ##
==========================================
- Coverage 90.13% 90.12% -0.01%
==========================================
Files 751 751
Lines 253639 253714 +75
Branches 47790 47797 +7
==========================================
+ Hits 228618 228663 +45
- Misses 16264 16309 +45
+ Partials 8757 8742 -15
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Continuing the WHATWG streams optimization work, this round targets the stream/webstream adapters (
Readable.fromWeb(),Writable.fromWeb(),Duplex.fromWeb()), which had never been profiled.Readable.fromWeb()and the read side ofDuplex.fromWeb()allocated a promise, a read-result object, and two reaction closures for every chunk throughreader.read(). Only one read is ever in flight (_readis not called again beforepush()), so a single reused read request now delivers chunks throughreadableStreamDefaultReaderRead(), forwarding each chunk in a microtask to keep the previous delivery order relative to errors and destroy.Writable.fromWeb()and the write side ofDuplex.fromWeb()paid two derived promises offwriter.readyplus thewriter.write()promise and a fresh closure pair per chunk. A single shared write request (the same contractpipeTouses since #64890) now dispatches chunks directly and settles the node callback. Failures are delivered in a microtask because the callback can destroy the stream while the writable machinery is mid-transition.Also adds
benchmark/webstreams/adapters.js— the suite had no rows for the adapter paths.Results (30 runs):
The two
toWebrows are untouched paths and neutral, included for coverage. Beyond the test suite and WPT, the change was validated with a differential stress harness (error mid-write, writev with cork, erroring controller, destroy during data, pre-closed/pre-errored streams, duplex echo, slow-sink backpressure): the observable event logs are byte-identical to the previous implementation.