Skip to content

perf(mux): per-session setup allocations (18.8 KiB B-tree leaf, oversized first pushes, io_slices growth) #1610

Closed

Description

@FlorentinDUBOIS

Problem

Every H1 request served on a fresh client connection pays for heap allocations made while its session is set up, before any byte is proxied. A release build with a malloc counter (LD_PRELOAD, one worker, loopback backend, 20 H1 requests, one request per connection, 7c820e57; every line below is unchanged at b74d5d97) measured 35 108 bytes and 67.5 allocations per request (45 malloc, 22.5 growing realloc). Grouping the allocations by their first sozu frames:

site allocations / request bytes / allocation what it is
lib/src/protocol/mux/router.rs:810 self.backends.insert(token, connection) 1 18 760 a full BTreeMap leaf
lib/src/server.rs:1649 → lib/src/http.rs Rc::new(RefCell::new(session)) 1 2 920 the session itself
lib/src/protocol/mux/mod.rs:1222 self.streams.push(stream) 1 1 616 one Stream
lib/src/protocol/mux/mod.rs:415 BackendRegistry::intern 1 224 4 × RegistryEntry (56 B)
lib/src/protocol/mux/mod.rs:1102 backend_streams.entry(token).or_default().push 1 32 4 × GlobalStreamId
lib/src/protocol/mux/h1.rs:576 context.pending_links.push_back 1 32 4 × GlobalStreamId
lib/src/protocol/mux/mod.rs:2598 dead_backends.push 1 32 4 × Token, per pass
lib/src/protocol/mux/h2_transmit.rs:176 ← h1.rs:696 (io_slices) 9 64 → 1 024 two Vec<IoSlice> growing 4 → 8 → … → 64

1. Router::backends allocates an 11-slot B-tree leaf for one connection

Router::backends is a BTreeMap<Token, Connection<SessionTcpStream>> (#1338 made it ordered). std's B-tree stores values inline in its nodes, and a leaf holds 2B − 1 = 11 of them. Connection<SessionTcpStream> is about 1.7 KiB, so the first insertion allocates 11 × (8 + ~1 704) ≈ 18.8 KiB to hold the single backend connection a session almost always has: 53 % of the bytes allocated per H1 request. No hasher, no slab: node capacity × value size.

HAProxy keeps no table here: a stream reaches its one server connection through a pointer (include/haproxy/stream-t.h:264 struct server *srv_conn, :316 struct stconn *scb, then include/haproxy/stconn-t.h:340 sedesc->conn), allocated per connection from a typed pool (src/backend.c:1977 conn_new(s->target), src/connection.c:41). A sozu mux session can hold several backend connections (H2, keep-alive to several backends), so a single slot is not enough, but the storage can be sized to what it holds.

2. create_sessions (2 920 B)

This is Rc<RefCell<HttpSession>>, the session object, allocated once at its exact size. Nothing to tune by capacity; shrinking it means changing the HttpSession/Mux layout (the frontend Connection is ~1.7 KiB inline), which is a trade-off and out of scope here.

3. Small vectors sized for four on their first push

Vec::push on an empty vector reserves four elements when an element is at most 1 KiB. Where a session almost always holds exactly one element (one backend in the registry, one stream per H1 backend, one pending link on an H1 frontend, one dead backend per pass), three of the four slots are never used. mod.rs:1222 is already exact: a Stream is larger than 1 KiB, so its first push reserves exactly one.

Capacity cannot remove these allocations, only shrink them. The per-pass dead_backends vector is the exception: it is allocated again on every pass that closes a backend, so a keep-alive frontend whose backend closes after each response pays one allocation per request.

4. io_slices grows by doubling on every new connection

#1582 made the IoSlice vector a field reused across write passes, but each new connection starts empty and its first pass doubles it from 4 up to the number of Store blocks queued (the H1 BlockConverter emits one per name, separator, value and CRLF): 5 allocations on the frontend (64 slices) and 4 on the backend (32 slices), 9 per request.

Proposed fix

  1. Store backend connections in a vector of (Token, Connection) kept sorted by Token, behind a small type exposing the map operations the mux uses. Iteration stays in Token order (h2 sans-io: determinism — seeded sources or pinned observable order (Q8, Q9) #1338 unchanged), and the first insertion allocates exactly one Connection (≈1.7 KiB instead of ≈18.8 KiB).
  2. Nothing; documented above.
  3. Size the first allocation of BackendRegistry, the backend_streams entries, the H1 pending_links and dead_backends for exactly one element, lazily (no allocation for a session that never uses them), and keep dead_backends on the session so later passes reuse it.
  4. In h2_transmit::gather, reserve the number of queued blocks before pushing, so a fresh connection allocates its vector once instead of doubling.

Each site gets an allocation test on crate::test_allocations, seen red before the fix; before/after end-to-end figures in H1, H2 and multiplexed H2 go in the PR.

Activity

  1. added 2 commits that reference this issue on Sep 27, 2026
    1321dcb
    addef7b
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