Skip to content

refactor: replace fixed coordination shards - #239

Merged
tisonkun merged 14 commits into
apache:mainfrom
tisonkun:codex/adaptive-once-map
Aug 30, 2026
Merged

tisonkun merged 14 commits into
apache:mainfrom
tisonkun:codex/adaptive-once-map

Conversation

@tisonkun

@tisonkun tisonkun commented Aug 29, 2026 •

Copy link
Copy Markdown
Member

Summary

  • replace eager CPU-scaled coordination shards with lazily allocated registries
  • implement OnceMap as one safe hashbrown::HashTable<Arc<Entry<K, V>>> guarded by a short-held mutex, with per-entry OnceCell state
  • implement singleflight::Group with one short-held registry lock that is released before user work runs
  • preserve OnceMap::remove and discard, including detached in-flight generations and cancellation cleanup
  • rewrite both benchmark suites around explicit cold, hot, and maintenance paths

Design Notes

OnceMap now has no unsafe, 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 Arc containing the key and its OnceCell. 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. remove and discard detach 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::Group follows the same short-held-registry model as Go's singleflight: 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 batches
  • once_map::hot: ready hit/miss and cached compute under 1, 2, 8, and 32 threads
  • once_map::maintenance: existing and missing remove / discard paths
  • singleflight::cold: leader lifecycle and concurrent distinct-key leaders
  • singleflight::hot: admission to an existing in-flight call, successful fan-in, and retry after leader error

Same-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 main

The 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.

Workload main This PR Result
Construct default 630–673 ns 2.5–3.0 ns about 99.6% faster
Initialize empty map 83–85 ns about 81 ns about 3.7% faster
Initialize 32 distinct cold keys 3.18–3.22 us 2.88–2.98 us about 8.5% faster
Initialize 32 callers on one cold key about 2.31 us 2.21–2.26 us about 3.3% faster
Same-key cached compute, 32 threads 0.92–1.18 us 0.41–0.68 us about 47.8% faster; noisy but directionally stable
Same-key get, 32 threads 0.30–0.36 us 0.36–0.45 us about 22% slower
Distributed get, 1024 entries, 32 threads 71–89 ns 0.82–1.54 us about 1,375% slower (14.8x)
Distributed miss, 1024 entries, 32 threads 68–120 ns 0.36–0.47 us about 341% slower (4.4x)
Remove hit, 1024 entries 74–75 ns 65–69 ns about 10% faster

The 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 main

These are means of three run medians from the same host and benchmark source; lower is better. The final OnceMap rewrite does not affect these results.

Workload main This PR Result
Construct default 443.47 ns 3.30 ns 99.3% faster
Uncontended successful leader 80.01 ns 71.87 ns 10.2% faster
Join 32 callers to an in-flight key 1.048 us 0.938 us 10.5% faster
Complete 32 coalesced callers 2.376 us 2.288 us 3.7% faster
Distinct-key leaders, 1 thread 97.31 ns 96.54 ns effectively unchanged
Distinct-key leaders, 8 threads 710.23 ns 2.163 us 204.6% slower (3.05x)
Distinct-key leaders, 32 threads 863.33 ns 7.550 us 774.5% slower (8.75x)

This 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 check
  • cargo x lint
  • cargo x test
  • focused Miri coverage for abandoned pending cleanup, colliding ready and pending entries, and proving that one pending computation does not block another key
  • isolated current-versus-main benchmark runs

@tisonkun
tisonkun marked this pull request as ready for review August 30, 2026 02:41
@tisonkun tisonkun changed the title perf: replace fixed coordination shards refactor: replace fixed coordination shards Aug 30, 2026
@tisonkun
tisonkun merged commit 35302a0 into apache:main Aug 30, 2026
9 checks passed
@tisonkun
tisonkun deleted the codex/adaptive-once-map branch September 2, 2026 11:30
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