Skip to content

perf(mux-h2): write_streams allocates its IoSlice vector on every write pass #1571

Closed

Description

@FlorentinDUBOIS

Site

H2Shell::write_streams in lib/src/protocol/mux/h2.rs (line 8200 on e123d73) builds a fresh scratch vector on every write pass:

let mut io_slices: Vec<IoSlice<'static>> = Vec::new();

The vector allocates on its first push, inside h2_transmit::gather (lib/src/protocol/mux/h2_transmit.rs:163), and is freed when write_streams returns. Any pass that transmits stream bytes therefore costs one heap allocation plus one free. A response costs at least one pass, so at least one allocation per response on the H2 hot path.

Measured cost

A unit test counts the heap allocations one warm write_streams pass makes, through a per-thread counting global allocator. The pass queues two Store blocks on a registered stream and writes them to a loopback socket. The first pass is the warm-up; the second one is measured:

  • cargo test --release -p sozu-lib --lib a_warm_write_pass_allocates_nothing -> left: 1, right: 0: exactly one allocation per transmitting pass, and it is this Vec.
  • In a debug build, finalize_write's Quiesce arm adds one format! under #[cfg(debug_assertions)]. It is not compiled in release and is out of scope.

Proposed fix

Keep the scratch vector on the connection, as a private io_slices field of H2Shell, and reuse it across passes. It is cleared, never freed. The capacity survives between passes, but a descriptor never does: gather clears the vector on entry (h2_transmit.rs:146), confirm clears it before Kawa::consume (h2_transmit.rs:219), and nothing sits between the two but the vectored socket write. The unsafe window stays as narrow as it is today. The field stays private, so the bracket cannot be split from outside the crate. No new dependency and no new unsafe. The number of writev(2) calls does not change.

Prior art:

  • hyper's h2 0.4.15 (src/codec/framed_write.rs:142) writes through tokio-util's poll_write_buf (src/util/poll_buf.rs:126-133), which gathers into a stack array [IoSlice; 64] on each write. No heap.
  • HAProxy copies frames into a per-connection output ring h2c->mbuf (src/mux_h2.c:5073, br_count(h2c->mbuf)), and raw_sock_from_buf sends each contiguous chunk with a single stack struct iovec (src/raw_sock.c:463-474).

A stack array bound to the borrow of kawa could also remove the 'static relabelling and the unsafe altogether. It would, however, change the public h2_transmit::gather signature that the simulation harness uses, and it would need a truncation rule. That makes it a separate refactor and it is out of scope here.

Activity

  1. added a commit that references this issue on Sep 26, 2026
    c9412d1
  2. added a commit that references this issue on Sep 26, 2026
    83bc921
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