Skip to content

bench(h2): measure the stream-table container choice for #1338 - #1502

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
bench/h2-stream-table-container
Sep 24, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
bench/h2-stream-table-container

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

A criterion benchmark for the one open question in #1338: should H2StreamTable::streams become a BTreeMap, or keep a HashMap with a seeded hasher? It converts nothing and changes no production code — it exists so the choice can be made on numbers and re-checked later.

The result inverts the premise the question was built on

The Q9 disposition preferred a seeded hasher on the assumption that O(1) beats O(log n) on the per-frame hot path. Measured, that assumption does not hold at the n that actually occurs.

btree / fixed-seed ratios, two-run mean (below 1.00 means BTreeMap is faster):

group n=8 n=100 n=1000 n=10000
get_hit 0.43 0.83 2.38 6.40
get_miss 0.57 0.88 2.00 6.12
churn 0.58 0.88 1.30 2.98
write_pass uniform 0.53 0.59 1.15 1.11
write_pass varied 0.58 0.93 1.94 2.16

At n = 8 BTreeMap is 1.7-2.3x faster on every group. At n = 100 it is equal or faster on all five comparisons and slower on none. The mechanism: SipHash-1-3 over a u32 costs about 9 ns regardless of n, while a BTreeMap of 11 or fewer keys is a single cache line. Per-frame lookup is 4.0 ns against 9.4 ns at n = 8. Being on the hot path is what makes the tree better here, not worse.

BTreeMap loses past the crossover, which sits between 100 and 1000: about +50 ns per frame lookup at n = 10000.

Separately, a fixed-seed hasher is below noise against today's RandomState in all 20 cells, so performance is an argument neither for nor against it.

Why those four n

8 is HashMap::with_capacity(8) in H2StreamTable::new; 100 is DEFAULT_MAX_CONCURRENT_STREAMS; 1000 is an operator-raised cap; 10000 is the MAX_SAFE_CONCURRENT_STREAMS clamp ceiling. Realistic is 8 to 100 — the other two are there to locate the crossover, not because they are expected.

Access pattern, counted rather than assumed

58 production call sites, 10 test. Only 13 of 58 change complexity class — 11 point lookups, 1 insert, 1 remove. 29 are len/is_empty (O(1) either way) and 16 already walk all n. What matters is frequency, not site count: one lookup per inbound frame, but n lookups plus a key walk and a sort per writable pass. The write pass, not the frame handler, is the heaviest consumer.

Method

Criterion 0.8.2, 60 cells, two clean passes on a 20-core box measured at 83.6 % idle with load sampled every 5 s throughout. Run-to-run noise floor: median 5.6 %, p75 10.7 %, p90 18.1 %. Comparisons are called decisive only when they clear that floor; several at n = 100 are reported as suggestive rather than decisive, and one as below noise.

An earlier pass is recorded in the vault note as discarded, with reasons: load reached 25.49 mid-run from a concurrent build, taking churn CV to 119.5 % against get_hit's 1.4-5.5 %, and it predated the seeded probe shuffle and the varied urgency arm — so it measured a different, BTreeMap-flattering benchmark.

That varied arm matters and is why it is there: a uniform-only write pass overstates BTreeMap at high n (1.11 against 2.16).

What this benchmark cannot tell you

Whether +50 ns per frame is material. Total per-frame cost was never measured — that needs a seam past pub(super), which was out of scope. The ratio is solid; its significance against the whole frame path is not established here.

Also worth weighing and invisible to any timing: a seeded hasher gives a per-build order guarantee only, since DefaultHasher's output is unspecified across std versions. BTreeMap's ordering is a language guarantee.

A production doc claim that is wrong

Found while counting access sites, and deliberately not fixed here — this changeset is benchmark-only.

lib/src/protocol/mux/h2_stream_table.rs:76 states streams "is NOT a determinism leak", justifying the update_initial_window_size site as applying "an additive delta to each stream's own window field independently and OR-combines a bool — both order-independent."

