Skip to content

refactor(mux-h2): move the HPACK coders and scratch buffers into hpack_state - #1403

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-hpack-state
Sep 21, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
refactor/h2-hpack-state

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

First step of the H2 sans-io extraction decided on 2026-09-21 (#1339, #1336). It is deliberately the
smallest one, and it should be reviewed as "is this a pure move with zero behaviour change" — not
as a design review. The design is settled on the issues.

What moves

The six HPACK-owned values that lived directly on ConnectionH2 — the loona_hpack
Decoder/Encoder pair and the four reusable scratch buffers converter_buf, lowercase_buf,
cookie_buf, priorities_buf — move into a new lib/src/protocol/mux/hpack_state.rs.

ConnectionH2 keeps a single hpack: HpackState field, so ownership does not split: the struct still
owns the state for the same lifetime. What changes is that HpackState's fields are private to that
module
, so nothing outside it can reach the raw HPACK state. That compiler-enforced narrowing is the
whole point of this step — it is the mechanism by which later steps can move the state without hunting
for stray field accesses.

Construction still happens exactly once, in ConnectionH2::new, with the same
set_max_allowed_table_size call. The two SETTINGS table-size-adjustment sites now go through
setters. converter.rs needed no change at all: H2BlockConverter::encoder already has exactly the
type HpackState::encoder_mut() returns.

Two things that are not purely mechanical, called out rather than absorbed

priorities_buf joins the take/put pattern, and there is an early return between the two.
Collapsing four separate fields into one struct removed the disjoint-field borrowing the compiler gave
for free, so priorities_buf now has to be taken out before encoder_mut() borrows self.hpack for
the pass — the same pattern the other three scratch buffers already used. write_streams has an early
return result; on the account_emitted_rst GOAWAY path between the take and the put, so on that
path the buffer is not returned to HpackState.

This is not a state loss: the baseline does priorities_buf.clear() then .extend(...) at the top of
every write pass, so the buffer carries nothing across passes. The cost is one lost allocation
capacity on a rare error path, and it makes priorities_buf behave exactly like converter_buf,
lowercase_buf and cookie_buf, which have had this shape all along.

A needless_range_loop clippy fired only after the buffer became a local. 'outer: for idx in 0..priorities_buf.len() became 'outer: for &stream_id in &priorities_buf. idx was used solely to
index priorities_buf, and the vector is not mutated inside the loop, so iteration order and values
are unchanged. No #[allow] was added.

Documentation

LIFECYCLE.md's value is line-accurate citations, and this diff shifts h2.rs by up to 20 lines and
mod.rs by one. 95 citations were re-anchored.

They were verified by content rather than by arithmetic: for each one, the text at the old line in the
base revision was compared against the text at the new line here. 94 are byte-identical.

The 95th is a correction of pre-existing drift, stated here so it is not laundered by the
renumbering around it.
§5.1 describes expect_read / expect_write; its citation read
h2.rs:1731-1732, which at the base pointed at expect_write and last_stream_id — off by one, from
some earlier edit. It now reads 1733-1734 and points at the pair the prose actually describes.

Three further citations outside LIFECYCLE.md had drifted and are fixed: two in doc/testing.md
(h2.rs:6142→6122, mod.rs:1306→1307) and one mod.rs citation inside LIFECYCLE.md
(mod.rs:2344→2345-2346, widened to cover the !is_draining() guard and the graceful_goaway call
it guards, which is what the surrounding prose describes).

Two citations that were already stale before this changeset are deliberately left alone rather
than mixed into a pure move — h2.rs:879 in invariant 7 (rst_sent.contains, whose real site is
elsewhere) and invariant 20's marker for the start of #[cfg(test)] mod tests. Both want a follow-up.

Validation

Run on the branch, exit codes checked directly rather than through a pipe.

cargo +1.93.1 build --all-features --locked                              -> exit 0
cargo +1.93.1 clippy --all-targets --all-features -- -D warnings         -> exit 0
cargo +nightly fmt --all -- --check                                      -> exit 0
cargo +1.93.1 test -p sozu-lib --locked protocol::mux::h2                -> exit 0
    test result: ok. 117 passed; 0 failed; 0 ignored; 0 measured; 804 filtered out
python3 .github/scripts/check_doc_citations.py --base origin/main        -> exit 0
    OK: none of the 198 cited lines compared against 7be659544163 changed their text.

Note for anyone re-running the tests: cargo test -p sozu-lib prints three test result: lines and
the last two are empty filtered suites, so a tail on that command shows running 0 tests and tells
you nothing.

What this does not do

No behaviour change, no new dependency, no async fn, no API surface outside mux/. It does not yet
introduce BufferSource, which is a decision on #1336 and not yet code. It does not touch the
zero.storage dual-role problem — that is the reassembly step, several changesets away.

Refs #1336, #1339.

…k_state

Pure move, no behaviour change. Relocates the six HPACK-owned fields that
lived directly on ConnectionH2 — the loona_hpack Decoder/Encoder pair and
the four scratch Vecs (converter_buf, lowercase_buf, cookie_buf,
priorities_buf) — into a new lib/src/protocol/mux/hpack_state.rs module.
ConnectionH2 keeps a single `hpack: HpackState` field, so ownership does
not split, but HpackState's fields are private to that module: every
access now goes through an accessor (decoder_mut, encoder_mut,
set_decoder_max_allowed_table_size, set_encoder_max_table_size,
take/put_converter_buf, take/put_lowercase_buf, take/put_cookie_buf,
take/put_priorities_buf, shrink_converter_buffers, reclaim_idle_buffers),
so nothing outside hpack_state.rs can reach the raw HPACK state anymore.
That compiler-enforced narrowing is the point of this step: it is the
first cut of the strangler extraction turning ConnectionH2 into a sans-io
core.

Construction still happens exactly once, in ConnectionH2::new, with the
same set_max_allowed_table_size call on the decoder. The two SETTINGS
table-size-adjustment sites (ACK sync, SETTINGS_HEADER_TABLE_SIZE) now go
through the new setters. write_streams still borrows the encoder for the
whole write pass via encoder_mut(); priorities_buf now has to be taken
out by value (take_priorities_buf) before that borrow starts and put back
after, because collapsing the encoder and the scratch buffers into one
struct field removes the automatic disjoint-field borrowing the compiler
previously gave four separate ConnectionH2 fields — this was the one
place the move was not purely mechanical. shrink_converter_buffers and
the cancel_timed_out_streams reclaim block both move onto HpackState
unchanged, just parameterised by the existing SCRATCH_BUF_RETAIN
constant. converter.rs needed no change: H2BlockConverter already
borrowed `&'a mut loona_hpack::Encoder<'static>`, which is exactly what
encoder_mut() returns.

Also updates the h2.rs/mod.rs line citations in LIFECYCLE.md that this
diff shifted, including two that were already stale before this change
(h2.rs:879 and the "mod tests onward" anchor) and are left pointing at
the same content as before, not corrected, since fixing pre-existing
drift is out of scope here.

Signed-off-by: Florentin Dubois <[email protected]>
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.

1 participant