Skip to content

Commit 22717d1

Browse files
araujoguiaduh95
authored andcommitted
http: batch body spans per parser execute
Buffer body spans in on_body() and deliver them to JS once per execute() call instead of once per chunk. A body sent as many small chunks no longer costs one C++ to JS call and Buffer per chunk. Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: #67187 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent fd58188 commit 22717d1

3 files changed

Lines changed: 124 additions & 15 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
// Measures parsing a chunked request body as the chunk size shrinks.
4+
// The wire buffer is fed to the parser in 64 KiB slices, like socket reads.
5+
6+
const common = require('../common');
7+
8+
const bench = common.createBenchmark(main, {
9+
chunkSize: [1, 16, 256, 4096],
10+
len: [64 * 1024],
11+
n: [500],
12+
}, {
13+
flags: ['--expose-internals', '--no-warnings'],
14+
});
15+
16+
function main({ chunkSize, len, n }) {
17+
const { HTTPParser } = common.binding('http_parser');
18+
const REQUEST = HTTPParser.REQUEST;
19+
const kOnHeaders = HTTPParser.kOnHeaders | 0;
20+
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
21+
const kOnBody = HTTPParser.kOnBody | 0;
22+
const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
23+
const kMaxHeaderPairs = 2000;
24+
const kReadSize = 64 * 1024;
25+
26+
const chunk = `${chunkSize.toString(16)}\r\n${'x'.repeat(chunkSize)}\r\n`;
27+
const wire = Buffer.from(
28+
'POST / HTTP/1.1\r\nHost: localhost\r\nTransfer-Encoding: chunked\r\n\r\n' +
29+
chunk.repeat(Math.ceil(len / chunkSize)) +
30+
'0\r\n\r\n');
31+
32+
const reads = [];
33+
for (let i = 0; i < wire.length; i += kReadSize)
34+
reads.push(wire.subarray(i, i + kReadSize));
35+
36+
let bodyBytes = 0;
37+
const parser = new HTTPParser();
38+
parser.initialize(REQUEST, {}, 0, 0, undefined, kMaxHeaderPairs);
39+
parser[kOnHeaders] = function() {};
40+
parser[kOnHeadersComplete] = function() {};
41+
parser[kOnBody] = function(b) { bodyBytes += b.length; };
42+
parser[kOnMessageComplete] = function() {};
43+
44+
bench.start();
45+
for (let i = 0; i < n; i++) {
46+
for (const read of reads)
47+
parser.execute(read);
48+
parser.initialize(REQUEST, {}, 0, 0, undefined, kMaxHeaderPairs);
49+
}
50+
bench.end(n);
51+
52+
const expected = Math.ceil(len / chunkSize) * chunkSize * n;
53+
if (bodyBytes !== expected)
54+
throw new Error(`Expected ${expected} body bytes, got ${bodyBytes}`);
55+
}

‎src/node_http_parser.cc‎

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#include <cstdlib> // free()
3535
#include <cstring> // strdup(), strchr()
36-
36+
#include <vector>
3737

3838
// This is a binding to llhttp (https://github.com/nodejs/llhttp)
3939
// The goal is to decouple sockets from parsing for more javascript-level
@@ -84,6 +84,7 @@ const uint32_t kOnTimeout = 6;
8484
const size_t kMaxHeaderFieldsCount = 32;
8585
// Maximum size of chunk extensions
8686
const size_t kMaxChunkExtensionsSize = 16384;
87+
const size_t kMaxBufferedBodySize = 64 * 1024;
8788

8889
const uint32_t kLenientNone = 0;
8990
const uint32_t kLenientHeaders = 1 << 0;
@@ -519,36 +520,66 @@ class Parser : public AsyncWrap, public StreamListener {
519520
return static_cast<int>(val);
520521
}
521522

