Skip to content

perf(mux): a reused backend connection still allocates per request in the reverse index, the delta ledger and the cluster id #1583

Closed

Description

@FlorentinDUBOIS

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:

  1. Reverse index — remove_backend_stream (lib/src/protocol/mux/mod.rs:1123) removes the backend_streams key once its Vec is empty, so Context::link_stream (mod.rs:976) rebuilds a Vec on the next attach: 1 allocation per request.
  2. Backend delta ledger — Mux::apply_backend_deltas (mod.rs:1252) and the pre-plan settle in Mux::ready_inner (mod.rs:2617) both empty Context::backend_deltas with std::mem::take, so the next record_backend_delta (reached from Connection::start_stream) reallocates it: 1 allocation per request.
  3. HttpContext::cluster_id — Router::plan_connect stores Some(cluster_id.to_owned()) (lib/src/protocol/mux/router.rs:477) although route_from_request has just returned an owned String: 1 allocation per request, gated or not.

Per dial (ConnectPlan::Dial → Mux::dial_backend):

  1. cluster_id copied twice — Router::decide_after_gate builds the plan with cluster_id.to_owned() (router.rs:768), then Mux::dial_backend copies it again into Connection::new_h1_client / new_h2_client (mod.rs:1991, mod.rs:2009): 2 copies per dial, where Position::Client needs 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 Vec in its backend_streams entry so the next link_stream reuses its capacity, and remove the key where the backend connection itself leaves Router::backends (the dead-backend sweep of Mux::ready_inner; Mux::close already clears the map). The index stays bounded by the live backend connections of the session, which the debug invariant of ready_inner then 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 String into HttpContext::cluster_id and have decide_after_gate and consult_ip_gate borrow it from there; ConnectResume stops carrying its own copy. No public type changes.
  • (4) Keep the single copy ConnectPlan::Dial carries and move it into Position::Client in Mux::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_nothing to 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]

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