Skip to content

perf(mpsc): replace boxed receiver wakers - #182

Merged
tisonkun merged 3 commits into
mainfrom
codex/atomic-waker
Aug 24, 2026
Merged

perf(mpsc): replace boxed receiver wakers#182
tisonkun merged 3 commits into
mainfrom
codex/atomic-waker

Conversation

@tisonkun

Copy link
Copy Markdown
Member

Summary

  • replace the heap-allocated AtomicOptionBox<Waker> used by bounded and unbounded MPSC receivers with an internal AtomicWaker
  • keep the MPSC feature dependency-free and inline the registration and wake-up hot paths
  • preserve notification ordering across register/wake races and recover cleanly from panicking custom wakers
  • benchmark repeated receiver registration and a complete pending/wake/receive cycle

Rationale

Every pending receiver poll currently clones a Waker, allocates a Box, atomically exchanges its pointer, and drops the previous allocation. The new state machine stores the waker inline and skips cloning when the registered waker already targets the same task.

The existing check-register-check sequence remains unchanged, so a wake that happens before registration is still observed through the second queue check. A wake that races with registration is completed by whichever side owns the inline waker slot.

The implementation remains private to Asyncband instead of adding an atomic-waker-style dependency. On 64-bit targets this trades approximately 16 additional inline bytes per channel state for eliminating allocations on pending polls.

Benchmarks

Local median results from repeated runs of cargo bench -p benchmarks --bench benchmarks -- mpsc::reregister_pending_receiver mpsc::wake_pending_receiver:

Benchmark Before After Change
reregister_pending_receiver 20.3–21.2 ns 5.8–6.3 ns 3.3–3.6x faster
wake_pending_receiver 31.3–32.0 ns 16.7–21.5 ns 1.5–1.9x faster

Validation

  • cargo +nightly x lint
  • cargo +nightly x check
  • cargo +1.86.0 x test
  • cargo test --workspace --all-features

Signed-off-by: tison <wander4096@gmail.com>
Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
tisonkun merged commit c505b91 into main Aug 24, 2026
9 checks passed
@tisonkun
tisonkun deleted the codex/atomic-waker branch August 24, 2026 03:32
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