That is true of each element's update and false of the loop. update_initial_window_size has None => return true on checked_add overflow, so which prefix of streams was mutated before the abort is iteration-order dependent. Per-element commutativity does not cover early termination.

Scope stated honestly: the early return leads to a GOAWAY and teardown, so whether the divergent partial mutation reaches the wire depends on the teardown and retry path. That was not traced to a conclusion. What is established is that the doc's stated reason is incomplete.

All three order-dependent sites behind streams were confirmed by reading: handle_goaway_frame (retry order feeding pending_links), update_initial_window_size (the partial-update set above), and ConnectionH2::close (teardown notify order).

Scope

Two files: lib/benches/h2_stream_table.rs and a four-line [[bench]] entry in lib/Cargo.toml. No CHANGELOG entry — the only prior standalone bench commit added none, and a benchmark changes no behaviour, API, route, metric, config key or CLI flag. No new dependency: criterion and rand are already dev-dependencies. The benchmark is standalone, importing only std, criterion and rand; it constructs its own containers and imports nothing from sozu_lib.

streams is not converted. That is a separate changeset if it happens.

Verification

Base 8248c782. Never --all-features except cargo doc.

fmt                exit 0
build              exit 0
clippy --all-targets  exit 0   -D warnings, 0 diagnostics
test -p sozu-lib   1110 + 1 + 1 + 5 = 1117 passed   (baseline at 8248c782: identical)
test -p sozu       226 passed
test -p sozu-e2e   447 passed, 4 filtered out
test -p sozu-sim   28 passed   (tokio_unstable)
clippy -p sozu-sim exit 0   -D warnings
cite --root .      exit 0
cite --base 8248c782   exit 0, 323 cited lines compared, 0 exempt
rustdoc            exit 0
cargo bench --bench h2_stream_table -- --test   exit 0, 60 Testing / 60 Success

The bench smoke-run's 60 is exactly 3 containers x 4 stream counts x (get_hit + get_miss + churn + 2 write_pass distributions), so every function compiles and executes its body — nothing is dead wiring.

That clippy --all-targets really covered the bench was proved rather than assumed: the unit graph from --message-format=json contains ('[email protected]', 'h2_stream_table', ('bench',)), and touching the bench and re-running returned fresh=False with 0 diagnostics.

Refs #1338

Criterion benchmark comparing HashMap with RandomState, BTreeMap, and
HashMap with BuildHasherDefault<DefaultHasher> for H2StreamTable's wire
map, under the operation mix the production call sites actually produce
rather than a synthetic get loop: one lookup per inbound frame, and one
key walk plus one lookup per stream per writable pass.

Four groups (get hit, get miss, steady-state churn, and the composite
write pass) at n = 8 / 100 / 1000 / 10000 -- the with_capacity hint in
H2StreamTable::new, the advertised DEFAULT_MAX_CONCURRENT_STREAMS, an
operator-raised cap, and the clamped MAX_SAFE_CONCURRENT_STREAMS
ceiling.

Result over two clean passes with a 10.7 % run-to-run noise floor: at
realistic n the BTreeMap is equal or faster on every group and 1.7-2.3x
faster at n = 8, because SipHash-1-3's ~9 ns constant over a u32
dominates a tree that is one cache line deep at that size. The crossover
sits between n = 100 and n = 1000; at n = 10000 the BTreeMap costs 6.4x
per lookup. A fixed-seed hasher is below noise against RandomState in
all 20 cells.

Two method choices that materially change the answer: probe order is a
seeded shuffle, since ascending probes keep one BTreeMap leaf cache-hot
and roughly halve its measured lookup cost, and the write pass is
measured under two urgency distributions, since uniform urgency alone
hands the BTreeMap a presorted sort key and overstates it at high n.

Measurement only: no production behaviour changes, and streams is not
converted.

Refs #1338

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit d08211f into main Sep 24, 2026
51 of 52 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the bench/h2-stream-table-container branch September 24, 2026 11:52
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