Skip to content

refactor: streamline ManualResetEvent and bounded broadcast waiters - #444

Merged
tisonkun merged 3 commits into
apache:mainfrom
tisonkun:refactor/event-and-broadcast-waiters
Nov 21, 2026
Merged

tisonkun merged 3 commits into
apache:mainfrom
tisonkun:refactor/event-and-broadcast-waiters

Conversation

@tisonkun

Copy link
Copy Markdown
Member

Summary

  • Replace ManualResetEvent's doubly-linked WaitList with WakerSet and a u64 generation counter, draining wakers in place before storing is_set = true so released waits complete and drop without reacquiring the waiter lock, and fold event::Waiter into auto_reset.
  • Assert real sender wakeups via WakeCounter across bounded broadcast backpressure tests so missed notifications cannot pass silently.
  • Replace broadcast::mpmc::bounded's sender Semaphore and AtomicUsize counter with WaitList<RetryWaiter> inside the channel's existing Inner lock, shrinking BoundedSender::send futures by 16 B (72 B -> 56 B), avoiding spurious follower wakeups when a cancelled notified send races with a refilled slot, and removing #[cfg(feature = "broadcast")] methods from internal::semaphore::Semaphore.

Design Notes

  • ManualResetEvent::set detaches all registered wakers and advances generation before publishing is_set = true with Release ordering. Any Wait future observing is_set() == true therefore has its slot already reclaimed and skips the lock on both poll and Drop; if a reset lands before the woken task runs, generation still identifies the completed cycle under the state lock without requiring 64-bit atomics on 32-bit targets.
  • WakerSet::drain_into populates a caller-owned WakerBatch in place, and broadcast send paths skip constructing a WakerBatch when no receivers are parked, avoiding 544 B stack moves on uncontended sends and set calls.
Metric / Benchmark (aarch64-apple-darwin, 7 alternating paired runs) Before (608d688) After (21cbe56) Delta
size_of::<ManualResetEvent>() 80 B 72 B -8 B
size_of_val(&event.wait_owned()) 56 B 40 B -16 B
size_of_val(&bounded_tx.send(1usize)) 72 B 56 B -16 B
primitives::event::wait::wait_already_set/threads=1 969 ms 798 ms -17.6%
primitives::event::wait::reused_waiter_handoff 339 ms 269 ms -20.6%
primitives::event::wait::waiter_fan_out/16 210 ms 153 ms -27.1%
primitives::event::wait::cancel_pending_reused 332 ms 301 ms -9.3%
primitives::event::wait::set_registered/256 186 ms 132 ms -29.0%
primitives::broadcast::mpmc::bounded::try_send_and_try_recv 233 ms 224 ms -3.9%
primitives::broadcast::mpmc::bounded::full_channel_sender_handoff_cycle/4 152 ms 126 ms -17.1%
primitives::broadcast::mpmc::bounded::deliver_to_waiting_receiver 288 ms 254 ms -11.8%
primitives::broadcast::mpmc::bounded::cancel_blocked_send 222 ms 195 ms -12.2%
primitives::broadcast::mpmc::unbounded::deliver_to_waiting_receiver 280 ms 241 ms -13.9%

ManualResetEvent previously stored waiters in a doubly-linked WaitList and kept detached nodes alive after set(), forcing every woken or cancelled wait to reacquire the waiter lock solely to free its node.\n\nTrack unset cycles with a u64 generation alongside WakerSet and drain wakers in place before publishing is_set, so released waits complete and drop without locking while event::Waiter moves into auto_reset.
Three bounded broadcast backpressure tests previously polled blocked send futures only with Waker::noop(), verifying that a subsequent poll could publish without checking that reclaiming slots actually woke the parked wakers.\n\nPoll those producers with WakeCounter so missed sender notifications cannot pass silently.
Bounded broadcast backpressure wakes producers to recheck the shared backlog rather than reserving capacity, yet previously tracked parked senders through a separate internal Semaphore, an AtomicUsize upper-bound counter, and two broadcast-gated Semaphore methods.\n\nStore WaitList<RetryWaiter> inside the channel's existing Inner lock, shrinking BoundedSender::send futures by 16 B, eliminating spurious follower wakeups when a cancelled notified send races with a refilled slot, and removing the broadcast-only methods from internal::semaphore.
@tisonkun
tisonkun merged commit d436cd9 into apache:main Nov 21, 2026
9 checks passed
@tisonkun
tisonkun deleted the refactor/event-and-broadcast-waiters branch November 21, 2026 01:31
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