stream: improve performance for sync write finishes · nodejs/node@ce6a865 · GitHub
Skip to content

Commit ce6a865

Browse files
addaleaxMylesBorins
authored andcommitted
stream: improve performance for sync write finishes
Improve performance and reduce memory usage when a writable stream is written to with the same callback (which is the most common case) and when the write operation finishes synchronously (which is also often the case). confidence improvement accuracy (*) (**) (***) streams/writable-manywrites.js sync='no' n=2000000 0.99 % ±3.20% ±4.28% ±5.61% streams/writable-manywrites.js sync='yes' n=2000000 *** 710.69 % ±19.65% ±26.47% ±35.09% Refs: #18013 Refs: #18367 PR-URL: #30710 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e624bb5 commit ce6a865

3 files changed

Lines changed: 66 additions & 8 deletions

File tree

benchmark/streams/writable-manywrites.js

Lines changed: 8 additions & 3 deletions

lib/_stream_writable.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ function WritableState(options, stream, isDuplex) {
137137
// The amount that is being written when _write is called.
138138
this.writelen = 0;
139139

140+
// Storage for data passed to the afterWrite() callback in case of
141+
// synchronous _write() completion.
142+
this.afterWriteTickInfo = null;
143+
140144
this.bufferedRequest = null;
141145
this.lastBufferedRequest = null;
142146

@@ -483,22 +487,41 @@ function onwrite(stream, er) {
483487
}
484488

485489
if (sync) {
486-
process.nextTick(afterWrite, stream, state, cb);
490+
// It is a common case that the callback passed to .write() is always
491+
// the same. In that case, we do not schedule a new nextTick(), but rather
492+
// just increase a counter, to improve performance and avoid memory
493+
// allocations.
494+
if (state.afterWriteTickInfo !== null &&
495+
state.afterWriteTickInfo.cb === cb) {
496+
state.afterWriteTickInfo.count++;
497+
} else {
498+
state.afterWriteTickInfo = { count: 1, cb, stream, state };
499+
process.nextTick(afterWriteTick, state.afterWriteTickInfo);
500+
}
487501
} else {
488-
afterWrite(stream, state, cb);
502+
afterWrite(stream, state, 1, cb);
489503
}
490504
}
491505
}
492506

493-
function afterWrite(stream, state, cb) {
507+
function afterWriteTick({ stream, state, count, cb }) {
508+
state.afterWriteTickInfo = null;
509+
return afterWrite(stream, state, count, cb);
510+
}
511+
512+
function afterWrite(stream, state, count, cb) {
494513
const needDrain = !state.ending && !stream.destroyed && state.length === 0 &&
495514
state.needDrain;
496515
if (needDrain) {
497516
state.needDrain = false;
498517
stream.emit('drain');
499518
}
500-
state.pendingcb--;
501-
cb();
519+
520+
while (count-- > 0) {
521+
state.pendingcb--;
522+
cb();
523+
}
524+
502525
finishMaybe(stream, state);
503526
}
504527

Lines changed: 30 additions & 0 deletions

0 commit comments

Comments
 (0)