Skip to content

perf(mpsc): decouple bounded payload storage from wait-queue locking - #457

Closed
tisonkun wants to merge 1 commit into
mainfrom
perf/mpsc-bounded-slot-pool
Closed

tisonkun wants to merge 1 commit into
mainfrom
perf/mpsc-bounded-slot-pool

Conversation

@tisonkun

Copy link
Copy Markdown
Member

Summary

mpsc::bounded previously stored buffered messages in a VecDeque<T> behind the same Mutex that managed endpoint liveness and parked wakers. Every send, try_send, Permit::send, and recv acquired that mutex and copied T while holding it, serializing multi-producer 1 KiB inline transfers across cores and paying extra round-trip moves on TrySendError::Full(value).

On targets with 64-bit atomics, this change preallocates buffer + 1 physical MaybeUninit<T> slots directly on the heap (falling back transparently to Mutex<State<T>> for zero-sized T, capacities whose auxiliary arrays exceed Layout limits or (1 << 30) - 1, or targets without 64-bit atomics) so producers claim an index before moving T, copy payloads outside any lock, and touch Mutex<State<T>> only when parking or closing:

  • Recycle slots from BoundedReceiver (&mut self) through a cache-line-isolated single-consumer free_ring (free_tail vs producer free_head and cached_free_tail), while routing cold Permit cancellations through Mutex<State<T>> so cancelled permits hand off directly to waiting senders without ABA tag wraparound or store-load races.
  • Publish initialized slots into a cache-line-isolated inbox stack with Ordering::Release, which the receiver detaches in batches and reverses into its private local_head FIFO chain without spin loops or publication holes.
  • Gate backing Pool<T> arrays behind a wait-free lease count held only by the live BoundedReceiver and active acquire_slot or Permit operations so dropping the receiver reclaims the slot and index allocations immediately even when idle disconnected BoundedSender handles remain alive.

Closes #401.

Benchmarks

5-run medians (cargo bench --package benchmarks --bench ecosystem and --bench primitives, lower is better):

