Skip to content

perf(mux): cut the allocations of a session's setup - #1612

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/session-setup-allocs
Sep 27, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/session-setup-allocs

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Closes #1610

What

Allocations made while a mux session is set up, measured per H1 request (one request per connection) on a release build with an LD_PRELOAD backtrace interposer, grouped by site:

site before after
Router::backends first insert (BTreeMap leaf, 11 × ~1.7 KiB Connection) 1 alloc, 18 760 B 0
io_slices (h2_transmit::gather, frontend + backend) 9 allocs, 2 944 B 2 allocs, 1 225 B
BackendRegistry::intern 1 alloc, 224 B 1 alloc, 56 B
backend_streams entry (Context::link_stream) 1 alloc, 32 B 1 alloc, 8 B
H1 pending_links 1 alloc, 32 B 1 alloc, 8 B
dead_backends (Mux::ready) 1 per pass that closes a backend 1 per session (reused)
session Rc<RefCell<HttpSession>> 1 alloc, 2 920 B 1 alloc, 4 648 B (inline slot)
Stream (Context::create_stream) 1 alloc, 1 616 B unchanged (already exact)
  • Router::backends becomes BackendConnections: the lowest-token connection inline, the others in the BTreeMap. Iteration keeps the total order on Token (h2 sans-io: determinism — seeded sources or pinned observable order (Q8, Q9) #1338), point operations stay O(log n) for multi-backend sessions. HAProxy keeps no table at all: a stream reaches its one server connection by pointer (include/haproxy/stream-t.h:264 srv_conn, :316 scb, include/haproxy/stconn-t.h:340 sedesc->conn, allocated by conn_new at src/backend.c:1977 from the pool of src/connection.c:41); a sozu mux session can hold several, hence the overflow map. The trade-off is the session object growing by the inline Connection (+1 728 B), paid once instead of the 18 760-byte leaf.
  • gather reserves kawa.out.len() descriptors before pushing: one allocation per fresh connection instead of doubling 4 → 64.
  • The registry, reverse-index entries and the H1 pending_links are sized for one on first use; dead_backends moves to Router and is reused across passes.

No new dependency.

End to end

Release, one worker, python backend, 20 sequential curl requests, LD_PRELOAD malloc counter, two runs each, per request:

scenario allocations before → after bytes before → after
H1, one request per connection 67.5–67.9 → 59.6–59.7 35 108–35 415 → 16 218–16 297
H2, one request per TLS connection 397.8–398.4 → 392.1–392.4 87 788–88 258 → 69 249–69 502
H2 multiplexed, 20 streams, one connection 67.6–67.7 → 63.0–63.2 8 154–8 160 → 6 052–6 058

Tests

One per site on crate::test_allocations (which gains bytes()), each seen red with its change reverted:

  • the_first_backend_connection_of_a_session_allocates_nothing (router.rs): 1 → 0 allocations
  • a_cold_h1_write_pass_sizes_its_descriptor_vector_once (h1.rs): 3 → 1 allocation above control
  • the_first_interned_backend_sizes_the_registry_for_one (mod.rs): 256 → 88 bytes
  • a_new_backend_entry_in_the_reverse_index_is_sized_for_one_stream (mod.rs): 32 → 8 bytes
  • a_parsed_h1_request_queues_its_link_in_a_queue_sized_for_one (h1.rs): 32 → 8 bytes
  • successive_dead_backends_reuse_one_dead_backend_list (mod.rs): 1 → 0 allocations on a later pass

Follow-up, out of scope: the hashbrown tables of Mux::timeouts and Context::backend_streams still cost several allocations per request.

Signed-off-by: Florentin Dubois [email protected]

Router::backends was a BTreeMap<Token, Connection<SessionTcpStream>>. std's
B-tree stores its values inline in 11-slot leaves and a Connection is about
1.7 KiB, so the first insertion allocated 18 760 bytes to hold the one
backend connection a session almost always has: 53 % of the bytes an H1
request allocated. BackendConnections keeps the lowest-token connection
inline and the others in the BTreeMap, so one backend costs no allocation,
iteration keeps the total order on Token (#1338) and point operations stay
O(log n). The trade-off is the session object itself, still one allocation,
growing from 2 920 to 4 648 bytes.

h2_transmit::gather reserves one descriptor per queued block before it
pushes, so a fresh connection sizes io_slices once instead of doubling it
from 4 up to 64 (9 allocations per H1 request become 2).

The BackendRegistry, a backend token's backend_streams entry and the H1
frontend's pending_links are sized for the one element they almost always
hold instead of four. The dead-backend list of Mux::ready moves to
Router::dead_backends and is reused across passes.

Release build, one worker, 20 requests, LD_PRELOAD malloc counter, two runs,
per request: H1 67.5-67.9 -> 59.6-59.7 allocations, 35 108-35 415 ->
16 218-16 297 bytes; H2 397.8-398.4 -> 392.1-392.4 allocations,
87 788-88 258 -> 69 249-69 502 bytes; multiplexed H2 67.6-67.7 -> 63.0-63.2
allocations, 8 154-8 160 -> 6 052-6 058 bytes.

Each site has an allocation test on crate::test_allocations, which gains
bytes() for the sites whose cost is a size rather than a count.

Closes #1610

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the perf/session-setup-allocs branch from b78adea to 1321dcb Compare September 27, 2026 05:40
@FlorentinDUBOIS
FlorentinDUBOIS merged commit addef7b into main Sep 27, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/session-setup-allocs branch September 27, 2026 06:37
FlorentinDUBOIS added a commit that referenced this pull request Sep 27, 2026
…nline (#1614)

Closes #1613

## What

#1612 left two `hashbrown` tables built once per mux session, so once per request on a one-request connection. Measured per site with `crate::test_allocations`:

| site | before | after |
|---|---|---|
| `Mux::timeouts` (frontend + one backend handle) | 1 alloc, 340 B | **0** |
| `Context::backend_streams` (first `link_stream`: table + entry `Vec`) | 2 allocs, 156 B | **0** |
| `backend_streams`, second stream on the same backend (spill) | 1 alloc, 32 B | 1 alloc, 32 B (unchanged) |

- `Mux::timeouts` becomes `MuxTimeouts`. Its keys are tokens, not timeout kinds: the frontend's never changes for the life of a session, so its handle is the `frontend` field; the backends' are an open set, held in an `InlineTokenMap<TimeoutContainer>`.
- `InlineTokenMap<V>` has `BackendConnections`' shape (#1612): the lowest-token entry inline, the others in a `BTreeMap`, iteration in ascending `Token` order. One backend costs no allocation.
- `Context::backend_streams` becomes `InlineTokenMap<LinkedStreams>`. `LinkedStreams` holds the first stream inline and spills into a `Vec` sized for four only when a second stream links to the same connection, the same allocation the old `Vec` made at that point. An emptied entry is still kept for the next link (#1583), and link order is preserved (`Mux::timeout` walks it in order).
- `Mux::close` replaces the frontend handle with an unarmed one where it used to drop the map entry: dropping the old one cancels its wheel entry, and a later `reschedule` sees what it would have inserted. The WebSocket upgrade moves the frontend handle out instead of removing it.
- Iteration order: only the debug consistency checks iterate either structure, so no log or metric depended on the `HashMap` order.
- The trade-off: the session object (`Rc<RefCell<HttpSession>>`, still one allocation) grows from 4 648 to 4 784 bytes.

HAProxy prior art, at `0ceb8c65f`: a stream keeps no per-connection timeout table. Its deadlines are fixed fields: `include/haproxy/stream-t.h:276` `conn_exp`, `include/haproxy/channel-t.h:208` `analyse_exp`, `include/haproxy/stconn-t.h:346-347` `lra`/`fsb`. `process_stream` folds them into the one task's `expire` (`include/haproxy/task-t.h:150`) with `tick_first` (`src/stream.c:2748-2754`). A sozu session keeps one wheel entry per connection because each connection's core publishes its own deadline. So the frontend gets a fixed field, and the backends an inline slot.

No new dependency.

## End to end

Release, one worker, python backend, 20 sequential `curl` requests, `LD_PRELOAD` malloc counter, two runs each, per request:

| scenario | allocations before → after | bytes before → after |
|---|---|---|
| H1, one request per connection | 59.5–59.9 → 56.55–57.0 | 16 178–16 454 → 15 819–16 172 |
| H2, one request per TLS connection | 392.45–392.6 → 389.3–389.6 | 69 694–69 883 → 69 514–69 583 |
| H2 multiplexed, 20 streams, one connection | 62.7–65.25 → 61.55–63.55 | 6 027–6 078 → 6 015–6 078 |

H1 and H2 lose the three allocations per connection (340 + 148 + 8 bytes). The byte saving is smaller (about −280 to −360 B) because the session object grew by 136 B. Grouped by site with an `LD_PRELOAD` backtrace interposer, the H1 `hashbrown` allocations drop from 6 to 4 per request: `http.rs` `timeouts` and `Context::link_stream` are gone, and the four left are in `server.rs`, out of scope. The 8-byte entry `Vec` is gone too. In the multiplexed case, one table of each per connection is spread over 20 requests, which is below the noise between runs.

## Tests

One per site, each seen failing on the code before this change (production files restored from `origin/main`, tests kept):

- `a_session_holds_its_frontend_and_first_backend_wheel_handles_without_allocating` (`mod.rs`): `(1, 340)` → `(0, 0)`.
- `a_new_backend_entry_in_the_reverse_index_is_sized_for_one_stream` (`mod.rs`, the #1612 test, strengthened): `(2, 156)` → `(0, 0)`.
- `a_second_stream_on_one_backend_spills_the_reverse_index_entry_once` bounds the spill: one 32-byte allocation, link order kept, and capacity reused on the next link. It passes on both sides by design.

Signed-off-by: Florentin Dubois <[email protected]>
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.

perf(mux): per-session setup allocations (18.8 KiB B-tree leaf, oversized first pushes, io_slices growth)

1 participant