Skip to content

Commit e85a819

Browse files
committed
stream: avoid tick in writable hot path
1 parent d37b0d2 commit e85a819

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

lib/internal/streams/writable.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ function Writable(options) {
396396
destroyImpl.construct(this, () => {
397397
const state = this._writableState;
398398

399-
if (!state.writing) {
399+
if ((state.state & kWriting) === 0) {
400400
clearBuffer(this, state);
401401
}
402402

@@ -612,24 +612,30 @@ function onwrite(stream, er) {
612612
}
613613

614614
if (sync) {
615+
const needDrain = (state.state & kNeedDrain) !== 0 && state.length === 0;
616+
const needTick = needDrain || (state.state & kDestroyed !== 0) || cb !== nop;
617+
615618
// It is a common case that the callback passed to .write() is always
616619
// the same. In that case, we do not schedule a new nextTick(), but
617620
// rather just increase a counter, to improve performance and avoid
618621
// memory allocations.
619622
if (cb === nop) {
620-
if ((state.state & kAfterWritePending) === 0) {
623+
if ((state.state & kAfterWritePending) === 0 && needTick) {
621624
process.nextTick(afterWrite, stream, state, 1, cb);
622625
state.state |= kAfterWritePending;
623626
} else {
624627
state.pendingcb -= 1;
625628
}
626-
} else if (state.afterWriteTickInfo !== null &&
627-
state.afterWriteTickInfo.cb === cb) {
628-
state.afterWriteTickInfo.count++;
629+
} else if ((state.state & kAfterWriteTickInfo) !== 0 &&
630+
state[kAfterWriteTickInfoValue].cb === cb) {
631+
state[kAfterWriteTickInfoValue].count++;
632+
} else if (needTick) {
633+
state[kAfterWriteTickInfoValue] = { count: 1, cb, stream, state };
634+
process.nextTick(afterWriteTick, state[kAfterWriteTickInfoValue]);
635+
state.state |= (kAfterWritePending | kAfterWriteTickInfo);
629636
} else {
630-
state.afterWriteTickInfo = { count: 1, cb, stream, state };
631-
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
632-
state.state |= kAfterWritePending;
637+
state.pendingcb--;
638+
finishMaybe(stream, state, true);
633639
}
634640
} else {
635641
afterWrite(stream, state, 1, cb);
@@ -638,7 +644,8 @@ function onwrite(stream, er) {
638644
}
639645

640646
function afterWriteTick({ stream, state, count, cb }) {
641-
state.afterWriteTickInfo = null;
647+
state.state &= ~kAfterWriteTickInfo;
648+
state[kAfterWriteTickInfoValue] = null;
642649
return afterWrite(stream, state, count, cb);
643650
}
644651

@@ -795,6 +802,8 @@ Writable.prototype.end = function(chunk, encoding, cb) {
795802
if (typeof cb === 'function') {
796803
if (err) {
797804
process.nextTick(cb, err);
805+
} else if ((state.state & kErrored) !== 0) {
806+
process.nextTick(cb, state[kErroredValue]);
798807
} else if ((state.state & kFinished) !== 0) {
799808
process.nextTick(cb, null);
800809
} else {

0 commit comments

Comments
 (0)