Skip to content

Commit bc93484

Browse files
committed
stream: fix sync write perf regression
While nodejs#31046 did make async writes faster it at the same time made sync writes slower. This PR corrects this while maintaining performance improvements.
1 parent d8c57cb commit bc93484

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
@@ -421,27 +421,24 @@ function onwrite(stream, er) {
421421
onwriteError(stream, state, er, cb);
422422
}
423423
} else {
424-
if (!state.destroyed) {
424+
if (state.buffered.length > state.bufferedIndex) {
425425
clearBuffer(stream, state);
426426
}
427-
if (state.needDrain || cb !== nop || state.ending || state.destroyed) {
428-
if (sync) {
429-
// It is a common case that the callback passed to .write() is always
430-
// the same. In that case, we do not schedule a new nextTick(), but
431-
// rather just increase a counter, to improve performance and avoid
432-
// memory allocations.
433-
if (state.afterWriteTickInfo !== null &&
434-
state.afterWriteTickInfo.cb === cb) {
435-
state.afterWriteTickInfo.count++;
436-
} else {
437-
state.afterWriteTickInfo = { count: 1, cb, stream, state };
438-
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
439-
}
427+
428+
if (sync) {
429+
// It is a common case that the callback passed to .write() is always
430+
// the same. In that case, we do not schedule a new nextTick(), but
431+
// rather just increase a counter, to improve performance and avoid
432+
// memory allocations.
433+
if (state.afterWriteTickInfo !== null &&
434+
state.afterWriteTickInfo.cb === cb) {
435+
state.afterWriteTickInfo.count++;
440436
} else {
441-
afterWrite(stream, state, 1, cb);
437+
state.afterWriteTickInfo = { count: 1, cb, stream, state };
438+
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
442439
}
443440
} else {
444-
state.pendingcb--;
441+
afterWrite(stream, state, 1, cb);
445442
}
446443
}
447444
}
@@ -489,7 +486,7 @@ function errorBuffer(state, err) {
489486

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

0 commit comments

Comments
 (0)