Skip to content

tcp: a connection is found by the slot its reactor gave it, not by its descriptor, so the table and the walks over it are as long as the reactor's own connections - #658

Open
MDA2AV wants to merge 1 commit into
mainfrom
feat/connection-slots
Open

MDA2AV wants to merge 1 commit into
mainfrom
feat/connection-slots

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Nov 23, 2026

Copy link
Copy Markdown
Owner

A reactor found a completion's connection in _connections, an array indexed by file descriptor, because the operation's tag (user_data) carried the fd. Descriptors are numbered across the process, so on every reactor that array was as long as the highest one open anywhere, for the few that are its own: at a million connections over 64 reactors, 1M entries (8 MB) per reactor for about 15,600 connections.

Finding one connection is one index, however long the array is. Going through all of them is not. The tick's walk visits every empty entry, four times a second and on every reactor at once, whenever a read or send timeout is set, and both are 60 s by default. #653 made that walk rarer, not cheaper. HttpArena's connections-ramp-h1 fits that: with 35 s timeouts, in the seconds before its first connection came due, genhttp-ioxide's p99.9 was 1.0-1.2 ms next to a p99 of 137-139 µs.

Now each connection takes a slot in its reactor's table when it is accepted, and the tag carries that.

What changes

  • _slots replaces _connections, for the lookup and for both walks, the tick's and the stop's. At accept a connection takes the last freed slot, or the next new one (Track). It leaves the table when the reactor lets go of it (Untrack) and gives the slot back when it is recycled, after the cancel and the close. The array is as long as the most connections the reactor has held at once.
  • The tag of a recv or a send carries the slot where it carried the fd. The generation beside it guards a slot's reuse the way it guarded a descriptor's: what is left over from an earlier connection finds the slot's next one under another generation, and is dropped. Accept keeps the listener's fd there, and client and UDP operations had slots of their own already.
  • The queues that carried (generation, fd) carry (generation, slot): flushes, incremental buffer returns, deferred write shutdowns, and recvs waiting for a buffer. System calls and SQEs take the fd from conn.ClientFd.
  • _tcpLive is gone: what is accepted and not recycled yet is the slots handed out less the free ones.

In state that is the count of slots handed out, the free ones (a Stack<int>) and the slot on each connection, in place of _tcpLive and the fd that travelled next to the connection through the submit and close methods.

The table still doesn't shrink. After a burst has gone the walk goes through as many slots as the reactor held at its peak, where main's goes through as many as the process did.

API: Reactor.EnqueueReturnQIncremental(int fd, ushort gen, ushort bid) was public and is internal, since its first argument is now the slot, which only the runtime has. Handlers give buffers back with TcpConnection.ReturnBuffer and ReturnBuffers, which pick the queue. GenHTTP, HttpArena's entries and the Playground don't call it. Package validation against 0.29.656, in strict mode, finds no other change.

The docs that described the tag and the table by fd are updated (learn/architecture, learn/dev-core, how-ioxide-does-h3).

Measured

On the dev box, pinned to its E-cores, main and this branch in turns.

The walk, timed on TcpSweep itself (optimized code) with both default timeouts, every connection idle with a read parked, and the caches overwritten in between, as they are between two ticks. Median of 200 ticks and their p99, each the middle one of three runs:

Connections main this branch
1,048,576 over 64 reactors, one sweeping 797 µs (p99 1,069) 118 µs (p99 139)
1,048,576 over 64 reactors, 16 sweeping at once 3,518 µs (p99 3,785) 1,518 µs (p99 1,675)
65,536 over 64 reactors, 16 at once 342 µs (p99 426) 119 µs (p99 152)
4,096 over 4 reactors, all 4 at once 27 µs (p99 37) 24 µs (p99 33)

What is left is looking at the connections themselves: about 90 ns each, out of memory, with 16 at once.

The request path, bench/ab.sh on Tcp/Raw (2 reactors, 64 connections, 6 s runs): main 531,058 req/s and 3.730 µs of CPU per request, this branch 530,124 and 3.736, over 8 rounds. Main against a second checkout of itself: 529,458 and 3.739 against 530,602 and 3.733, over 6. No difference to see.

The table at a million connections over 64 reactors: 16,384 entries, 128 KB, per reactor instead of 8 MB.

Not in this PR

Tests

  • ConnectionSlotTests (Unit, new), on a reactor that never runs: which slot a connection takes, that only its slot and generation together find it, that its slot goes to nobody before it is recycled and the last one freed goes first, growth, and the live count.
  • SlotTests (E2E, new): slots stay low whatever the descriptors; connections one after another reuse the same few; the table grows under 1,300 open connections; under churn, with resets and handlers that flush and let go off the reactor, each client gets its own answer, in both recv modes. The cancels behind a failed send and behind a full recv queue reach their own connection's recv, which no test covered.
  • The tcp: a stale recv completion can close the next connection on its fd, because every new connection object starts at generation 0 #416 case in HardeningTests (a recycled connection's stale recv completion) made sure the next connection got the same descriptor. That completion names the slot now, so the case is about that, without the descriptor filling. It still fails when generations repeat.

27 mutants in a separate checkout, all caught: a slot never freed, freed before the recycle, never taken again, or taken first-freed first; an untracked connection still found; the generation ignored or repeating; the growth check off by one; the live count forgetting the recycled or what is off the table; a recv, send or sendmsg tagged with the descriptor, or submitted on the slot; the recycle's close or the stop's abort made on the slot; either walk missing the last slot; a flush, either kind of buffer return, a deferred shutdown or a starved recv queued under the descriptor; and either recv cancel naming it. SlotTests with HardeningTests passed 12 of 12 repeat runs, and 8 of 8 pinned to two cores and to one.

Suite Result
E2E 823 passed, 0 failed, 1 skipped (a Kestrel kTLS test)
Unit 406 passed, 0 failed, 0 skipped
Http 113 passed, 0 failed, 0 skipped
File 14 passed, 0 failed, 0 skipped
Tls 217 passed, 0 failed, 41 skipped (every skip is a kTLS test: no tls module on this box)
Chaos 105 passed, 0 failed, 0 skipped
Pg, Redis not run, not reached by this change

Overlap

git merge-tree --write-tree against every open PR: this adds no conflict, in files or in hunks.

…s descriptor, so the table and the walks over it are as long as the reactor's own connections

The tag of a recv or send carried the connection's descriptor, and the reactor found the connection in a table indexed by it. Descriptors are numbered across the process, so on every reactor that table was as long as the highest one open anywhere, and the tick's walk, made whenever a read or send timeout is set, went through all of it.

A connection takes a slot in its reactor's table at accept and gives it back when it is recycled, and the tag carries the slot. One array, as long as the most connections the reactor has held at once, serves the lookup and both walks, the tick's and the stop's. The generation guards a slot's reuse as it did a descriptor's. The flush, buffer-return, deferred-shutdown and starved-recv entries carry the slot too, and the count of live connections is the slots used less the ones free.

Reactor.EnqueueReturnQIncremental is internal now: its first argument is the slot, which no caller outside the runtime has.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant