Skip to content

Commit

Permalink
stream: fix sync write perf regression
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ronag committed Apr 23, 2020
1 parent d8c57cb commit bc93484
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,27 +421,24 @@ function onwrite(stream, er) {
onwriteError(stream, state, er, cb);
}
} else {
if (!state.destroyed) {
if (state.buffered.length > state.bufferedIndex) {
clearBuffer(stream, state);
}
if (state.needDrain || cb !== nop || state.ending || state.destroyed) {
if (sync) {
// It is a common case that the callback passed to .write() is always
// the same. In that case, we do not schedule a new nextTick(), but
// rather just increase a counter, to improve performance and avoid
// memory allocations.
if (state.afterWriteTickInfo !== null &&
state.afterWriteTickInfo.cb === cb) {
state.afterWriteTickInfo.count++;
} else {
state.afterWriteTickInfo = { count: 1, cb, stream, state };
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
}

if (sync) {
// It is a common case that the callback passed to .write() is always
// the same. In that case, we do not schedule a new nextTick(), but
// rather just increase a counter, to improve performance and avoid
// memory allocations.
if (state.afterWriteTickInfo !== null &&
state.afterWriteTickInfo.cb === cb) {
state.afterWriteTickInfo.count++;
} else {
afterWrite(stream, state, 1, cb);
state.afterWriteTickInfo = { count: 1, cb, stream, state };
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
}
} else {
state.pendingcb--;
afterWrite(stream, state, 1, cb);
}
}
}
Expand Down Expand Up @@ -489,7 +486,7 @@ function errorBuffer(state, err) {

// If there's something in the buffer waiting, then process it
function clearBuffer(stream, state) {
if (state.corked || state.bufferProcessing) {
if (state.corked || state.bufferProcessing || state.destroyed) {
return;
}

Expand Down

0 comments on commit bc93484

Please sign in to comment.