mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
http2: avoid per-write closures in kWriteGeneric
Every _write()/_writev() on an Http2Stream allocated four closures and an anonymous nextTick callback to coordinate the write callback with the end-of-stream check. Since the stream machinery dispatches at most one write at a time, that coordination state can live on the stream's kState object instead, with shared named functions for the end check and completion logic. When trailers are pending the writable side cannot be shut down early anyway, so the end-of-stream check tick is now skipped entirely for those writes. Also pre-initialize the kState fields that used to be added dynamically (shutdownWritableCalled, fd) so hot-path stores no longer transition the object shape. h2load, 1 KiB response payload, -c 4 -m 100, mean of 6 alternating runs vs main: core API 61.0k -> 70.7k req/s (+15.9% cumulative), compat API 43.7k -> 50.4k req/s (+15.3% cumulative). Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/64265 Backport-PR-URL: https://github.com/nodejs/node/pull/64663 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
This commit is contained in:
1 parent
157bd07b6e
commit
f11ac0da37
1 file changed
+71
-35
+71
-35
@@ -1991,6 +1991,47 @@ function shutdownWritable(callback) {
|
||||
return afterShutdown.call(req, 0);
|
||||
}
|
||||
|
||||
// Completes one of the two halves of a dispatched write (the write callback
|
||||
// itself and the end-of-stream check); the stream machinery callback runs
|
||||
// once both have finished. The state lives on stream[kState] because only a
|
||||
// single write may be in flight at any given time.
|
||||
function finishWrite(stream) {
|
||||
const state = stream[kState];
|
||||
if (--state.writePending !== 0)
|
||||
return;
|
||||
const cb = state.writeCb;
|
||||
state.writeCb = null;
|
||||
const err = aggregateTwoErrors(state.endErr, state.writeErr);
|
||||
state.writeErr = null;
|
||||
state.endErr = null;
|
||||
// writeGeneric does not destroy on error and
|
||||
// we cannot enable autoDestroy,
|
||||
// so make sure to destroy on error.
|
||||
if (err) {
|
||||
stream.destroy(err);
|
||||
}
|
||||
cb(err);
|
||||
}
|
||||
|
||||
// Runs on the tick after a write was dispatched: if the write turned out to
|
||||
// be the last chunk of an ending writable, shut the writable side down right
|
||||
// away so the final DATA frame can include the END_STREAM flag.
|
||||
function endCheckNT(stream) {
|
||||
const state = stream[kState];
|
||||
if (state.writeErr ||
|
||||
!stream._writableState.ending ||
|
||||
stream._writableState.buffered.length ||
|
||||
(state.flags & STREAM_FLAGS_HAS_TRAILERS)) {
|
||||
finishWrite(stream);
|
||||
return;
|
||||
}
|
||||
debugStreamObj(stream, 'shutting down writable on last write');
|
||||
shutdownWritable.call(stream, (err) => {
|
||||
state.endErr = err;
|
||||
finishWrite(stream);
|
||||
});
|
||||
}
|
||||
|
||||
function finishSendTrailers(stream, headersList) {
|
||||
// The stream might be destroyed and in that case
|
||||
// there is nothing to do.
|
||||
@@ -2098,6 +2139,12 @@ class Http2Stream extends Duplex {
|
||||
writeQueueSize: 0,
|
||||
trailersReady: false,
|
||||
endAfterHeaders: false,
|
||||
writeCb: null,
|
||||
writeErr: null,
|
||||
endErr: null,
|
||||
writePending: 0,
|
||||
shutdownWritableCalled: false,
|
||||
fd: -1,
|
||||
};
|
||||
|
||||
// Fields used by the compat API to avoid megamorphisms.
|
||||
@@ -2285,45 +2332,34 @@ class Http2Stream extends Duplex {
|
||||
if (!this.headersSent)
|
||||
this[kProceed]();
|
||||
|
||||
let req;
|
||||
// The stream machinery dispatches at most one _write()/_writev() at a
|
||||
// time, so the coordination state between the write callback and the
|
||||
// end-of-stream check below can live on the stream state instead of
|
||||
// being captured by per-write closures.
|
||||
const state = this[kState];
|
||||
state.writeCb = cb;
|
||||
state.writeErr = null;
|
||||
state.endErr = null;
|
||||
|
||||
let waitingForWriteCallback = true;
|
||||
let waitingForEndCheck = true;
|
||||
let writeCallbackErr;
|
||||
let endCheckCallbackErr;
|
||||
const done = () => {
|
||||
if (waitingForEndCheck || waitingForWriteCallback) return;
|
||||
const err = aggregateTwoErrors(endCheckCallbackErr, writeCallbackErr);
|
||||
// writeGeneric does not destroy on error and
|
||||
// we cannot enable autoDestroy,
|
||||
// so make sure to destroy on error.
|
||||
if (err) {
|
||||
this.destroy(err);
|
||||
}
|
||||
cb(err);
|
||||
};
|
||||
if (state.flags & STREAM_FLAGS_HAS_TRAILERS) {
|
||||
// Trailers are pending, so the writable side cannot be shut down
|
||||
// early anyway; there is no point in scheduling the end check.
|
||||
state.writePending = 1;
|
||||
} else {
|
||||
state.writePending = 2;
|
||||
// Shutdown write stream right after last chunk is sent
|
||||
// so final DATA frame can include END_STREAM flag
|
||||
process.nextTick(endCheckNT, this);
|
||||
}
|
||||
|
||||
// This is invoked both as a method on the write req and as a plain
|
||||
// call, so the stream has to be captured here.
|
||||
const writeCallback = (err) => {
|
||||
waitingForWriteCallback = false;
|
||||
writeCallbackErr = err;
|
||||
done();
|
||||
state.writeErr = err;
|
||||
finishWrite(this);
|
||||
};
|
||||
const endCheckCallback = (err) => {
|
||||
waitingForEndCheck = false;
|
||||
endCheckCallbackErr = err;
|
||||
done();
|
||||
};
|
||||
// Shutdown write stream right after last chunk is sent
|
||||
// so final DATA frame can include END_STREAM flag
|
||||
process.nextTick(() => {
|
||||
if (writeCallbackErr ||
|
||||
!this._writableState.ending ||
|
||||
this._writableState.buffered.length ||
|
||||
(this[kState].flags & STREAM_FLAGS_HAS_TRAILERS))
|
||||
return endCheckCallback();
|
||||
debugStreamObj(this, 'shutting down writable on last write');
|
||||
shutdownWritable.call(this, endCheckCallback);
|
||||
});
|
||||
|
||||
let req;
|
||||
if (writev)
|
||||
req = writevGeneric(this, data, writeCallback);
|
||||
else
|
||||
|
||||
Reference in new issue
Block a user