http,http2: speed up header validation and write coalescing - #65332
Open
anonrig wants to merge 4 commits into
Open
http,http2: speed up header validation and write coalescing#65332anonrig wants to merge 4 commits into
anonrig wants to merge 4 commits into
Conversation
Collaborator
|
Review requested:
|
Replace regex-based header token/value checks with byte lookup tables, cache OutgoingMessage lenient-validation, and coalesce headers with small Buffer bodies into a single socket write. Scan IncomingMessage rawHeaders for Content-Length and Transfer-Encoding so optimizeEmptyRequests does not force req.headers construction. On the HTTP/2 path, skip toLowerCase for already-lowercase names, use a Set for sensitive/single-value header checks, and reserve outgoing session storage to avoid reallocs while gathering nghttp2 frames. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
Send headers, the last chunk, and the chunked terminator in a single write() when res.end() is used with Transfer-Encoding: chunked. Reuse prebuilt HTTP/1.1 status lines for default reason phrases, skip toLowerCase() on common outgoing header names, and hoist HTTP/2 header serialization off the per-call closure. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
anonrig
force-pushed
the
cursor/http-http2-perf-f24c
branch
from
August 16, 2026 18:59
76c8e14 to
62327c4
Compare
The chunked end() fast path concatenated headers with the body and wrote the result using the body encoding. That re-encoded obs-text header values as UTF-8 and reduced corked res.end() to a single socket.write(), which broke test-http-server-non-utf8-header and test-http-response-cork. Copy headers as latin1 into the combined buffer, and accept a single write after uncork. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
JS lookup tables were slower than V8's regex for header values and for token names longer than ~10 bytes. Restore the regex path for those cases, and use the table for names of length <= 10 so Connection / Keep-Alive stay on the faster path. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
Member
Author
|
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- |
ronag
approved these changes
Aug 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This speeds up the HTTP/1 and HTTP/2 hot paths that show up in
writeHead()/end(), small-body writes, and HTTP/2 header serialization.HTTP/1
Connection,Keep-Alive). Longer names stay on the regex path; a JS table overthose lengths is slower than V8's regex.
OutgoingMessagelenient-validation and skipstatusMessagevalidation for the built-in reason phrases.
HTTP/1.1 <code> <reason>\r\nstatus lines.toLowerCase()for the common Title-Case / lowercase spellingsof
Connection,Content-Length,Transfer-Encoding, and friends.Bufferbodies into onesocket.write().res.end(chunk)is used withTransfer-Encoding: chunkedandheaders have not been flushed, send headers + last chunk + terminator
in a single write (headers copied as latin1 so obs-text is preserved).
IncomingMessage.rawHeadersforContent-Length/Transfer-EncodingsooptimizeEmptyRequestsdoes not forcereq.headersconstruction.HTTP/2
toLowerCase()when header names are already lowercase.buildNgHeaderStringprocessing off a per-call closure.Setfor sensitive / strict single-value header checks.nghttp2_session_mem_sendchunks.These are incremental, behavior-preserving changes. They do not move
end-to-end
benchmark/http/simple.jsby 50%. Localwrknumbers vsupstream/main(same machine, release build, 2x 5s):check_is_http_tokenforConnectionis about +21%. Header-valuevalidation stays on regex (a JS
Uint8Arrayscan was 30–50% slower).Tests
checkInvalidHeaderCharcases intest/parallel/test-http-common.js.test/parallel/test-http-chunked-end-coalesce.jschecks the wireformat of coalesced chunked
end()(string, buffer, UTF-8, trailers,unusual
Content-Lengthcasing, latin1 header values).Local: 779/779
test-http*/test-https*/test-http2*passedagainst a release build of this branch.