Skip to content

perf(mpsc): coalesce unbounded receiver wakeups - #231

Closed
tisonkun wants to merge 2 commits into
mainfrom
codex/optimize-unbounded-mpsc
Closed

perf(mpsc): coalesce unbounded receiver wakeups#231
tisonkun wants to merge 2 commits into
mainfrom
codex/optimize-unbounded-mpsc

Conversation

@tisonkun

@tisonkun tisonkun commented Aug 28, 2026

Copy link
Copy Markdown
Member

Summary

  • avoid shared write operations on every unbounded send when no receive call is parked
  • retain the standard-library queue core and the existing public API without adding dependencies
  • add deterministic coverage for burst notification coalescing and last-sender disconnect

A local Apple Silicon comparison used the existing ecosystem benchmark with two seconds per benchmark. Median latency changed as follows:

Case main This PR Change
ready round trip 9.185 ns 7.340 ns -20.1%
try round trip 8.290 ns 6.566 ns -20.8%
concurrent, 1 producer 177.9 us 148.6 us -16.5%
concurrent, 2 producers 1.003 ms 871 us -13.2%
concurrent, 4 producers 2.124 ms 1.789 ms -15.8%
concurrent, 8 producers 3.498 ms 3.551 ms +1.5%

The eight-producer result is effectively unchanged; contention in the standard-library MPMC queue remains the limiting factor there.

Design Notes

The queue remains the durable source of truth. The receiver follows register waker -> publish waiting -> recheck queue, while a successful sender follows enqueue -> claim waiting -> wake. The waiting flag uses sequentially consistent operations.

If a sender claims the flag, it owns the wake. If it observes the pre-waiting false value, its enqueue precedes the receiver's subsequent queue recheck, so the receiver observes the message instead of sleeping. Clearing the flag after a successful recheck prevents stale notification ownership. Last-sender disconnect uses the same notification path after dropping the standard-library sender.

This keeps the proof local to notification delivery, adds no unsafe code or dependency, and intentionally leaves replacement of the queue backend in #209 out of scope.

Validated with cargo x test, cargo x check, and cargo x lint.

@tisonkun
tisonkun marked this pull request as draft August 28, 2026 14:57
@tisonkun tisonkun closed this Aug 28, 2026
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