mirror of
https://github.com/nodejs/node.git
synced 2026-11-21 12:59:16 -05:00
http2: avoid copying the options in respond()
respond() copied the user-provided options object on every call just so it could normalize and locally flip options.endStream, and prepareResponseHeadersObject() then looked the :status and date fields up again on the dictionary-mode null-prototype headers copy it had just built. Use a local variable for endStream and pick up :status/date while copying the headers instead. No measurable throughput change on its own; this removes an object clone and several dictionary-mode property lookups per response. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/64265 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Tim Perry <pimterry@gmail.com>
This commit is contained in:
1 parent
351da9c1b1
commit
5e47f2b468
1 file changed
+20
-15
+20
-15
@@ -2813,31 +2813,35 @@ function prepareResponseHeaders(stream, headersParam, options) {
|
||||
function prepareResponseHeadersObject(oldHeaders, options) {
|
||||
assertIsObject(oldHeaders, 'headers', ['Object', 'Array']);
|
||||
const headers = { __proto__: null };
|
||||
let statusCode;
|
||||
let hasDate = false;
|
||||
|
||||
if (oldHeaders !== null && oldHeaders !== undefined) {
|
||||
// This loop is here for performance reason. Do not change.
|
||||
// The :status and date fields are picked up while copying so they do
|
||||
// not have to be looked up again on the null-prototype copy.
|
||||
for (const key in oldHeaders) {
|
||||
if (ObjectHasOwn(oldHeaders, key)) {
|
||||
headers[key] = oldHeaders[key];
|
||||
const value = oldHeaders[key];
|
||||
headers[key] = value;
|
||||
if (key === HTTP2_HEADER_STATUS)
|
||||
statusCode = value;
|
||||
else if (key === HTTP2_HEADER_DATE)
|
||||
hasDate = value != null;
|
||||
}
|
||||
}
|
||||
headers[kSensitiveHeaders] = oldHeaders[kSensitiveHeaders];
|
||||
}
|
||||
|
||||
const statusCode =
|
||||
headers[HTTP2_HEADER_STATUS] =
|
||||
headers[HTTP2_HEADER_STATUS] | 0 || HTTP_STATUS_OK;
|
||||
statusCode = headers[HTTP2_HEADER_STATUS] = statusCode | 0 || HTTP_STATUS_OK;
|
||||
|
||||
if (options.sendDate == null || options.sendDate) {
|
||||
headers[HTTP2_HEADER_DATE] ??= utcDate();
|
||||
if (!hasDate && (options.sendDate == null || options.sendDate)) {
|
||||
headers[HTTP2_HEADER_DATE] = utcDate();
|
||||
}
|
||||
|
||||
validatePreparedResponseHeaders(headers, statusCode);
|
||||
|
||||
return {
|
||||
headers,
|
||||
statusCode: headers[HTTP2_HEADER_STATUS],
|
||||
};
|
||||
return { headers, statusCode };
|
||||
}
|
||||
|
||||
function prepareResponseHeadersArray(headers, options) {
|
||||
@@ -3222,15 +3226,17 @@ class ServerHttp2Stream extends Http2Stream {
|
||||
const state = this[kState];
|
||||
|
||||
assertIsObject(options, 'options');
|
||||
options = { ...options };
|
||||
// The options are only read, never mutated, so the user-provided object
|
||||
// can be used directly instead of copying it.
|
||||
options ??= kEmptyObject;
|
||||
|
||||
debugStreamObj(this, 'initiating response');
|
||||
this[kUpdateTimer]();
|
||||
|
||||
options.endStream = !!options.endStream;
|
||||
const endStream = !!options.endStream;
|
||||
|
||||
let streamOptions = 0;
|
||||
if (options.endStream)
|
||||
if (endStream)
|
||||
streamOptions |= STREAM_OPTION_EMPTY_PAYLOAD;
|
||||
|
||||
if (options.waitForTrailers) {
|
||||
@@ -3253,12 +3259,11 @@ class ServerHttp2Stream extends Http2Stream {
|
||||
|
||||
// Close the writable side if the endStream option is set or status
|
||||
// is one of known codes with no payload, or it's a head request
|
||||
if (!!options.endStream ||
|
||||
if (endStream ||
|
||||
statusCode === HTTP_STATUS_NO_CONTENT ||
|
||||
statusCode === HTTP_STATUS_RESET_CONTENT ||
|
||||
statusCode === HTTP_STATUS_NOT_MODIFIED ||
|
||||
this.headRequest === true) {
|
||||
options.endStream = true;
|
||||
this.end();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user