Repository navigation
perf(lazy-cell): drive initialization and broadcast completion via OnceState - #447
Merged
Merged
Conversation
When overflow initializers and observers both drain while a waiter still occupies the inline slot, drop overflow allocations whose backing capacity grew past a small bounded cache (8 slots) while retaining small allocations for repeated short-lived cancellations and releasing all overflow storage once the inline slot also empties. Also outline contended `AcquireInit` poll and drop paths so uncontended `OnceState` callers inline cleanly.
…alization `OnceCell::get_or_try_init` returning `Err` hands leadership to at most one queued candidate rather than broadcasting; only successful initialization wakes all queued callers at once.
…ceState Replace `LazyCell`'s internal `asyncband::mutex::Mutex<State<F, Fut>>` with `UnsafeCell<State<F, Fut>>` coordinated by `OnceState` so fresh uncontended forcing takes direct atomic state transitions and completed or poisoned initialization broadcasts wakeups to all queued callers at once. Previously, concurrent callers waiting on `LazyCell::force` queued on the state mutex: completion or poisoning woke followers serially, and a paused head waiter blocked trailing callers indefinitely. Using `OnceState` hands leadership to at most one FIFO successor when an in-flight caller cancels (preserving the pinned attempt in place) while broadcasting terminal completion and poisoning to every waiting caller. Also flatten `drive_pinned_attempt` into a synchronous poll helper to shrink `LazyCell` forcing futures and replace `thread::panicking()` checks with a cold `PoisonOnUnwind` guard that is forgotten on normal returns. Fixes #399
8 tasks done
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
Unify
LazyCellontoOnceStatealongside two follow-up refinements toOnceStateandCHANGELOG.mdfrom #446:OnceState: Drop empty overflow allocations whose backing capacity grew past a small 8-slot cache even while an inline waiter remains queued, retaining small allocations for repeated short-lived cancellations and freeing all overflow storage once the inline slot also empties. Outline contendedAcquireInitpoll and drop paths so uncontended callers inline cleanly.OnceCell::get_or_try_initbroadcasts wakeups when initialization succeeds, whereas failed attempts hand leadership to at most one queued candidate.LazyCellonOnceState(Reduce the cost of forcing a fresh LazyCell #399): ReplaceLazyCell's internalasyncband::mutex::Mutex<State<F, Fut>>withUnsafeCell<State<F, Fut>>coordinated byOnceState, flattendrive_pinned_attemptinto a synchronous poll helper to shrink forcing futures, and replacethread::panicking()checks on normal paths with a coldPoisonOnUnwindguard. Cancelling an active driver still hands leadership to at most one FIFO successor to resume the same pinned future in place, while completion and poisoning now wake every queued caller at once so a paused head waiter cannot stall trailing callers.Benchmarks
Apple M4 Max (aarch64-apple-darwin), 7-run medians via
cargo x bench --bench primitives -- lazy --repeat 7:v0.8.0main(c0ac0c1)mainonce::force::force_lazyonce::force::force_lazy_from_futureonce::force::forced_lazyonce::force::force_lazy_waiter_batch/1once::force::force_lazy_waiter_batch/4once::force::force_lazy_waiter_batch/16On 64-bit macOS,
LazyCell<usize, Pending<usize>>shrinks from 128 B to 96 B and itsforcefuture shrinks from 104 B to 56 B, with zero heap allocations on uncontended or single-waiter forcing.Fixes #399
Supersedes #414
Part of #445