Repository navigation
Description
Summary
#1581 (39dcc9c) removed the backend-id copies from the reuse branch, and its test measured the routing decision only as a difference against the mux bookkeeping it triggers. That bookkeeping still allocates on every request that reuses a backend connection, and the cluster id is still copied once per request and twice per dial. Measured end to end, a request on a reused H1 keep-alive backend allocates on every pass through Router::plan_connect.
Sites (origin/main 39dcc9c)
Per request, on a reused backend connection:
- Reverse index —
remove_backend_stream(lib/src/protocol/mux/mod.rs:1123) removes thebackend_streamskey once itsVecis empty, soContext::link_stream(mod.rs:976) rebuilds aVecon the next attach: 1 allocation per request. - Backend delta ledger —
Mux::apply_backend_deltas(mod.rs:1252) and the pre-plan settle inMux::ready_inner(mod.rs:2617) both emptyContext::backend_deltaswithstd::mem::take, so the nextrecord_backend_delta(reached fromConnection::start_stream) reallocates it: 1 allocation per request. HttpContext::cluster_id—Router::plan_connectstoresSome(cluster_id.to_owned())(lib/src/protocol/mux/router.rs:477) althoughroute_from_requesthas just returned an ownedString: 1 allocation per request, gated or not.
Per dial (ConnectPlan::Dial → Mux::dial_backend):
cluster_idcopied twice —Router::decide_after_gatebuilds the plan withcluster_id.to_owned()(router.rs:768), thenMux::dial_backendcopies it again intoConnection::new_h1_client/new_h2_client(mod.rs:1991,mod.rs:2009): 2 copies per dial, wherePosition::Clientneeds one.
Measurement
Per-thread counting allocator crate::test_allocations (lib/src/lib.rs), debug test build, steady state after one warm-up request, H1 keep-alive backend staged by the routing_fixture of lib/src/protocol/mux/router.rs:
| Window | Allocations per request |
|---|---|
Router::plan_connect, ungated (session_address: None, as every existing fixture) |
9 = 3 debug-only DebugEvent::Str(get_route()) + 3 in route_from_request + 1 site 3 + 1 site 1 + 1 site 2 |
plan_connect → CheckIpLimit, gated (session_address: Some, the production path) |
7 = 3 debug-only + 3 routing + 1 site 3 |
consult_ip_gate (gated) |
3, in SessionManager bookkeeping |
plan_connect_resume (gated) |
2 = site 1 + site 2 |
release (unlink_stream + ledger drain) |
0 |
In a release build the debug history push disappears, leaving 6 per request ungated.
The three in route_from_request are outside these four sites and are listed so the remaining gap is explicit: captured_authority = host.to_owned() (router.rs:828), the trie_path Vec::with_capacity(16) of Router::lookup (lib/src/router/mod.rs:165), and the cluster id cloned out of the route table into RouteResult (lib/src/router/mod.rs:2329 / :2364). The gate's own are in lib/src/server.rs (consult_ip_gate passes resume.cluster_id().to_owned(), mod.rs:1227, and track_cluster_connection clones it again, server.rs:809).
Proposed fix
- (1) Keep the emptied
Vecin itsbackend_streamsentry so the nextlink_streamreuses its capacity, and remove the key where the backend connection itself leavesRouter::backends(the dead-backend sweep ofMux::ready_inner;Mux::closealready clears the map). The index stays bounded by the live backend connections of the session, which the debug invariant ofready_innerthen checks instead of the key count. - (2) Drain the ledger in place through one
BackendRegistry::apply_all, used by both drains, so its capacity survives. It is bounded by the largest number of deltas a single pass pushes. - (3) Move the routed
StringintoHttpContext::cluster_idand havedecide_after_gateandconsult_ip_gateborrow it from there;ConnectResumestops carrying its own copy. No public type changes. - (4) Keep the single copy
ConnectPlan::Dialcarries and move it intoPosition::ClientinMux::dial_backend. - Extend perf(mux): stop copying the backend id into a String per request #1581's
a_request_on_a_reused_backend_connection_allocates_nothingto assert absolute zero, and add an end-to-end test of the reuse branch on both the gated and the ungated path, plus a dial test proving the connection owns the planned allocation.
Target: 0 allocations per request for the reuse branch itself; route_from_request and the SessionManager gate remain out of scope and are measured separately.
Signed-off-by: Florentin Dubois [email protected]
Activity
perf(router): copy no authority and allocate no trie path per request (…
perf(mux): stop allocating per request on a reused backend connection (…
perf(mux): keep a session's wheel handles and backend reverse index i…