Skip to content

perf(mux): the backend id is copied into a String on every reused-connection request and on every dial #1579

Closed

Description

@FlorentinDUBOIS

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 second String copy 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 &str gauge label.
  • lib/src/protocol/mux/mod.rs:1991 / :2009 — Mux::dial_backend: cluster_id.to_owned() into Connection::new_h{1,2}_client, which stores it in Position::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 its Vec is empty, so Context::link_stream (mod.rs:976-982) rebuilds a Vec on every attach;
  • Mux::apply_backend_deltas (mod.rs:1252-1256) empties the ledger with std::mem::take, so the next Context::record_backend_delta (mod.rs:925, reached from Connection::start_stream) reallocates it.

Proposed fix

  • Make HttpContext::backend_id and SessionMetrics::backend_id Option<Rc<str>>, and fill them with Rc::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 an Rc::clone of the id taken before the BackendId moves into the connection.
  • The per-dial cluster_id.to_owned() stays: Position::Client owns a String, and removing it means carrying an Rc<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

  1. added a commit that references this issue on Sep 26, 2026
    39dcc9c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions