Skip to content

http: use intrusive lists in ConnectionsList - #65296

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:http-connectionslist-intrusive
Open

http: use intrusive lists in ConnectionsList#65296
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:http-connectionslist-intrusive

Conversation

@mcollina

@mcollina mcollina commented Aug 15, 2026

Copy link
Copy Markdown
Member

Every HTTP message was performing multiple erase and insert operations on the two std::set instances in ConnectionsList, which are ordered by a key (last_message_start_) that mutates on every message. In a hello-world server profile this showed up as ~2.2% of total CPU cycles spent in red-black tree rebalancing plus tree-node allocations, all on the per-request hot path.

This PR replaces both sets with intrusive doubly-linked lists:

  • A node is linked to itself when it is not in a list, so Remove() is idempotent and the destructor unlinks automatically, which also removes the old "Pop from the lists BEFORE resetting last_message_start_" footgun.
  • Membership in the "all connections" list no longer changes per message, so the Pop+Push pair in on_message_begin/on_message_complete is gone entirely.
  • Updating the active list is now a single O(1) unlink + tail append with no allocations.
  • Appending to the tail keeps the active list ordered by last_message_start_ because uv_hrtime() is monotonic, so expired() semantics are unchanged. The arrays returned by all()/idle()/active() are now in insertion order instead of comparator order; none of their consumers depend on ordering.

Benchmark results (Linux, i7-7700, server and benchmarker pinned to separate cores):

                                                                                           confidence improvement accuracy (*)   (**)  (***)
http/simple.js duration=5 chunkedEnc=0 c=50 chunks=1 len=4 type='bytes' benchmarker='wrk'          **      4.06 %       ±2.41% ±3.23% ±4.25%
http/simple.js duration=5 chunkedEnc=0 c=500 chunks=1 len=4 type='bytes' benchmarker='wrk'                 2.35 %       ±2.45% ±3.29% ±4.34%

A separate interleaved wrk A/B (hello-world server, -t2 -c50, 5 alternating pairs) shows +4.9% (26,599 → 27,914 req/s), with the patched binary winning every pair. A perf profile of the patched binary confirms the _Rb_tree symbols are gone and malloc traffic in the parser path is roughly halved.

This PR was prepared with the help of AI assistance.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. http_parser Issues and PRs related to the HTTP Parser dependency or the http_parser binding. needs-ci PRs that need a full CI run. labels Aug 15, 2026
@mcollina
mcollina force-pushed the http-connectionslist-intrusive branch from e7c6580 to 41044a2 Compare August 15, 2026 04:07
@codecov

codecov Bot commented Aug 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.27451% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.12%. Comparing base (347e266) to head (0cc64e0).
⚠️ Report is 156 commits behind head on main.

Files with missing lines Patch % Lines
src/node_http_parser.cc 86.27% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65296      +/-   ##
==========================================
- Coverage   90.30%   90.12%   -0.19%     
==========================================
  Files         759      752       -7     
  Lines      248294   251826    +3532     
  Branches    46860    47356     +496     
==========================================
+ Hits       224220   226954    +2734     
- Misses      15516    16225     +709     
- Partials     8558     8647      +89     
Files with missing lines Coverage Δ
src/node_http_parser.cc 84.72% <86.27%> (-0.26%) ⬇️

... and 174 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.

@ronag ronag 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.

Have you benchmarked against an unordered_set or just a plain vector?

I suspect those would be more cache friendly and avoid pointer chasing.

@ronag ronag added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Aug 16, 2026
@ronag
ronag requested a review from jasnell August 16, 2026 07:59
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 16, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@ronag ronag added the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 17, 2026
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Aug 17, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator
Commit Queue failed
- Loading data for nodejs/node/pull/65296
✔  Done loading data for nodejs/node/pull/65296
----------------------------------- PR info ------------------------------------
Title      http: use intrusive lists in ConnectionsList (#65296)
Author     Matteo Collina <matteo.collina@gmail.com> (@mcollina)
Branch     mcollina:http-connectionslist-intrusive -> nodejs:main
Labels     c++, http_parser, author ready, needs-ci, commit-queue
Commits    1
 - http: use intrusive lists in ConnectionsList
Committers 1
 - Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/65296
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/65296
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Sat, 15 Aug 2026 04:05:15 GMT
   ✔  Approvals: 2
   ✔  - Robert Nagy (@ronag) (TSC): https://github.com/nodejs/node/pull/65296#pullrequestreview-4943029706
   ✔  - Paolo Insogna (@ShogunPanda) (TSC): https://github.com/nodejs/node/pull/65296#pullrequestreview-4949195971
   ✘  3 GitHub CI job(s) failed:
   ✘    - format-cpp: FAILURE (https://github.com/nodejs/node/actions/runs/31863623606/job/94961040997)
   ✘    - lint-commit-message: FAILURE (https://github.com/nodejs/node/actions/runs/31863623617/job/94961006361)
   ✘    - test-macOS: FAILURE (https://github.com/nodejs/node/actions/runs/31863623630/job/94961038864)
   ℹ  Last Full PR CI on 2026-08-16T15:24:03Z: https://ci.nodejs.org/job/node-test-pull-request/75886/
- Querying data for job/node-test-pull-request/75886/
✔  Build data downloaded
- Querying failures of job/node-test-commit/90616/
✔  Data downloaded
   ✘  2 failure(s) on the last Jenkins CI run
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/32009841931

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@mcollina
mcollina force-pushed the http-connectionslist-intrusive branch from 41044a2 to 340ded7 Compare August 17, 2026 15:45
Every HTTP message was performing multiple erase and insert operations
on two std::set instances ordered by a mutating key, showing up as ~2%
of CPU cycles in a hello-world server profile due to red-black tree
rebalancing and node allocations.

Replace both sets with intrusive doubly-linked lists. Membership in the
list of all connections no longer changes per message, and updating the
active connections list is now O(1) with no allocations. Appending to
the tail keeps the active list ordered by last_message_start_ because
uv_hrtime() is monotonic.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina force-pushed the http-connectionslist-intrusive branch from 340ded7 to 0cc64e0 Compare August 17, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-failed An error occurred while landing this pull request using GitHub Actions. http_parser Issues and PRs related to the HTTP Parser dependency or the http_parser binding. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants