mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
stream: fix sync write perf regression
While https://github.com/nodejs/node/pull/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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
@@ -422,27 +422,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -490,7 +487,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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user