mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 18:25:13 +08:00
The parser read the `maxHeaderPairs` property from its JS object in C++ once per header section to enforce the header count limit. That lookup is a runtime property load for every parsed request, and costs up to 7% on the parser benchmark for requests with few headers. Pass the limit to `initialize()` instead and keep it in a field. The property is still set, as the JS side uses it to trim the header list. Refs: https://github.com/nodejs/node/pull/64988 Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/66250 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Xuguang Mei <meixuguang@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
len: [4, 8, 16, 32],
|
|
n: [1e5],
|
|
}, {
|
|
flags: ['--expose-internals', '--no-warnings'],
|
|
});
|
|
|
|
function main({ len, n }) {
|
|
const { HTTPParser } = common.binding('http_parser');
|
|
const REQUEST = HTTPParser.REQUEST;
|
|
const kOnHeaders = HTTPParser.kOnHeaders | 0;
|
|
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
|
|
const kOnBody = HTTPParser.kOnBody | 0;
|
|
const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
|
|
const kMaxHeaderPairs = 2000;
|
|
|
|
function processHeader(header, n) {
|
|
const parser = newParser(REQUEST);
|
|
|
|
bench.start();
|
|
for (let i = 0; i < n; i++) {
|
|
parser.execute(header, 0, header.length);
|
|
parser.initialize(REQUEST, {}, 0, 0, undefined, kMaxHeaderPairs);
|
|
}
|
|
bench.end(n);
|
|
}
|
|
|
|
function newParser(type) {
|
|
const parser = new HTTPParser();
|
|
// Direct parsers bypass cleanParser(); use its production default.
|
|
parser.initialize(type, {}, 0, 0, undefined, kMaxHeaderPairs);
|
|
|
|
parser.headers = [];
|
|
|
|
parser[kOnHeaders] = function() { };
|
|
parser[kOnHeadersComplete] = function() { };
|
|
parser[kOnBody] = function() { };
|
|
parser[kOnMessageComplete] = function() { };
|
|
|
|
return parser;
|
|
}
|
|
|
|
let header = `GET /hello HTTP/1.1\r\nContent-Type: text/plain\r\n`;
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
header += `X-Filler${i}: ${Math.random().toString(36).substring(2)}\r\n`;
|
|
}
|
|
header += '\r\n';
|
|
|
|
processHeader(Buffer.from(header), n);
|
|
}
|