Repository navigation
perf(mux): cut the allocations of a session's setup - #1612
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 27, 2026 04:53
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
force-pushed
the
perf/session-setup-allocs
branch
from
September 27, 2026 05:40
b78adea to
1321dcb
Compare
This was referenced Sep 27, 2026
Closed
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_PRELOADbacktrace interposer, grouped by site:Router::backendsfirst insert (BTreeMapleaf, 11 × ~1.7 KiBConnection)io_slices(h2_transmit::gather, frontend + backend)BackendRegistry::internbackend_streamsentry (Context::link_stream)pending_linksdead_backends(Mux::ready)Rc<RefCell<HttpSession>>Stream(Context::create_stream)Router::backendsbecomesBackendConnections: the lowest-token connection inline, the others in theBTreeMap. Iteration keeps the total order onToken(h2 sans-io: determinism — seeded sources or pinned observable order (Q8, Q9) #1338), point operations stayO(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:264srv_conn,:316scb,include/haproxy/stconn-t.h:340sedesc->conn, allocated byconn_newatsrc/backend.c:1977from the pool ofsrc/connection.c:41); a sozu mux session can hold several, hence the overflow map. The trade-off is the session object growing by the inlineConnection(+1 728 B), paid once instead of the 18 760-byte leaf.gatherreserveskawa.out.len()descriptors before pushing: one allocation per fresh connection instead of doubling 4 → 64.pending_linksare sized for one on first use;dead_backendsmoves toRouterand is reused across passes.No new dependency.
End to end
Release, one worker, python backend, 20 sequential
curlrequests,LD_PRELOADmalloc counter, two runs each, per request:Tests
One per site on
crate::test_allocations(which gainsbytes()), each seen red with its change reverted:the_first_backend_connection_of_a_session_allocates_nothing(router.rs): 1 → 0 allocationsa_cold_h1_write_pass_sizes_its_descriptor_vector_once(h1.rs): 3 → 1 allocation above controlthe_first_interned_backend_sizes_the_registry_for_one(mod.rs): 256 → 88 bytesa_new_backend_entry_in_the_reverse_index_is_sized_for_one_stream(mod.rs): 32 → 8 bytesa_parsed_h1_request_queues_its_link_in_a_queue_sized_for_one(h1.rs): 32 → 8 bytessuccessive_dead_backends_reuse_one_dead_backend_list(mod.rs): 1 → 0 allocations on a later passFollow-up, out of scope: the
hashbrowntables ofMux::timeoutsandContext::backend_streamsstill cost several allocations per request.Signed-off-by: Florentin Dubois [email protected]