Repository navigation
refactor: replace fixed coordination shards - #239
Merged
Merged
Conversation
Signed-off-by: tison <[email protected]>
tisonkun
marked this pull request as ready for review
August 30, 2026 02:41
This was referenced Nov 22, 2026
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.
Summary
OnceMapas one safehashbrown::HashTable<Arc<Entry<K, V>>>guarded by a short-held mutex, with per-entryOnceCellstatesingleflight::Groupwith one short-held registry lock that is released before user work runsOnceMap::removeanddiscard, including detached in-flight generations and cancellation cleanupDesign Notes
OnceMapnow has nounsafe, no fixed shard count, no eager heap allocation, and no new dependency. The table lock protects only entry ownership: lookup, insertion, and unlinking. Value cloning, user computation, formatting, and key/value destruction happen after the lock is released.Each table entry is an
Arccontaining the key and itsOnceCell. Callers for the same key share that cell, while callers for different keys only serialize during the short registry operation. A cancelled or failed initializer removes an entry only when the table and its cleanup guard are the final owners.removeanddiscarddetach the current generation without cancelling callers that already hold it.This deliberately chooses a small, safe baseline over the atomic trie and deferred-reclamation experiments explored during development. It keeps construction and cold paths cheap and makes removal ordinary hash-table work, but a single registry mutex cannot provide parallel distributed-key reads under heavy thread contention. That tradeoff is explicit so a later optimization can be justified by representative workloads rather than by a fixed shard count or a large unsafe reclamation protocol.
singleflight::Groupfollows the same short-held-registry model as Go'ssingleflight: the lock covers admission and cleanup, never the user future. Its intended path is duplicate suppression; many unrelated, extremely short leaders can still contend on the registry.Benchmark Design
The benchmark names now encode the state being measured:
once_map::cold: absent-key success/error and deterministic same-key versus distinct-key initialization batchesonce_map::hot: ready hit/miss and cached compute under 1, 2, 8, and 32 threadsonce_map::maintenance: existing and missingremove/discardpathssingleflight::cold: leader lifecycle and concurrent distinct-key leaderssingleflight::hot: admission to an existing in-flight call, successful fan-in, and retry after leader errorSame-key coalescing is driven deterministically by suspending the leader and polling all waiters. Cross-thread benchmarks are used only for stable hot state. Input construction and large drops remain outside timed sections, batch benchmarks report item throughput, and fixed sample sizes keep one-time TLS or allocator work from changing Divan's iteration granularity.
OnceMap versus
mainThe values below are ranges from two isolated run medians on the same 14-core Apple Silicon host using the exact same benchmark source; lower is better.
mainThe result is not a general throughput win. It is a simpler safe baseline that improves construction, initialization, same-key coordination, and removal, while explicitly giving up
main's sharded parallelism for many-thread distributed hot reads.SingleFlight versus
mainThese are means of three run medians from the same host and benchmark source; lower is better. The final
OnceMaprewrite does not affect these results.mainThis improves construction and the duplicate-suppression path. The benchmark also exposes the cost of serializing many unrelated, very short leaders; for expensive user work the absolute coordination cost is smaller, but the limitation remains part of the design tradeoff.
Validation
cargo x checkcargo x lintcargo x testmainbenchmark runs