Repository navigation
Description
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 thisVec.- In a debug build,
finalize_write'sQuiescearm adds oneformat!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
h20.4.15 (src/codec/framed_write.rs:142) writes through tokio-util'spoll_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)), andraw_sock_from_bufsends each contiguous chunk with a single stackstruct 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
perf(mux-h2): reuse the write pass IoSlice vector across passes
perf(mux-h1): reuse the write pass IoSlice vector across passes (#1582)
perf(mux-h2): reuse the write pass IoSlice vector across passes (sozu…