Skip to content

perf(lazy-cell): drive initialization and broadcast completion via OnceState - #447

Merged
tisonkun merged 3 commits into
mainfrom
perf/lazy-cell-once-state
Nov 21, 2026
Merged

tisonkun merged 3 commits into
mainfrom
perf/lazy-cell-once-state

Conversation

@tisonkun

Copy link
Copy Markdown
Member

Summary

Unify LazyCell onto OnceState alongside two follow-up refinements to OnceState and CHANGELOG.md from #446:

  1. Bounded overflow reclamation in 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 contended AcquireInit poll and drop paths so uncontended callers inline cleanly.
  2. Changelog broadcast wording: Clarify that OnceCell::get_or_try_init broadcasts wakeups when initialization succeeds, whereas failed attempts hand leadership to at most one queued candidate.
  3. LazyCell on OnceState (Reduce the cost of forcing a fresh LazyCell #399): Replace LazyCell's internal asyncband::mutex::Mutex<State<F, Fut>> with UnsafeCell<State<F, Fut>> coordinated by OnceState, flatten drive_pinned_attempt into a synchronous poll helper to shrink forcing futures, and replace thread::panicking() checks on normal paths with a cold PoisonOnUnwind guard. 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:

Benchmark Rounds v0.8.0 main (c0ac0c1) This PR Change vs main
once::force::force_lazy 7,000,000 271 ms 126 ms 66 ms -47.6%
once::force::force_lazy_from_future 7,000,000 271 ms 124 ms 61 ms -50.8%
once::force::forced_lazy 30,000,000 95 ms 95 ms 90 ms -5.3%
once::force::force_lazy_waiter_batch/1 4,000,000 502 ms 454 ms 384 ms -15.4%
once::force::force_lazy_waiter_batch/4 1,000,000 249 ms 243 ms 207 ms -14.8%
once::force::force_lazy_waiter_batch/16 250,000 218 ms 220 ms 163 ms -25.9%

On 64-bit macOS, LazyCell<usize, Pending<usize>> shrinks from 128 B to 96 B and its force future shrinks from 104 B to 56 B, with zero heap allocations on uncontended or single-waiter forcing.

Fixes #399
Supersedes #414
Part of #445

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
@tisonkun
tisonkun merged commit 7c39e3c into main Nov 21, 2026
9 checks passed
@tisonkun
tisonkun deleted the perf/lazy-cell-once-state branch November 21, 2026 21:25
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.

Reduce the cost of forcing a fresh LazyCell

1 participant