Skip to content

perf(mux): BackendRegistry::id_for allocates an Rc<str> on every backend dial #1564

Closed

Description

@FlorentinDUBOIS

Site

BackendRegistry::id_for in lib/src/protocol/mux/mod.rs (origin/main d2d4bbd0, line 390) builds the core's BackendId on every backend dial:

fn id_for(&mut self, backend: &Rc<RefCell<Backend>>) -> BackendId {
    let slot = self.intern(backend);
    let borrow = backend.borrow();
    BackendId {
        slot,
        backend_id: Rc::from(borrow.backend_id.as_str()),
        address: borrow.address,
    }
}

intern already reuses the slot of a backend the session dialled before, but Rc::from(&str) copies Backend::backend_id into a fresh Rc<str> each time. It is reached from RegistryDialer::dial, i.e. once per backend connection opened by a mux session (H1 and H2 alike).

Cost per dial

One heap allocation of 16 + backend_id.len() bytes (the RcBox header plus the string bytes), one memcpy of the id, and the matching free when the BackendId drops. The id never changes for the life of a registry entry, so the copy carries no information the Backend does not already hold.

Measurement

A counting global allocator (the model already used by lib/tests/backend_selection.rs) around repeated id_for calls on the same, already-interned backend: every call costs exactly one allocation in steady state. The PR adds that test and shows it red before the fix.

Proposed correction

Allocate the Rc<str> once, on the control plane, and make the dial path a reference-count increment:

  • preferred: Backend::backend_id becomes an Rc<str> built in Backend::new, and id_for clones it (zero allocations per dial);
  • fallback, if the type change spreads beyond a handful of call sites or into a public API of sozu-command-lib: BackendRegistry caches one Rc<str> beside each slot, so the id is allocated once per session and per backend instead of once per dial.

The PR will state which one it takes and why, and pin the steady-state allocation count with a regression test.

Activity

  1. added 2 commits that reference this issue on Sep 26, 2026
    9cbeeb5
    e6e87b1
  2. added a commit that references this issue on Sep 26, 2026
    a3e72a3
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