Skip to content

http: reduce per-request server overhead - #64348

Open
anonrig wants to merge 2 commits into
nodejs:mainfrom
anonrig:http-server-fastpath
Open

anonrig wants to merge 2 commits into
nodejs:mainfrom
anonrig:http-server-fastpath

Conversation

@anonrig

@anonrig anonrig commented Jul 7, 2026 •

Copy link
Copy Markdown
Member

Server-side fast-path work reducing per-request overhead on the hot
keep-alive path. Builds on #64346 (its commit is the base of this branch and
appears in the diff until it lands; the two commits on top are this PR).

What changes

http: reduce per-request server overhead

  • end(chunk) issued a second, empty socket.write whose only purpose was
    to carry the finish callback — a full Writable pass and an extra
    cork-queue entry per response. When the header block is already rendered
    and the framing is final (no chunked trailer, no strict content-length
    accounting), the finish callback now rides the body write; writes
    complete in order, so observable finish timing is unchanged.
  • The response options bag and the pending-data callback only depend on the
    connection: allocated once per socket, reused per response.
  • The response close listener is installed once per socket (the handler was
    already guarded on socket._httpMessage), instead of an
    add/removeListener pair per response.
  • Status lines for default reason phrases are cached per status code; the
    Keep-Alive: line is memoized per (timeout, max) configuration.

http: move invariant IncomingMessage defaults to the prototype —
dropped from this PR after review (semver-major per @jasnell); will be
resubmitted separately with the proper label.

Measurements

CPU-per-request harness (fresh server process per sample, in-process
pipelined keep-alive clients, interleaved old/new, Welch t-test), hello
world with Content-Length, baseline = #64346's head:

stack requests per CPU-second p
first commit +7.34% (25x600k samples) 2.0e-14
both commits +8.46% (15x600k samples) 2.2e-11

Cross-check under autocannon (-c 50): 130.5K -> 138.9K req/s (+6.4%)
for the first commit alone. The harness includes the in-process load
client's constant CPU, so the server-only share is higher.

Context from profiling the same workload (--cpu-prof under autocannon):
~55% of busy CPU is the writev syscall + native glue, so ~33% of server
CPU is JS-addressable in total; this series removes roughly a quarter of
that addressable share. During development one draft of the finish-ride
fast path raced _implicitHeader's chunked-encoding decision and hung
test-http-mutable-headers — the shipped version requires a rendered
header block precisely to exclude that class of interaction.

Correctness

  • All test/parallel/test-http-*, test-http.js, test-https-*, and
    test-http2-* pass; linters clean.
  • Not included on purpose: defaulting optimizeEmptyRequests (documented
    as breaking for servers that read bodies of body-header-less requests —
    measured at an additional +4.4% for opt-in users) and any change to
    drain/backpressure semantics.

AI assistance disclosure

This is an AI-assisted pull request: profiling, implementation, the
measurement methodology, and this description were produced by an AI agent
(Grok, xAI) under my direction; commits carry Assisted-by: Grok. I
reviewed and take responsibility for every change.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Jul 7, 2026
@anonrig anonrig added request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Jul 7, 2026
Comment thread lib/_http_incoming.js Resolved
@jasnell jasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Jul 8, 2026
@codecov

codecov Bot commented Jul 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.53%. Comparing base (c58ccfa) to head (0cd8413).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #64348   +/-   ##
=======================================
  Coverage   90.52%   90.53%           
=======================================
  Files         805      805           
  Lines      285669   285683   +14     
  Branches    55151    55158    +7     
=======================================
+ Hits       258616   258646   +30     
+ Misses      17173    17168    -5     
+ Partials     9880     9869   -11     
Files with missing lines Coverage Δ
lib/internal/http/outgoing.js 98.06% <100.00%> (+0.03%) ⬆️
lib/internal/http/server.js 96.63% <100.00%> (-0.03%) ⬇️

... and 24 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@anonrig
anonrig force-pushed the http-server-fastpath branch from af61c79 to 43a9d71 Compare July 8, 2026 01:11
@jasnell jasnell removed the semver-major PRs that contain breaking changes and should be released in the next major version. label Jul 8, 2026
@anonrig

anonrig commented Jul 9, 2026

Copy link
Copy Markdown
Member Author

cc @jasnell @mcollina @ronag can i get a review on this please?

@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Jul 9, 2026
@nodejs-github-bot

This comment was marked as outdated.

@gurgunday gurgunday left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina

Copy link
Copy Markdown
Member

linter is not happy

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@Ethan-Arrowood Ethan-Arrowood left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Also TIL about ~~ integer coercion.

anonrig and others added 2 commits November 28, 2026 01:52
Rebase onto the internal/http split. Lenient-validation caching and the
Keep-Alive line cache already landed. This keeps the remaining per-message
cuts:

- Pre-filter the known-field matcher on length and first character so
  ordinary headers skip toLowerCase().
- Hoist header-block length, content-length, and header-value length
  loads the engine cannot fold across calls.
- Load the header string and the socket accessor once per write.
- Flush corked chunked buffers through a shared callback runner. The
  drain re-check added on main stays on that runner.

Assisted-by: Cursor
Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com>
Status-line caching, Keep-Alive memoization, response-option reuse, and
riding finish on the final body write already landed. The remaining
per-request cut is the response 'close' listener: it is a no-op unless
a response is attached, so it is installed once per socket instead of
an add/removeListener pair on every keep-alive response.

Assisted-by: Cursor
Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com>
@cursor
cursor Bot force-pushed the http-server-fastpath branch from 43a9d71 to 0cd8413 Compare November 28, 2026 01:54
@Ethan-Arrowood

Copy link
Copy Markdown
Contributor

fyi commit lint is failing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants