Repository navigation
bench(h2): measure the stream-table container choice for #1338 - #1502
Merged
Merged
Conversation
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
requested review from
Wonshtrum and
llenotre
as code owners
September 24, 2026 10:13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A criterion benchmark for the one open question in #1338: should
H2StreamTable::streamsbecome aBTreeMap, or keep aHashMapwith 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)beatsO(log n)on the per-frame hot path. Measured, that assumption does not hold at thenthat actually occurs.btree / fixed-seedratios, two-run mean (below 1.00 meansBTreeMapis faster):get_hitget_misschurnwrite_passuniformwrite_passvariedAt
n = 8BTreeMapis 1.7-2.3x faster on every group. Atn = 100it is equal or faster on all five comparisons and slower on none. The mechanism: SipHash-1-3 over au32costs about 9 ns regardless ofn, while aBTreeMapof 11 or fewer keys is a single cache line. Per-frame lookup is 4.0 ns against 9.4 ns atn = 8. Being on the hot path is what makes the tree better here, not worse.BTreeMaploses past the crossover, which sits between 100 and 1000: about +50 ns per frame lookup atn = 10000.Separately, a fixed-seed hasher is below noise against today's
RandomStatein all 20 cells, so performance is an argument neither for nor against it.Why those four
n8isHashMap::with_capacity(8)inH2StreamTable::new;100isDEFAULT_MAX_CONCURRENT_STREAMS;1000is an operator-raised cap;10000is theMAX_SAFE_CONCURRENT_STREAMSclamp 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 alln. What matters is frequency, not site count: one lookup per inbound frame, butnlookups 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 = 100are 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
churnCV to 119.5 % againstget_hit's 1.4-5.5 %, and it predated the seeded probe shuffle and thevariedurgency arm — so it measured a different,BTreeMap-flattering benchmark.That
variedarm matters and is why it is there: a uniform-only write pass overstatesBTreeMapat highn(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:76statesstreams"is NOT a determinism leak", justifying theupdate_initial_window_sizesite as applying "an additive delta to each stream's ownwindowfield independently and OR-combines abool— both order-independent."That is true of each element's update and false of the loop.
update_initial_window_sizehasNone => return trueonchecked_addoverflow, 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
streamswere confirmed by reading:handle_goaway_frame(retry order feedingpending_links),update_initial_window_size(the partial-update set above), andConnectionH2::close(teardown notify order).Scope
Two files:
lib/benches/h2_stream_table.rsand a four-line[[bench]]entry inlib/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 andrandare already dev-dependencies. The benchmark is standalone, importing onlystd,criterionandrand; it constructs its own containers and imports nothing fromsozu_lib.streamsis not converted. That is a separate changeset if it happens.Verification
Base
8248c782. Never--all-featuresexceptcargo doc.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-targetsreally covered the bench was proved rather than assumed: the unit graph from--message-format=jsoncontains('[email protected]', 'h2_stream_table', ('bench',)), and touching the bench and re-running returnedfresh=Falsewith 0 diagnostics.Refs #1338