Scenario main (f199ce9) This PR (19772b7) Change Tokio (v1.53.1)
mpsc/bounded/tasks-4p1c-cap64-4w-inline1k (#401) 309 ms 63 ms -79.6% 94 ms
mpsc/bounded/tasks-4p1c-cap64-4w-boxed1k 115 ms 45 ms -60.9% 99 ms
mpsc/bounded/threads-4p1c-cap64 465 ms 180 ms -61.3% 248 ms
mpsc/bounded/tasks-1p1c-cap1024-4w 182 ms 112 ms -38.5% 345 ms
mpsc/bounded/try-roundtrip 317 ms 240 ms -24.3% 365 ms
mpsc/bounded/ready-roundtrip 295 ms 226 ms -23.4% 522 ms
mpsc/bounded/tasks-1p1c-cap64-4w 325 ms 281 ms -13.5% 516 ms
mpsc/bounded/ready-roundtrip-inline1k 153 ms 143 ms -6.5% 148 ms
mpsc/bounded/ready-roundtrip-boxed1k 51 ms 48 ms -5.9% 62 ms
mpsc/bounded/external-receiver-1p1c-cap64-4w 284 ms 263 ms -7.4% 274 ms
mpsc/bounded/tasks-1p1c-cap64-current 295 ms 282 ms -4.4% 497 ms
mpsc/bounded/tasks-1p1c-cap1024-current 270 ms 261 ms -3.3% 498 ms
mpsc/bounded/tasks-4p1c-cap64-current 284 ms 277 ms -2.5% 475 ms
mpsc/bounded/tasks-4p1c-cap64-4w 298 ms 308 ms +3.4% 548 ms
mpsc/bounded/tasks-4p1c-cap1-4w 304 ms 354 ms +16.4% 358 ms
mpsc/bounded/tasks-4p1c-cap1024-4w 283 ms 474 ms +67.5% 552 ms
mpsc/reservation/reserve-publish-receive 276 ms 169 ms -38.8% 357 ms
mpsc/reservation/tasks-4p1c-cap1-4w 335 ms 398 ms +18.8% 374 ms
primitives::mpsc::bounded::drain_backpressured_senders/1 324 ms 314 ms -3.1% -
primitives::mpsc::bounded::drain_backpressured_senders/4 260 ms 244 ms -6.2% -
primitives::mpsc::bounded::drain_backpressured_senders/16 226 ms 209 ms -7.5% -
primitives::mpsc::bounded::cancel_backpressured_senders/1 429 ms 415 ms -3.3% -
primitives::mpsc::bounded::cancel_backpressured_senders/4 306 ms 300 ms -2.0% -
primitives::mpsc::bounded::cancel_backpressured_senders/16 262 ms 241 ms -8.0% -

Tradeoffs:

  • On tasks-4p1c-cap1024-4w (usize scalar bursts with zero per-message work across 4 worker threads), an unfair OS mutex (main) parks 3 of the 4 threads so one core pushes sequentially in L1 cache, whereas lock-free MPSC keeps all 4 cores concurrently executing atomic updates on shared cursors (still 1.16x faster than Tokio's 552 ms).
  • On cap1-4w, every message parks and wakes senders through Mutex<State<T>> after an initial lock-free check, adding one extra atomic probe before entering the wait queue.
  • After a large unconsumed burst, the first receive reverses the detached LIFO chain in place in O(batch) steps before subsequent receives pop in O(1) from local_head.

Verification

  • cargo x lint
  • cargo x check
  • cargo x test
  • cargo x bench -- --test
  • cargo x semver --release-version 0.8.1
  • cargo +1.86.0 check -p asyncband --all-features
  • RUSTFLAGS="-Dwarnings" cargo +nightly check -Zbuild-std=std,panic_abort --target powerpc-unknown-linux-gnu -p asyncband --all-features
  • MIRIFLAGS="-Zmiri-many-seeds=0..4" cargo +nightly miri test -p asyncband --lib mpsc::bounded
  • MIRIFLAGS="-Zmiri-many-seeds=0..4" cargo +nightly miri test -p tests-integration --test mpsc_test

`mpsc::bounded` previously stored buffered messages in a `VecDeque<T>` behind the same `Mutex` that managed endpoint liveness and parked wakers. Every `send`, `try_send`, `Permit::send`, and `recv` acquired that mutex and copied `T` while holding it, serializing multi-producer `1 KiB` inline transfers and paying extra round-trip moves on `TrySendError::Full(value)`.

On targets with 64-bit atomics, preallocate `buffer + 1` physical `MaybeUninit<T>` slots directly on the heap (falling back transparently to `Mutex<State<T>>` for zero-sized `T`, capacities above `(1 << 30) - 1`, or targets without 64-bit atomics) so producers claim an index before moving `T`, write and read payloads outside any lock, and synchronize parked tasks through `Mutex<State<T>>` only when capacity is exhausted or the receiver is empty:

- Recycle slots from `BoundedReceiver` (`&mut self`) through a cache-line-isolated single-consumer `free_ring` (`free_tail` vs producer `free_head` and `cached_free_tail`), while routing cold `Permit` cancellations through `Mutex<State<T>>` so cancelled permits coordinate directly with waiting senders without ABA tag wraparound or store-load races.
- Publish initialized slots into a cache-line-isolated `inbox` stack with `Ordering::Release`, which the receiver detaches in batches and reverses into its private `local_head` FIFO chain without spin loops or publication holes.
- Gate backing `Pool<T>` storage behind a wait-free lease count held only by the live `BoundedReceiver` and active `acquire_slot` / `Permit` operations so dropping the receiver reclaims the slot arrays immediately even when idle disconnected `BoundedSender` handles remain alive.

Closes #401.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun

Copy link
Copy Markdown
Member Author

Closed as later.

@tisonkun tisonkun closed this Nov 22, 2026
@tisonkun
tisonkun deleted the perf/mpsc-bounded-slot-pool branch November 22, 2026 23:49
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.

Improve bounded MPSC throughput for inline 1 KiB messages

1 participant