Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: fix sync write perf regression #33032

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member Author

@ronag ronag Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since clearBuffer is not inlineable this condition removes a lot of the overhead of calling into clearBuffer

clearBuffer(stream, state);
}
if (state.needDrain || cb !== nop || state.ending || state.destroyed) {
Copy link
Member Author

@ronag ronag Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition was an optimization which did not add much if any value and made the code more complex.

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