stream: drain WHATWG stream queues from a head index#64354
Open
mcollina wants to merge 2 commits into
Open
Conversation
Skip the size algorithm call and its result validation when the size algorithm is the default one, and stop re-running the full ShouldCallPull predicate at per-chunk call sites where its inputs are already established. readable-async-iterator type=normal: +16.6% (***) readable-read-buffered: +13.9% to +27.5% (**/***) pipe-to: +3.7% to +6.0% (15/16 configs significant) Signed-off-by: Matteo Collina <hello@matteocollina.com>
The default readable and writable controller queues were drained with ArrayPrototypeShift, which is O(queue length); reading a buffered queue one chunk at a time is therefore O(n^2). Dequeue through a moving head index instead so each read is O(1), dropping the consumed prefix in amortized O(1) once it reaches half of the backing array (past a small floor, so a short oscillating queue never pays a per-dequeue array mutation). readable-read-buffered: +14.8% to +35.4% (**/***) readable-async-iterator type=normal: +17.8% (***) pipe-to: +12.9% to +18.1% (all 16 configs ***) Signed-off-by: Matteo Collina <hello@matteocollina.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #64354 +/- ##
==========================================
- Coverage 90.24% 90.23% -0.01%
==========================================
Files 741 741
Lines 240976 241059 +83
Branches 45408 45419 +11
==========================================
+ Hits 217477 217530 +53
- Misses 15070 15099 +29
- Partials 8429 8430 +1
🚀 New features to boost your workflow:
|
| // each dequeue O(1); the consumed prefix is dropped in amortized O(1) once | ||
| // it grows to at least half of the backing array (and past a small floor, | ||
| // so a short oscillating queue never pays a per-dequeue array mutation). | ||
| // Callers must treat `queue.length - queueHead`, not `queue.length`, as the |
Contributor
There was a problem hiding this comment.
Perhaps we should have a helper function for that?
function queueLength(controller) {
const state = controller[kState];
return state.queue.length - state.queueHead;
}Or does that not get inlined as aggressively as we'd want?
jasnell
reviewed
Jul 8, 2026
| } else if (!controller[kState].closeRequested && | ||
| controller[kState].started && | ||
| controller[kState].highWaterMark - | ||
| controller[kState].queueTotalSize > 0) { |
Member
There was a problem hiding this comment.
This is repeated more than once and is complicated enough to move into a utility function.
jasnell
reviewed
Jul 8, 2026
| if (stream[kState].state === 'readable') { | ||
| if (isReadableStreamDefaultController(controller)) { | ||
| if (controller[kState].queue.length > 0) { | ||
| if (controller[kState].queueHead < controller[kState].queue.length) { |
Member
There was a problem hiding this comment.
Likewise this... worth moving into a utility function
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.
Drain the default readable and writable controller queues through a moving head index instead of
ArrayPrototypeShift(which is O(queue length), making a buffered drain O(n^2)). Each dequeue becomes O(1); the consumed prefix is dropped in amortized O(1).The first commit is #64320; only the last commit is new here.