Skip to content

perf(once-map): build FromIterator unlocked and reuse duplicate-key entries - #453

Merged
tisonkun merged 3 commits into
mainfrom
perf/keyed-coalescing-and-elastic-shrink
Nov 22, 2026
Merged

tisonkun merged 3 commits into
mainfrom
perf/keyed-coalescing-and-elastic-shrink

Conversation

@tisonkun

@tisonkun tisonkun commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Part of #445 (Wave 2).
Closes #355.

OnceMap::from_iter previously inserted each (K, V) pair by delegating through the general OnceMap insertion path, acquiring the table mutex on every element and allocating a fresh Arc<Entry<K, V>> whenever duplicate keys appeared.

This pull request populates the backing HashTable directly before wrapping it in Mutex, and updates duplicate-key entries in place via HashTable::find_mut and Arc::get_mut without reallocating the Arc or pre-reserving extra buckets on full tables. It supersedes #355 while retaining Co-authored-by credit and preserving the duplicate-key destructor panic cleanup from #409.

Benchmarks

Apple M4 Max (cargo bench -p benchmarks --bench primitives -- once_map::build, median of 5 runs):

Benchmark Rounds main (7054cef) This PR (c685930) Change
once_map::build::collect_distinct_keys/64 93.75k 159 ms 136 ms -14.5%
once_map::build::collect_distinct_keys/1024 5,859 160 ms 139 ms -13.1%
once_map::build::collect_duplicate_keys/64 93.75k 157 ms 87 ms -44.6%
once_map::build::collect_duplicate_keys/1024 5,859 149 ms 84 ms -43.6%

tisonkun and others added 2 commits November 22, 2026 22:51
Move completed values out of `Entry` via `Arc::try_unwrap` whenever the caller
holds the sole remaining reference at retrieval time, eliminating `V::clone()`
on uncontended `work` and `try_work` calls without adding state to `Group`.

Signed-off-by: tison <[email protected]>
…ntries

Populate the backing `HashTable` before wrapping it in `Mutex` in
`OnceMap::from_iter`, avoiding per-element lock acquisitions and updating
existing `Entry` slots in place via `find_mut` and `Arc::get_mut` on duplicate
keys without extra allocations or bucket pre-reservation.

Also remove the single-use `Entries` alias and stale sharded-table comments,
and add `once_map::build` benchmarks for distinct- and duplicate-key
collection.

Co-authored-by: Orthur <[email protected]>
Signed-off-by: tison <[email protected]>
@tisonkun
tisonkun force-pushed the perf/keyed-coalescing-and-elastic-shrink branch from 1864e91 to 62f394c Compare November 22, 2026 14:54
@tisonkun tisonkun changed the title perf: avoid uncontended singleflight clones, speed up OnceMap::from_iter, and shrink idle burst capacity perf: avoid sole-owner singleflight clones and speed up OnceMap::from_iter Nov 22, 2026
This reverts commit c36a5d4.

`singleflight::Group` requires `V: Clone` to share values across coalesced callers, so idiomatic callers pass cheaply cloned payloads (`Arc<T>`, `Bytes`, or scalars). Because `OnceCell::get_or_try_init` already returns `&V`, discarding that reference to attempt `Arc::try_unwrap` on the entry regresses uncontended scalar calls (`leader_success`: +5.4%) and adds failed atomic `compare_exchange` traffic across shared `Arc` headers on coalesced fan-out (`complete_coalesced/16`: +26.5%). Restore the direct `v.clone()` return path so idiomatic workloads pay no atomic unwrap overhead.

Signed-off-by: tison <[email protected]>
@tisonkun tisonkun changed the title perf: avoid sole-owner singleflight clones and speed up OnceMap::from_iter perf(once-map): build FromIterator unlocked and reuse duplicate-key entries Nov 22, 2026
@tisonkun
tisonkun merged commit dfd11c7 into main Nov 22, 2026
9 checks passed
@tisonkun
tisonkun deleted the perf/keyed-coalescing-and-elastic-shrink branch November 22, 2026 15:38
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