http2: reduce per-request allocations

Cut several sources of per-stream/per-request overhead on the hot
path:

- Track 'priority'/'frameError' stream listeners by overriding the
  EventEmitter methods on Http2Stream instead of subscribing to
  'newListener'/'removeListener', which made every listener add and
  remove on every stream emit an extra tracking event.
- Replace the per-call SafeSet and sensitive-header mapping in
  buildNgHeaderString with a lazily allocated array and an
  empty-array fast path, and skip the HTTP token regex and
  connection-specific header checks for well-known single-value
  header names.
- Replace per-call closures with shared named handlers in
  onStreamClose, afterShutdown and Http2Stream._destroy.
- Skip the pendingStreams Set add/delete for streams that are
  created with their native handle already available (all server
  streams).
- Hoist the per-request onStreamTimeout closure factories in the
  compat layer to module-level handlers, and avoid a once() wrapper
  allocation per server stream.

h2load, 1 KiB response payload, -c 4 -m 100, mean of 6 alternating
runs: core API 60.2k -> 69.3k req/s (+15%), compat API 43.6k ->
46.2k req/s (+5.9%).

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:
Matteo Collina authored and Antoine du Hamel committed 2026-08-04 18:40:43 +02:00
1 parent bce92debba
commit 3ed37153f8
2 files changed
+29 -18

No files matched your search

+8 -7
View File
@@ -300,11 +300,12 @@ function onStreamCloseRequest() {
req.emit('close');
}
function onStreamTimeout(kind) {
return function onStreamTimeout() {
const obj = this[kind];
obj.emit('timeout');
};
function onStreamTimeoutRequest() {
this[kRequest].emit('timeout');
}
function onStreamTimeoutResponse() {
this[kResponse].emit('timeout');
}
class Http2ServerRequest extends Readable {
@@ -332,7 +333,7 @@ class Http2ServerRequest extends Readable {
stream.on('error', onStreamError);
stream.on('aborted', onStreamAbortedRequest);
stream.on('close', onStreamCloseRequest);
stream.on('timeout', onStreamTimeout(kRequest));
stream.on('timeout', onStreamTimeoutRequest);
this.on('pause', onRequestPause);
this.on('resume', onRequestResume);
}
@@ -486,7 +487,7 @@ class Http2ServerResponse extends Stream {
stream.on('aborted', onStreamAbortedResponse);
stream.on('close', onStreamCloseResponse);
stream.on('wantTrailers', onStreamTrailersReady);
stream.on('timeout', onStreamTimeout(kResponse));
stream.on('timeout', onStreamTimeoutResponse);
}
// User land modules such as finalhandler just check truthiness of this
+21 -11
View File
@@ -770,14 +770,16 @@ function buildNgHeaderString(arrayOrMap,
let pseudoHeaders = '';
let count = 0;
const singles = new SafeSet();
let singles;
const sensitiveHeaders = arrayOrMap[kSensitiveHeaders] || emptyArray;
const neverIndex = sensitiveHeaders.map((v) => v.toLowerCase());
const neverIndex = sensitiveHeaders.length === 0 ?
emptyArray : sensitiveHeaders.map((v) => v.toLowerCase());
function processHeader(key, value) {
key = key.toLowerCase();
const isSingleValueField = kSingleValueFields.has(key);
const isStrictSingleValueField = strictSingleValueFields &&
kSingleValueFields.has(key);
isSingleValueField;
let isArray = ArrayIsArray(value);
if (isArray) {
switch (value.length) {
@@ -795,11 +797,15 @@ function buildNgHeaderString(arrayOrMap,
value = String(value);
}
if (isStrictSingleValueField) {
if (singles.has(key))
if (singles === undefined) {
singles = [key];
} else if (singles.includes(key)) {
throw new ERR_HTTP2_HEADER_SINGLE_VALUE(key);
singles.add(key);
} else {
singles.push(key);
}
}
const flags = neverIndex.includes(key) ?
const flags = neverIndex.length !== 0 && neverIndex.includes(key) ?
kNeverIndexFlag :
kNoHeaderFlags;
if (key[0] === ':') {
@@ -810,11 +816,15 @@ function buildNgHeaderString(arrayOrMap,
count++;
return;
}
if (!checkIsHttpToken(key)) {
throw new ERR_INVALID_HTTP_TOKEN('Header name', key);
}
if (isIllegalConnectionSpecificHeader(key, value)) {
throw new ERR_HTTP2_INVALID_CONNECTION_HEADERS(key);
// Well-known single-value fields are all valid HTTP tokens and none of
// them is a connection-specific header, so both checks can be skipped.
if (!isSingleValueField) {
if (!checkIsHttpToken(key)) {
throw new ERR_INVALID_HTTP_TOKEN('Header name', key);
}
if (isIllegalConnectionSpecificHeader(key, value)) {
throw new ERR_HTTP2_INVALID_CONNECTION_HEADERS(key);
}
}
if (isArray) {
for (let j = 0; j < value.length; ++j) {