Repository navigation
Description
Summary
After #1565 (a3e72a3) a redial no longer copies the backend id in BackendRegistry::id_for, but the id is still turned back into a heap String further down the same path — on every request that reuses a pooled backend connection, and several times on every dial. Every copy is of an Rc<str> that BackendId already carries.
Sites (origin/main a3e72a3)
Per request, on a reused backend connection (H1 keep-alive socket or H2 multiplex slot):
lib/src/protocol/mux/router.rs:740—Router::decide_after_gate, reuse branch:stream.context.backend_id = Some(backend.backend_id.to_string()).lib/src/protocol/mux/router.rs:742— same branch:stream.metrics.backend_id = Some(backend.backend_id.to_string()).
Per dial (ConnectPlan::Dial → Mux::dial_backend):
lib/src/protocol/mux/router.rs:1185—Router::backend_from_request:context.backend_id = Some((*dialed.backend.backend_id).to_owned()).lib/src/protocol/mux/mod.rs:2032—Mux::dial_backend:stream.metrics.backend_id = stream.context.backend_id.to_owned()(a secondStringcopy of the same id).lib/src/protocol/mux/mod.rs:1986—Mux::dial_backend:let backend_id_for_gauge = backend.backend_id.to_string(), used only as a&strgauge label.lib/src/protocol/mux/mod.rs:1991/:2009—Mux::dial_backend:cluster_id.to_owned()intoConnection::new_h{1,2}_client, which stores it inPosition::Client.
Static count: 2 heap allocations per request on a reused connection, and 3 backend-id copies plus 1 cluster-id copy per dial.
Cause
HttpContext::backend_id (lib/src/protocol/kawa_h1/editor.rs:277) and SessionMetrics::backend_id (lib/src/lib.rs:1375) are Option<String>. Every reader in the tree already goes through as_deref() / as_ref() into a &str (log context macros, access log, record_backend_metrics!, default answers), so nothing needs ownership of a separate buffer.
Measurement
Per-thread counting allocator crate::test_allocations (lib/src/lib.rs), 64 iterations after one warm-up, on a3e72a3:
| Path | Test (lib/src/protocol/mux/router.rs) |
Allocations |
|---|---|---|
Reused keep-alive backend, Router::decide_after_gate |
a_request_on_a_reused_backend_connection_allocates_nothing |
256, against 128 for a control doing only the mux bookkeeping the branch triggers (Connection::start_stream + Context::link_stream): 2 per request from the backend id |
Dial, Router::backend_from_request through a stub BackendDialer |
stamping_a_dialled_backend_allocates_nothing |
64: 1 per dial |
Mux::dial_backend needs a live L7Proxy and ProxySession, which this crate has no test double for, so its two further copies per dial (mod.rs:1986, mod.rs:2032) are counted from the source.
The control's own 128 (2 per request) are also real per-request allocations, independent of the backend id:
remove_backend_stream(lib/src/protocol/mux/mod.rs:1123-1134) removes the reverse-index key once itsVecis empty, soContext::link_stream(mod.rs:976-982) rebuilds aVecon every attach;Mux::apply_backend_deltas(mod.rs:1252-1256) empties the ledger withstd::mem::take, so the nextContext::record_backend_delta(mod.rs:925, reached fromConnection::start_stream) reallocates it.
Proposed fix
- Make
HttpContext::backend_idandSessionMetrics::backend_idOption<Rc<str>>, and fill them withRc::clone(&backend.backend_id): a reference-count increment instead of a heap copy. This changes the type of two public fields (not a new API surface); the number of broken sites is reported in the PR. - In
Mux::dial_backend, take the gauge label from anRc::cloneof the id taken before theBackendIdmoves into the connection. - The per-dial
cluster_id.to_owned()stays:Position::Clientowns aString, and removing it means carrying anRc<str>cluster id through routing, which is a separate change.
Out of scope, noted for follow-ups: lib/src/protocol/mux/router.rs:477 stores Some(cluster_id.to_owned()) into HttpContext::cluster_id on every request, on both the reuse and the dial path; and the two structural allocations above (reverse-index Vec, delta ledger).
Signed-off-by: Florentin Dubois [email protected]
Activity
perf(mux): stop copying the backend id into a String per request (soz…