522-
523523
int on_body(const char* at, size_t length) {
524524
if (length == 0)
525525
return 0;
526526

527+
if (pending_body_len_ + length > kMaxBufferedBodySize && !FlushBody()) {
528+
llhttp_set_error_reason(&parser_, "HPE_JS_EXCEPTION:JS Exception");
529+
return HPE_USER;
530+
}
531+
532+
if (pending_body_len_ == 0) {
533+
pending_body_ = at;
534+
} else {
535+
if (body_buf_.empty()) {
536+
body_buf_.reserve(kMaxBufferedBodySize);
537+
body_buf_.assign(pending_body_, pending_body_ + pending_body_len_);
538+
}
539+
body_buf_.insert(body_buf_.end(), at, at + length);
540+
}
541+
pending_body_len_ += length;
542+
543+
return 0;
544+
}
545+
546+
bool FlushBody() {
547+
if (pending_body_len_ == 0) return true;
548+
549+
const char* data = body_buf_.empty() ? pending_body_ : body_buf_.data();
550+
size_t length = pending_body_len_;
551+
pending_body_len_ = 0;
552+
553+
if (got_exception_) {
554+
body_buf_.clear();
555+
return true;
556+
}
557+
527558
Environment* env = this->env();
528559
HandleScope handle_scope(env->isolate());
529560

530561
Local<Value> cb = CachedCallback(kOnBody, kOnBodyCallback);
562+
if (!cb->IsFunction()) {
563+
body_buf_.clear();
564+
return true;
565+
}
531566

532-
if (!cb->IsFunction())
533-
return 0;
534-
535-
Local<Value> buffer = Buffer::Copy(env, at, length).ToLocalChecked();
536-
537-
MaybeLocal<Value> r = MakeCallback(cb.As<Function>(), 1, &buffer);
567+
Local<Value> buffer = Buffer::Copy(env, data, length).ToLocalChecked();
568+
body_buf_.clear();
538569

539-
if (r.IsEmpty()) {
570+
if (MakeCallback(cb.As<Function>(), 1, &buffer).IsEmpty()) {
540571
got_exception_ = true;
541-
llhttp_set_error_reason(&parser_, "HPE_JS_EXCEPTION:JS Exception");
542-
return HPE_USER;
572+
return false;
543573
}
544574

545-
return 0;
575+
return true;
546576
}
547577

548-
549578
int on_message_complete() {
550579
HandleScope scope(env()->isolate());
551580

581+
if (!FlushBody()) return -1;
582+
552583
if (connectionsList_ != nullptr) {
553584
connectionsList_->PopActive(this);
554585
}
@@ -884,6 +915,9 @@ class Parser : public AsyncWrap, public StreamListener {
884915
Save();
885916
}
886917

918+
// Deliver buffered body spans while `data` is still alive.
919+
FlushBody();
920+
887921
// Calculate bytes read and resume after Upgrade/CONNECT pause
888922
size_t nread = len;
889923
if (err != HPE_OK) {
@@ -1139,6 +1173,9 @@ class Parser : public AsyncWrap, public StreamListener {
11391173
bool is_being_freed_ = false;
11401174
size_t current_buffer_len_;
11411175
const char* current_buffer_data_;
1176+
const char* pending_body_ = nullptr;
1177+
size_t pending_body_len_ = 0;
1178+
std::vector<char> body_buf_;
11421179
bool headers_completed_ = false;
11431180
size_t header_pairs_ = 0;
11441181
double max_header_pairs_ = -1;

‎test/parallel/test-http-parser.js‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,9 @@ function expectBody(expected) {
329329
assert.strictEqual(versionMinor, 1);
330330
});
331331

332+
// Chunks parsed in the same execute() call are delivered as one buffer.
332333
let body_part = 0;
333-
const body_parts = ['123', '123456', '1234567890'];
334+
const body_parts = ['1231234561234567890'];
334335

335336
const onBody = mustCallAtLeast((buf) => {
336337
const body = String(buf);
@@ -367,9 +368,10 @@ function expectBody(expected) {
367368
assert.strictEqual(versionMinor, 1);
368369
});
369370

371+
// One buffer per execute() call, holding all the chunks it parsed.
370372
let body_part = 0;
371373
const body_parts =
372-
['123', '123456', '123456789', '123456789ABC', '123456789ABCDEF'];
374+
['123123456', '123456789123456789ABC123456789ABCDEF'];
373375

374376
const onBody = mustCallAtLeast((buf) => {
375377
const body = String(buf);
@@ -560,6 +562,21 @@ function expectBody(expected) {
560562
parser.execute(req2, 0, req2.length);
561563
}
562564

565+
// Buffered body spans are flushed once they would exceed 64 KiB.
566+
{
567+
const chunk = `400\r\n${'x'.repeat(1024)}\r\n`;
568+
const request = Buffer.from(
569+
'POST / HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n' +
570+
chunk.repeat(100) + '0\r\n\r\n');
571+
572+
const sizes = [];
573+
const parser = newParser(REQUEST);
574+
parser[kOnBody] = (buf) => sizes.push(buf.length);
575+
parser.execute(request, 0, request.length);
576+
577+
assert.deepStrictEqual(sizes, [64 * 1024, 36 * 1024]);
578+
}
579+
563580
// Test parser 'this' safety
564581
// https://github.com/joyent/node/issues/6690
565582
assert.throws(function() {

0 commit comments

Comments
 (0)