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
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.
@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 91.11111% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.31%. Comparing base (347e266) to head (41044a2).
⚠️ Report is 121 commits behind head on main.

Files with missing lines Patch % Lines
src/node_http_parser.cc 91.11% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             main   #65296     +/-   ##
=========================================
  Coverage   90.30%   90.31%             
=========================================
  Files         759      751      -8     
  Lines      248294   250241   +1947     
  Branches    46860    47294    +434     
=========================================
+ Hits       224220   226002   +1782     
- Misses      15516    15618    +102     
- Partials     8558     8621     +63     
Files with missing lines Coverage Δ
src/node_http_parser.cc 84.72% <91.11%> (-0.26%) ⬇️

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

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants