GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — http,http2: speed up header validation and write coalescing by anonrig · Pull Request #65332 · nodejs/node Conversation
|
| config | main | this PR | delta |
|---|---|---|---|
| buffer, len=4, chunked, c=50 | 84.6k req/s | 91.2k | +7.9% |
| bytes, len=4, chunked, c=50 | 78.2k | 78.3k | +0.1% |
| buffer, len=1024, chunked, c=50 | 73.4k | 77.4k | +5.4% |
| bytes, len=1024, content-length, c=50 | 77.0k | 81.4k | +5.8% |
check_is_http_token for Connection is about +21%. Header-value
validation stays on regex (a JS Uint8Array scan was 30–50% slower).
The packed-header follow-up keeps bytes off the JS string heap until
something actually reads rawHeaders / headers. A per-request native
BaseObject was slower than creating the string array, so the bytes live
in one Buffer with flag bits for Host / Expect / body headers.
checkInvalidHeaderChar cases in test/parallel/test-http-common.js.test/parallel/test-http-chunked-end-coalesce.js checks the wireend() (string, buffer, UTF-8, trailers,Content-Length casing, latin1 header values).test/parallel/test-http-native-headers.js checks lazy Host / Expectreq.headers access.Local: 405/405 test/parallel/test-http-* (HTTP/1) passed against a
release build of the packed-headers tree. Earlier 776/776
test-http* / test-https* / test-http2* passed on the same branch
before the Buffer packing tweak.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Review requested:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
76c8e14 to
62327c4
Compare
|
Local results from a release build of this branch vs Correctness
Micros (3 runs, ops/s) A JS lookup table for all token lengths and for header-value checks was a regression (V8 regex is faster past ~10 bytes). Latest commit keeps regex for values and for tokens longer than 10, and uses the table for
HTTP/2 So the measurable end-to-end win is the small- |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This PR optimizes Node.js HTTP/1.1 and HTTP/2 hot paths around header validation/serialization and write coalescing to reduce per-request overhead in writeHead()/end() and small-body responses.
Changes:
toLowerCase(), and added header/body write coalescing (including a combined chunked end() write).req.headers construction when checking for Content-Length / Transfer-Encoding by scanning rawHeaders.Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/parallel/test-http-response-cork.js | Updates expectations to allow fewer socket.write() calls due to new coalescing behavior. |
| test/parallel/test-http-common.js | Adds more checkInvalidHeaderChar() test cases for strict vs lenient validation. |
| test/parallel/test-http-chunked-end-coalesce.js | New test validating wire format when chunked res.end() is coalesced into a single write. |
| src/node_http2.cc | Reserves outgoing HTTP/2 storage in send path to reduce reallocations. |
| lib/internal/http2/util.js | Refactors nghttp2 header string building to reduce lowercasing/closure overhead and uses Set for strict single-value checks. |
| lib/internal/http2/core.js | Avoids unconditional toLowerCase() in response header array scanning. |
| lib/_http_server.js | Prebuilds HTTP/1.1 status lines and avoids unnecessary statusMessage validation for defaults; uses req._hasBodyHeaders() for optimizeEmptyRequests. |
| lib/_http_outgoing.js | Coalesces headers with small bodies; adds combined chunked end() fast path; caches lenient-header-validation result; reduces common-case toLowerCase(). |
| lib/_http_incoming.js | Adds IncomingMessage.prototype._hasBodyHeaders() scanning rawHeaders to avoid constructing req.headers. |
| lib/_http_common.js | Speeds up checkIsHttpToken() by using a lookup table for short names and regex for longer ones. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| write_(this, chunk, encoding, null, true); | ||
| write_(this, chunk, encoding, onFinish.bind(undefined, this), true); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Fixed. On the chunk path the user callback is now queued with queueEndCallback() before write_(), so a synchronous socket.write() (for example assignSocket() plus a custom Writable) cannot run onFinish() first and then treat the callback as already finished.
Covered by the new assignSocket() + sync end('hello', cb) case in test/parallel/test-http-outgoing-message-write-callback.js.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Follow-up: incoming header bytes now stay off the JS string heap until something actually reads them.
A first attempt used a
I am not updating the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@ ## main #65332 +/- ## ========================================== - Coverage 90.52% 90.50% -0.03% ========================================== Files 805 805 Lines 285669 286151 +482 Branches 55151 55257 +106 ========================================== + Hits 258616 258991 +375 - Misses 17173 17263 +90 - Partials 9880 9897 +17
🚀 New features to boost your workflow:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| header = header.toLowerCase(); | ||
| break; | ||
| } | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What impact does this actually have? I'd prefer not to micro-optimize these things if the benefit is only marginal. The more we micro-optimize this stuff the more difficult maintenance becomes.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Agreed — the HTTP/2 header benches were within noise. Reverted this to an unconditional toLowerCase().
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| const c = key.charCodeAt(i); | ||
| if (c >= 65 && c <= 90) | ||
| return key.toLowerCase(); | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This is an example of what I mean, this is introducing duplication that is more difficult to maintain.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Removed asciiToLowerIfNeeded. buildNgHeaderString uses toLowerCase() again; the only leftover from that pass is the hoisted processor / SafeSet cleanup, which is not a second lowercasing helper.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| const len = name.length; | ||
| if (len === 14) { | ||
| if (name === 'Content-Length' || name === 'content-length' || | ||
| name === 'CONTENT-LENGTH' || name.toLowerCase() === 'content-length') { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is there any evidence that the all-caps variations here are used often enough to warrant special casing? I've never seen it used in practice.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
No evidence they are common enough. Dropped the CONTENT-LENGTH / TRANSFER-ENCODING special cases. The materialized fallback still checks Title-Case, lowercase, and toLowerCase().
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| // Packed incoming headers: magic + count + flags + (nlen, vlen, name, value)* | ||
| // Kept as a Buffer so JS strings are created only when rawHeaders / headers | ||
| // are actually read. Avoids a native BaseObject per request. | ||
| constexpr uint32_t kNativeHeadersMagic = 0x5244484E; // 'NHDR' |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think you have an endianness bug here.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yes. The packed uint32_ts were memcpy'd in host order, while JS always read them as little-endian, which is what failed on s390x and AIX.
They are now written and read with explicit LE helpers (WriteU32LE / ReadU32LE). Magic 0x5244484E is the LE encoding of ASCII NHDR on every arch.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| constexpr uint32_t kNativeHeaderFlagContentLength = 1 << 2; | ||
| constexpr uint32_t kNativeHeaderFlagTransferEncoding = 1 << 3; | ||
| constexpr uint32_t kNativeHeaderFlagTE = 1 << 4; | ||
| constexpr size_t kNativeHeadersPrefix = 12; |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It would be better to export these to the js side to avoid having to define two different sets
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Exported from C++ on HTTPParser: kNativeHeadersMagic, kNativeHeaderFlag*, kNativeHeadersCountOffset, kNativeHeadersFlagsOffset, and kNativeHeadersPrefix. JS reads those instead of keeping a second copy.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| }, | ||
| }); | ||
|
|
||
| ObjectDefineProperty(IncomingMessage.prototype, 'rawHeaders', { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This is an observable change (data property -> prototype property) making this potentially semver-major
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The constructor still does this.rawHeaders = [], so a fresh IncomingMessage keeps an own data property.
When headers arrive packed, _setNativeHeaders installs an own accessor that materializes into an own writable data property on first get or set. It is not a prototype getter.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| calculateLenientFlags, | ||
| prepareError, | ||
| kSkipPendingData, | ||
| unpackHeaderList, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is this actually used anywhere? I couldn't find it if it is.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It was only used by test/parallel/test-http-parser.js. Removed the export; that test now materializes via internalBinding('http_parser').nativeHeadersToArray.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Coverage regressions and endianness bug, at the very least, need to be looked at before this lands. There are a number of questionable changes here (noted in comments);
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Addressed the review notes in the latest commits on this branch:
Replies are on the individual threads. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
commits are missing appropriate |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
After #65802, Host/Expect/body checks still allocate toLowerCase() copies and Expect/HTTP/1.0 TE still read req.headers. Compare names without allocating, read those values from rawHeaders, intern parser header names, and skip Title-Case toLowerCase on common outgoing fields. Refs: #65802 Signed-off-by: Yagiz Nizipli <[email protected]> Assisted-by: a closed-source coding agent PR-URL: #66120 Refs: #65332 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
After nodejs#65802, Host/Expect/body checks still allocate toLowerCase() copies and Expect/HTTP/1.0 TE still read req.headers. Compare names without allocating, read those values from rawHeaders, intern parser header names, and skip Title-Case toLowerCase on common outgoing fields. Refs: nodejs#65802 Signed-off-by: Yagiz Nizipli <[email protected]> Assisted-by: a closed-source coding agent PR-URL: nodejs#66120 Refs: nodejs#65332 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
After #65802, Host/Expect/body checks still allocate toLowerCase() copies and Expect/HTTP/1.0 TE still read req.headers. Compare names without allocating, read those values from rawHeaders, intern parser header names, and skip Title-Case toLowerCase on common outgoing fields. Refs: #65802 Signed-off-by: Yagiz Nizipli <[email protected]> Assisted-by: a closed-source coding agent PR-URL: #66120 Refs: #65332 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
After #65802, Host/Expect/body checks still allocate toLowerCase() copies and Expect/HTTP/1.0 TE still read req.headers. Compare names without allocating, read those values from rawHeaders, intern parser header names, and skip Title-Case toLowerCase on common outgoing fields. Refs: #65802 Signed-off-by: Yagiz Nizipli <[email protected]> Assisted-by: a closed-source coding agent PR-URL: #66120 Refs: #65332 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
After #65802, Host/Expect/body checks still allocate toLowerCase() copies and Expect/HTTP/1.0 TE still read req.headers. Compare names without allocating, read those values from rawHeaders, intern parser header names, and skip Title-Case toLowerCase on common outgoing fields. Refs: #65802 Signed-off-by: Yagiz Nizipli <[email protected]> Assisted-by: a closed-source coding agent PR-URL: #66120 Refs: #65332 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
After #65802, Host/Expect/body checks still allocate toLowerCase() copies and Expect/HTTP/1.0 TE still read req.headers. Compare names without allocating, read those values from rawHeaders, intern parser header names, and skip Title-Case toLowerCase on common outgoing fields. Refs: #65802 Signed-off-by: Yagiz Nizipli <[email protected]> Assisted-by: a closed-source coding agent PR-URL: #66120 Refs: #65332 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
After #65802, Host/Expect/body checks still allocate toLowerCase() copies and Expect/HTTP/1.0 TE still read req.headers. Compare names without allocating, read those values from rawHeaders, intern parser header names, and skip Title-Case toLowerCase on common outgoing fields. Refs: #65802 Signed-off-by: Yagiz Nizipli <[email protected]> Assisted-by: a closed-source coding agent PR-URL: #66120 Refs: #65332 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Filip Skokan <[email protected]>
Rebase onto the internal/http split. Lenient-validation caching and default status-line caching already landed; this keeps the remaining hot-path work: - Check HTTP tokens of length <= 10 with the lookup table. - Coalesce a small body with the header block, and send a chunked end(chunk) as one latin1 write when the header block is still pending. - Recognize the usual Title-Case header names without toLowerCase(). - Keep incoming header bytes packed until rawHeaders or headers is read. - Skip the parser body when a request is dumped. - Hoist HTTP/2 header-string processing off a per-call closure and reserve outgoing session storage. Assisted-by: Cursor Signed-off-by: Yagiz Nizipli <[email protected]> Co-authored-by: Yagiz Nizipli <[email protected]>
37317f2 to
62f65f6
Compare
Requested changes must be addressed to merge this pull request.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.