Skip to content

perf(mux-h1): reuse the write pass IoSlice vector across passes - #1582

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/h1-write-iovec-no-alloc
Sep 26, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/h1-write-iovec-no-alloc

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1580

What

ConnectionH1::writable built a fresh Vec<IoSlice> on every write pass, in both directions (response to the frontend, request to the backend). It allocated on the first descriptor and then made one realloc per doubling. An H1 header block is dozens of Store blocks, one per status-line piece and four per header, so intentrace shows 45 descriptors in the writev(2) of a curl request and 32 in that of a python3 -m http.server response.

This is #1571 on the H1 side, and #1578's mechanism is already shared. h2_transmit::gather and h2_transmit::confirm are generic over Kawa<T: AsBuffer>, and gather does exactly what the H1 loop did. So the change reuses that pair and adds no new abstraction:

  • ConnectionH1 gets an io_slices: Vec<IoSlice<'static>> field. It is pub(super) only because the two constructors live in mux/connection.rs, like peer_address.
  • writable replaces its hand-written loop and its queued sum with h2_transmit::gather, and its kawa.consume(size) with h2_transmit::confirm.

Safety argument

Only the vector's capacity survives a pass, never a descriptor:

  • gather clears the vector on entry, and confirm clears it before Kawa::consume.
  • Between the two, writable makes the vectored write, which reborrows the descriptors and cannot retain them. It then copies the accepted bytes into the replay capture, which reads the descriptors and writes to retry_buffer, never to kawa, and pushes one debug event.
  • The window is therefore wider than H2's three statements, and the SAFETY comment describes this shape rather than copying H2's.
  • Its one early return inside the window is taken only when the vector is empty.
  • If the write panics, the stranded descriptors are dropped without being read (IoSlice has no Drop), and the next gather clears them before anything could read them.

h1.rs gains its first unsafe block, which is the call to gather under that function's existing # Safety contract. There is no new unsafe code and no new dependency. The docs of h2_transmit.rs and doc/h2_mux_internals.md now name the second caller.

Measurements

Unit tests. a_warm_h1_response_write_pass_allocates_nothing (Position::Server) and a_warm_h1_request_write_pass_allocates_nothing (Position::Client, freshly dialled, so the replay capture stays off) each count the allocations of one warm writable pass. They use the shared crate::test_allocations counter; no new #[global_allocator]. The pass writes 16 Store blocks to a live loopback peer.

before after
cargo test --release, response 3 0
cargo test --release, request 3 0
cargo test (debug), each side 3 0

On a3e72a3, one block per pass costs 1 allocation. With 16 blocks, the 3 allocations are one alloc and two reallocs (4 → 8 → 16). To see the tests fail, give writable a per-pass local vector in place of self.io_slices, as their doc comments describe. That measures left: 3, right: 0 in both profiles.

Release binary, built with --no-default-features --features crypto-ring,opentelemetry,splice,simd. The setup is one worker, an H1 listener, and a python3 -m http.server backend. The counts come from intentrace -p <worker> --trace writev, with ANSI codes stripped and lines anchored on the syscall-name field:

workload (N=100) before after
writev(2), 100 sequential connections 200 200
writev(2), 100 requests on one curl connection 200 200
descriptors per writev(2) 100 × 45, 100 × 32 100 × 45, 100 × 32

The writev(2) count does not change: the change moves where the descriptors live, not how many go into each call.

Rebased on #1578

Rebased on b5169d44, which includes #1578. The two callers of gather now share one "two production callers" paragraph in its docs, and the H1 field sits beside H2's private io_slices field.

Checks

The following all exit 0:

  • cargo build --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd
  • cargo +nightly fmt --all -- --check
  • clippy with the same features and --all-targets -- -D warnings
  • cargo test -p sozu-lib (1161), -p sozu (226), -p sozu-command-lib (185)
  • cargo test -p sozu-e2e -- --skip tests::fuzz_tests:: (449)
  • sim test and sim clippy under RUSTFLAGS="--cfg tokio_unstable"
  • check_doc_citations.py --self-test, --root ., --base b5169d44, and --audit. Four h1.rs citations in the two LIFECYCLE.md files are re-anchored to the same text in the same function.
  • RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked

Signed-off-by: Florentin Dubois [email protected]

ConnectionH1::writable built a fresh Vec<IoSlice> on every write pass,
in both directions: one allocation on the first descriptor, then one
realloc per doubling. An H1 header block is dozens of Store blocks, so
intentrace shows 45 descriptors in the writev(2) of a curl request and
32 in that of a python3 -m http.server response.

The vector is now a pub(super) io_slices field of ConnectionH1, set only
by its two constructors, and writable fills and discharges it through
h2_transmit::gather and h2_transmit::confirm, the pair the H2 write pass
already uses, instead of its own loop. Only the capacity survives a
pass: gather clears the vector on entry and confirm clears it before
Kawa::consume. Between the two, writable only writes, copies the
accepted bytes into the replay capture (retry_buffer, never kawa) and
pushes a debug event; its one early return there is taken only when the
vector is empty. h1.rs gains its first unsafe block, the gather call
under that function's existing safety contract. No new unsafe code and
no new dependency.

Measured with the shared test_allocations counter, one warm 16-block
pass: 3 -> 0 allocations on the response and on the request side, in
debug and release. Release build, one worker, H1 listener, python
backend, intentrace -p <worker> --trace writev: 200 writev(2) before and
after for 100 sequential connections and for 100 requests on one
connection, with the same descriptor counts per call.

Closes #1580

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the perf/h1-write-iovec-no-alloc branch from d6b848e to e357b9d Compare September 26, 2026 18:44
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 4d104b3 into main Sep 26, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/h1-write-iovec-no-alloc branch September 26, 2026 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(mux-h1): writable allocates its IoSlice vector on every write pass

1 participant