Skip to content

Commit ee68b0d

Browse files
committed
stream: improve write performance
1 parent e13a37e commit ee68b0d

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

lib/_stream_writable.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -493,30 +493,30 @@ function onwrite(stream, er) {
493493
onwriteError(stream, state, er, cb);
494494
}
495495
} else {
496-
// Check if we're actually ready to finish, but don't emit yet
497-
var finished = needFinish(state) || stream.destroyed;
498-
499-
if (!finished &&
500-
!state.corked &&
496+
if (!state.corked &&
501497
!state.bufferProcessing &&
502498
state.bufferedRequest) {
503499
clearBuffer(stream, state);
504500
}
505501

506-
if (sync) {
507-
// It is a common case that the callback passed to .write() is always
508-
// the same. In that case, we do not schedule a new nextTick(), but rather
509-
// just increase a counter, to improve performance and avoid memory
510-
// allocations.
511-
if (state.afterWriteTickInfo !== null &&
512-
state.afterWriteTickInfo.cb === cb) {
513-
state.afterWriteTickInfo.count++;
502+
if (state.ending || state.needDrain || cb !== nop) {
503+
if (sync) {
504+
// It is a common case that the callback passed to .write() is always
505+
// the same. In that case, we do not schedule a new nextTick(), but
506+
// rather just increase a counter, to improve performance and avoid
507+
// memory allocations.
508+
if (state.afterWriteTickInfo !== null &&
509+
state.afterWriteTickInfo.cb === cb) {
510+
state.afterWriteTickInfo.count++;
511+
} else {
512+
state.afterWriteTickInfo = { count: 1, cb, stream, state };
513+
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
514+
}
514515
} else {
515-
state.afterWriteTickInfo = { count: 1, cb, stream, state };
516-
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
516+
afterWrite(stream, state, 1, cb);
517517
}
518518
} else {
519-
afterWrite(stream, state, 1, cb);
519+
state.pendingcb--;
520520
}
521521
}
522522
}

0 commit comments

Comments
 (0)