Skip to content

Commit e07c4ff

Browse files
ronagBethGriggs
authored andcommitted
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 <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: Zeyu Yang <himself65@outlook.com>
1 parent 2bb4ac4 commit e07c4ff

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

lib/_stream_writable.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -415,27 +415,24 @@ function onwrite(stream, er) {
415415
onwriteError(stream, state, er, cb);
416416
}
417417
} else {
418-
if (!state.destroyed) {
418+
if (state.buffered.length > state.bufferedIndex) {
419419
clearBuffer(stream, state);
420420
}
421-
if (state.needDrain || cb !== nop || state.ending || state.destroyed) {
422-
if (sync) {
423-
// It is a common case that the callback passed to .write() is always
424-
// the same. In that case, we do not schedule a new nextTick(), but
425-
// rather just increase a counter, to improve performance and avoid
426-
// memory allocations.
427-
if (state.afterWriteTickInfo !== null &&
428-
state.afterWriteTickInfo.cb === cb) {
429-
state.afterWriteTickInfo.count++;
430-
} else {
431-
state.afterWriteTickInfo = { count: 1, cb, stream, state };
432-
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
433-
}
421+
422+
if (sync) {
423+
// It is a common case that the callback passed to .write() is always
424+
// the same. In that case, we do not schedule a new nextTick(), but
425+
// rather just increase a counter, to improve performance and avoid
426+
// memory allocations.
427+
if (state.afterWriteTickInfo !== null &&
428+
state.afterWriteTickInfo.cb === cb) {
429+
state.afterWriteTickInfo.count++;
434430
} else {
435-
afterWrite(stream, state, 1, cb);
431+
state.afterWriteTickInfo = { count: 1, cb, stream, state };
432+
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
436433
}
437434
} else {
438-
state.pendingcb--;
435+
afterWrite(stream, state, 1, cb);
439436
}
440437
}
441438
}
@@ -483,7 +480,7 @@ function errorBuffer(state, err) {
483480

484481
// If there's something in the buffer waiting, then process it.
485482
function clearBuffer(stream, state) {
486-
if (state.corked || state.bufferProcessing) {
483+
if (state.corked || state.bufferProcessing || state.destroyed) {
487484
return;
488485
}
489486

0 commit comments

Comments
 (0)