Skip to content

perf(mux): keep a session's wheel handles and backend reverse index inline - #1614

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/mux-hashmap-allocs
Sep 27, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/mux-hashmap-allocs

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

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 (perf(mux): cut the allocations of a session's setup #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 (perf(mux): a reused backend connection still allocates per request in the reverse index, the delta ledger and the cluster id #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 perf(mux): cut the allocations of a session's setup #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]

…nline

Mux::timeouts and Context::backend_streams were hashbrown tables built
once per session, so once per request on a one-request connection:
1 allocation / 340 bytes for the timeouts, and 2 allocations / 156 bytes
(table and entry Vec) for the first link_stream.

The timeout keys are tokens, not timeout kinds. The frontend's never
changes for the life of a session and becomes the `frontend` field of
MuxTimeouts. The backends' are an open set, held in an InlineTokenMap:
BackendConnections' shape, with the lowest-token entry inline and the
others in a BTreeMap. The reverse index becomes an
InlineTokenMap<LinkedStreams>. Its entries hold their first stream
inline and spill into a Vec of four, the allocation the old Vec made
at that point, only when a second stream links to the same connection.
Emptied entries are still kept (#1583), and link order is preserved.

Per H1 and H2 request (release, LD_PRELOAD counter), this removes
three allocations: 59.5-59.9 -> 56.55-57.0 for H1 and 392.45-392.6 ->
389.3-389.6 for H2. The session object grows from 4 648 to 4 784 bytes.

Closes #1613

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit a078550 into main Sep 27, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/mux-hashmap-allocs branch September 27, 2026 08:19
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): the Mux::timeouts and Context::backend_streams tables still allocate per session

1 participant