Skip to content

Commit ab7d9db

Browse files
committed
stream: fix sync write perf regression
While #31046 did make async writes faster it at the same time made sync writes slower. This PR corrects this while maintaining performance improvements. PR-URL: #33032 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Zeyu Yang <[email protected]>
1 parent 003fb53 commit ab7d9db

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

lib/_stream_writable.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -422,27 +422,24 @@ function onwrite(stream, er) {
422422
onwriteError(stream, state, er, cb);
423423
}
424424
} else {
425-
if (!state.destroyed) {
425+
if (state.buffered.length > state.bufferedIndex) {
426426
clearBuffer(stream, state);
427427
}
428-
if (state.needDrain || cb !== nop || state.ending || state.destroyed) {
429-
if (sync) {
430-
// It is a common case that the callback passed to .write() is always
431-
// the same. In that case, we do not schedule a new nextTick(), but
432-
// rather just increase a counter, to improve performance and avoid
433-
// memory allocations.
434-
if (state.afterWriteTickInfo !== null &&
435-
state.afterWriteTickInfo.cb === cb) {
436-
state.afterWriteTickInfo.count++;
437-
} else {
438-
state.afterWriteTickInfo = { count: 1, cb, stream, state };
439-
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
440-
}
428+
429+
if (sync) {
430+
// It is a common case that the callback passed to .write() is always
431+
// the same. In that case, we do not schedule a new nextTick(), but
432+
// rather just increase a counter, to improve performance and avoid
433+
// memory allocations.
434+
if (state.afterWriteTickInfo !== null &&
435+
state.afterWriteTickInfo.cb === cb) {
436+
state.afterWriteTickInfo.count++;
441437
} else {
442-
afterWrite(stream, state, 1, cb);
438+
state.afterWriteTickInfo = { count: 1, cb, stream, state };
439+
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
443440
}
444441
} else {
445-
state.pendingcb--;
442+
afterWrite(stream, state, 1, cb);
446443
}
447444
}
448445
}
@@ -490,7 +487,7 @@ function errorBuffer(state, err) {
490487

491488
// If there's something in the buffer waiting, then process it.
492489
function clearBuffer(stream, state) {
493-
if (state.corked || state.bufferProcessing) {
490+
if (state.corked || state.bufferProcessing || state.destroyed) {
494491
return;
495492
}
496493

0 commit comments

Comments
 (0)