Repository navigation
Description
Site
ConnectionH1::writable in lib/src/protocol/mux/h1.rs (line 651 on a3e72a3) builds a fresh descriptor vector on every write pass, on both positions (response to the frontend, request to the backend):
let mut io_slices = Vec::new();
for block in kawa.out.iter() { /* one IoSlice per Store, up to the first Delimiter */ }The vector allocates on its first push and is freed when writable returns. It then grows by doubling (4 → 8 → 16 → 32 descriptors), and each growth is one more realloc. The H1 BlockConverter (kawa 0.7.1, src/protocol/h1/converter.rs) emits one Store per piece of a header block: the status line is 6 blocks and every header 4 (key, ": ", val, "\r\n"). So a pass that writes a header block costs 1 allocation plus one realloc per doubling, and a body pass costs at least 1.
Measured cost
A unit test counts the heap allocations one warm writable pass makes, through the shared per-thread counter crate::test_allocations (lib/src/lib.rs). The pass queues Store::Static blocks on a linked stream and writes them to a live loopback peer. The first pass is the warm-up; the second one is measured. On a3e72a3:
| blocks per pass | Position::Server (response) |
Position::Client (request) |
|---|---|---|
1, cargo test --release |
1 | 1 |
16, cargo test --release |
3 | 3 |
16, cargo test (debug) |
3 | 3 |
Every one of those allocations is this Vec: 1 allocation for the first 4 descriptors, then one realloc at 8 and at 16. Nothing else on the warm pass allocates. The fixture uses a freshly dialled upstream, so the replay capture's buffer.reserve(size) (line 711), which only a pooled upstream pays, is not counted.
Proposed fix
This is #1571 on the H1 side, and the mechanism #1578 uses there is already shared. h2_transmit::gather and h2_transmit::confirm (lib/src/protocol/mux/h2_transmit.rs:145 and :212) are generic over Kawa<T: AsBuffer>. gather does exactly what this loop does: one descriptor per Store, up to the first Delimiter, returning the bytes offered. confirm clears the vector and then calls Kawa::consume. So:
- keep the scratch vector on the connection, as a
pub(super) io_slices: Vec<IoSlice<'static>>field ofConnectionH1, set in the two constructors ofconnection.rs; - replace the hand-written loop and the
queuedsum withh2_transmit::gather(kawa, &mut self.io_slices), and thekawa.consume(size)withh2_transmit::confirm.
The capacity survives between passes, a descriptor never does: gather clears the vector on entry (h2_transmit.rs:146) and confirm clears it before Kawa::consume (h2_transmit.rs:219). Between the two, writable only makes the vectored write, runs the replay capture (which reads the descriptors and writes to retry_buffer, not to kawa.storage), and pushes one debug event. Its one early return in that window is taken only when the vector is empty.
This adds h1.rs's first unsafe block: the call to gather, under the same safety contract as the H2 call site. It adds no new unsafe code, no new dependency, and does not change the number of writev(2) calls.
Related: #1571 (the H2 issue), #1578 (its fix, not merged). Both edit the same "only production caller" paragraph of h2_transmit.rs, so the second one to merge needs a small rebase.
Activity
perf(mux-h1): reuse the write pass IoSlice vector across passes
perf(mux-h1): reuse the write pass IoSlice vector across passes
perf(mux-h1): reuse the write pass IoSlice vector across passes (sozu…