Skip to content

Commit cf882a7

Browse files
authored
stream: fold desired-size check into WHATWG backpressure update
writableStreamUpdateBackpressure ran two release-mode assertions and took a precomputed backpressure value that every caller derived with a separate GetBackpressure/GetDesiredSize call pair. All callers already hold the 'writable', not-close-queued invariant the assertions checked, so derive the backpressure from the desired size inside the function and drop the assertions and the helper. This runs up to twice per write. writer.write() loop: +10%, pipe-to: +4.6% avg over 16 configs (local harness / benchmark, all configs positive). Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #64451 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 7569219 commit cf882a7

1 file changed

Lines changed: 12 additions & 21 deletions

File tree

lib/internal/webstreams/writablestream.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -757,13 +757,15 @@ function writableStreamClose(stream) {
757757
return promise;
758758
}
759759

760-
function writableStreamUpdateBackpressure(stream, backpressure) {
761-
const streamState = stream[kState];
762-
assert(streamState.state === 'writable');
763-
assert(!streamState.closeQueuedOrInFlight);
764-
const {
765-
writer,
766-
} = streamState;
760+
// Callers invoke this only in the 'writable' state with no close queued or
761+
// in flight (the two conditions the spec's assertions check), so the
762+
// backpressure value is derived here from the desired size instead of being
763+
// computed and passed in by each caller.
764+
function writableStreamUpdateBackpressure(controller, streamState) {
765+
const controllerState = controller[kState];
766+
const backpressure =
767+
controllerState.highWaterMark - controllerState.queueTotalSize <= 0;
768+
const writer = streamState.writer;
767769
if (writer !== undefined && streamState.backpressure !== backpressure) {
768770
if (backpressure) {
769771
// The spec replaces [[readyPromise]] with a fresh pending promise;
@@ -1100,9 +1102,7 @@ function writableStreamDefaultControllerWrite(controller, chunk, chunkSize) {
11001102
const streamState = stream[kState];
11011103
if (!streamState.closeQueuedOrInFlight &&
11021104
streamState.state === 'writable') {
1103-
writableStreamUpdateBackpressure(
1104-
stream,
1105-
writableStreamDefaultControllerGetBackpressure(controller));
1105+
writableStreamUpdateBackpressure(controller, streamState);
11061106
}
11071107
writableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
11081108
}
@@ -1128,9 +1128,7 @@ function writableStreamDefaultControllerProcessWrite(controller, chunk) {
11281128
dequeueValue(controller);
11291129
if (!streamState.closeQueuedOrInFlight &&
11301130
state === 'writable') {
1131-
writableStreamUpdateBackpressure(
1132-
stream,
1133-
writableStreamDefaultControllerGetBackpressure(controller));
1131+
writableStreamUpdateBackpressure(controller, streamState);
11341132
}
11351133
writableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
11361134
};
@@ -1229,10 +1227,6 @@ function writableStreamDefaultControllerClearAlgorithms(controller) {
12291227
controller[kState].sizeAlgorithm = undefined;
12301228
}
12311229

1232-
function writableStreamDefaultControllerGetBackpressure(controller) {
1233-
return writableStreamDefaultControllerGetDesiredSize(controller) <= 0;
1234-
}
1235-
12361230
function writableStreamDefaultControllerAdvanceQueueIfNeeded(controller) {
12371231
const {
12381232
queue,
@@ -1317,9 +1311,7 @@ function setupWritableStreamDefaultController(
13171311
};
13181312
stream[kState].controller = controller;
13191313

1320-
writableStreamUpdateBackpressure(
1321-
stream,
1322-
writableStreamDefaultControllerGetBackpressure(controller));
1314+
writableStreamUpdateBackpressure(controller, stream[kState]);
13231315

13241316
const startResult = startAlgorithm();
13251317

@@ -1402,7 +1394,6 @@ module.exports = {
14021394
writableStreamDefaultControllerError,
14031395
writableStreamDefaultControllerClose,
14041396
writableStreamDefaultControllerClearAlgorithms,
1405-
writableStreamDefaultControllerGetBackpressure,
14061397
writableStreamDefaultControllerAdvanceQueueIfNeeded,
14071398
setupWritableStreamDefaultControllerFromSink,
14081399
setupWritableStreamDefaultController,

0 commit comments

Comments
 (0)