Repository navigation
perf(mux): keep a session's wheel handles and backend reverse index inline - #1614
Merged
Merged
Conversation
…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
requested review from
Wonshtrum and
llenotre
as code owners
September 27, 2026 07:25
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 #1613
What
#1612 left two
hashbrowntables built once per mux session, so once per request on a one-request connection. Measured per site withcrate::test_allocations:Mux::timeouts(frontend + one backend handle)Context::backend_streams(firstlink_stream: table + entryVec)backend_streams, second stream on the same backend (spill)Mux::timeoutsbecomesMuxTimeouts. Its keys are tokens, not timeout kinds: the frontend's never changes for the life of a session, so its handle is thefrontendfield; the backends' are an open set, held in anInlineTokenMap<TimeoutContainer>.InlineTokenMap<V>hasBackendConnections' shape (perf(mux): cut the allocations of a session's setup #1612): the lowest-token entry inline, the others in aBTreeMap, iteration in ascendingTokenorder. One backend costs no allocation.Context::backend_streamsbecomesInlineTokenMap<LinkedStreams>.LinkedStreamsholds the first stream inline and spills into aVecsized for four only when a second stream links to the same connection, the same allocation the oldVecmade 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::timeoutwalks it in order).Mux::closereplaces 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 laterreschedulesees what it would have inserted. The WebSocket upgrade moves the frontend handle out instead of removing it.HashMaporder.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:276conn_exp,include/haproxy/channel-t.h:208analyse_exp,include/haproxy/stconn-t.h:346-347lra/fsb.process_streamfolds them into the one task'sexpire(include/haproxy/task-t.h:150) withtick_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
curlrequests,LD_PRELOADmalloc counter, two runs each, per request: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_PRELOADbacktrace interposer, the H1hashbrownallocations drop from 6 to 4 per request:http.rstimeoutsandContext::link_streamare gone, and the four left are inserver.rs, out of scope. The 8-byte entryVecis 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_oncebounds 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]