http: reduce per-request allocations in the server - #65802
Open
mcollina wants to merge 1 commit into
Open
Conversation
Remove allocations and repeated work performed for every request: - reuse a per-connection updateOutgoingData closure and a shared 'finish' listener instead of binding two functions per request, reaching resOnFinish through the connection state stored on the response - cache the ServerResponse options object per server (custom response classes keep receiving a fresh object) - check for Host, Expect, Content-Length and Transfer-Encoding by scanning rawHeaders instead of materializing req.headers, which was built (with per-name toLowerCase calls) for every request even when the application never reads it - cache the rendered status line per status code when the reason phrase is the default, skipping its character validation - cache the complete 'Date: ...' header line in the utcDate cache and the keep-alive header pair for the current server settings - compute the lenient-validation option chain once per message Signed-off-by: Matteo Collina <[email protected]>
Collaborator
|
Review requested:
|
ronag
approved these changes
Sep 4, 2026
jasnell
approved these changes
Sep 4, 2026
Collaborator
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65802 +/- ##
==========================================
+ Coverage 90.05% 90.15% +0.09%
==========================================
Files 754 769 +15
Lines 255727 261721 +5994
Branches 48312 49688 +1376
==========================================
+ Hits 230286 235944 +5658
- Misses 16564 16795 +231
- Partials 8877 8982 +105
🚀 New features to boost your workflow:
|
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.
Removes allocations and repeated work the HTTP server performs for every request:
updateOutgoingData.bind()becomes one closure per connection reused by every response, and the per-requestresOnFinish.bind()becomes a shared'finish'listener that reaches everything through the connection state stored on the response.req.headersmaterialization: therequireHostHeadercheck (req.headers.host), theExpectcheck, andoptimizeEmptyRequests' body-header check now scanrawHeaders(over the same range theheadersgetter exposes) instead of building the whole headers object — with atoLowerCase()per header name — for every request even when the application never readsreq.headers.Date:header line: the complete line is cached alongside the existing one-secondutcDate()cache.Connection: keep-alive\r\nKeep-Alive: timeout=Nis rendered once per server settings instead of per response.Responses are byte-identical to main (verified by diffing raw responses).
Benchmarks (i7-7700, server pinned to one core,
wrk -t2 -c50on separate physical cores, interleaved rounds, CPU from/proc/<pid>/stat):Sampled heap allocation drops from 5206 to 4531 B/request (−13%); the
req.headersbuild (~200 B + per-nametoLowerCaseper request) disappears from the profile entirely.test/parallel/test-http-*,test-http2-*,test-stream-*,test-net-*,test-https-*and the http diagnostics-channel suites all pass (1171 tests).Follow-up to #65732 and #65749.
🤖 Generated with Claude Code
https://claude.ai/code/session_01TV8WVjiHJPR2hW4D6h1Xpr