Skip to content

Commit e4f8389

Browse files
committed
http: fast-forward teardown of unread messages
When a response finishes and the incoming message was fully received but never read, _dump() no longer goes through resume(): read(0) at EOF schedules the 'end' emission directly, skipping the resume_ tick, the 'resume' emit and the flow() machinery. Reduces the per-request nextTick count of a hello-world HTTP server from 7 to 6 and CPU per request by ~4%. Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent 6cff903 commit e4f8389

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

lib/_http_incoming.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,15 @@ IncomingMessage.prototype._dump = function _dump() {
512512
// If there is buffered data, it may trigger 'data' events.
513513
// Remove 'data' event listeners explicitly.
514514
this.removeAllListeners('data');
515-
this.resume();
515+
const state = this._readableState;
516+
if (state.ended && !state.destroyed && state.length === 0) {
517+
// The message was fully received and never read: there is nothing to
518+
// pull off the wire. Go straight to the 'end' emission instead of
519+
// paying for the resume() and flow() scheduling machinery.
520+
this.read(0);
521+
} else {
522+
this.resume();
523+
}
516524
}
517525
};
518526

0 commit comments

Comments
 (0)