Skip to content

Commit d9d8fbc

Browse files
committed
http: optimize IncomingMessage._dump
1 parent 2ea31e5 commit d9d8fbc

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

lib/_http_server.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const {
2525
ArrayIsArray,
26+
Boolean,
2627
Error,
2728
MathMin,
2829
NumberIsFinite,
@@ -32,6 +33,7 @@ const {
3233
Symbol,
3334
SymbolAsyncDispose,
3435
SymbolFor,
36+
globalThis,
3537
} = primordials;
3638

3739
const net = require('net');
@@ -1102,6 +1104,22 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
11021104
});
11031105
}
11041106

1107+
if (req.method === 'HEAD' || req.method === 'GET') {
1108+
// Fast dump where request "has" already emitted all lifecycle events.
1109+
// This avoid a lot of unnecessary overhead otherwise introduced by
1110+
// stream.Readable life cycle rules. The downside is that this will
1111+
// break some servers that read GET bodies.
1112+
1113+
req._dumped = true;
1114+
req._readableState.ended = true;
1115+
req._readableState.endEmitted = true;
1116+
req._readableState.destroyed = true;
1117+
req._readableState.closed = true;
1118+
req._readableState.closeEmitted = true;
1119+
1120+
req._read();
1121+
}
1122+
11051123
if (socket._httpMessage) {
11061124
// There are already pending outgoing res, append.
11071125
state.outgoing.push(res);

test/common/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const bits = ['arm64', 'loong64', 'mips', 'mipsel', 'ppc64', 'riscv64', 's390x',
3636
.includes(process.arch) ? 64 : 32;
3737
const hasIntl = !!process.config.variables.v8_enable_i18n_support;
3838

39+
globalThis.IS_NODE_TEST = true;
40+
3941
const {
4042
atob,
4143
btoa,

test/parallel/test-http-chunk-extensions-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const assert = require('assert');
124124
}));
125125

126126
sock.end('' +
127-
'GET / HTTP/1.1\r\n' +
127+
'PUT / HTTP/1.1\r\n' +
128128
`Host: localhost:${port}\r\n` +
129129
'Transfer-Encoding: chunked\r\n\r\n' +
130130
'2;' + 'A'.repeat(10000) + '=bar\r\nAA\r\n' +

test/parallel/test-http.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ const server = http.Server(common.mustCall((req, res) => {
5252
if (expectedRequests.length === 0)
5353
server.close();
5454

55-
req.on('end', () => {
55+
if (req.readableEnded) {
5656
res.writeHead(200, { 'Content-Type': 'text/plain' });
5757
res.write(`The path was ${url.parse(req.url).pathname}`);
5858
res.end();
59-
});
60-
req.resume();
59+
} else {
60+
req.on('end', () => {
61+
res.writeHead(200, { 'Content-Type': 'text/plain' });
62+
res.write(`The path was ${url.parse(req.url).pathname}`);
63+
res.end();
64+
});
65+
req.resume();
66+
}
6167
}, 3));
6268
server.listen(0);
6369

0 commit comments

Comments
 (0)