Repository navigation
Conversation
`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>
Member
Author
|
Closed as later. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mpsc::boundedpreviously stored buffered messages in aVecDeque<T>behind the sameMutexthat managed endpoint liveness and parked wakers. Everysend,try_send,Permit::send, andrecvacquired that mutex and copiedTwhile holding it, serializing multi-producer1 KiBinline transfers across cores and paying extra round-trip moves onTrySendError::Full(value).On targets with 64-bit atomics, this change preallocates
buffer + 1physicalMaybeUninit<T>slots directly on the heap (falling back transparently toMutex<State<T>>for zero-sizedT, capacities whose auxiliary arrays exceedLayoutlimits or(1 << 30) - 1, or targets without 64-bit atomics) so producers claim an index before movingT, copy payloads outside any lock, and touchMutex<State<T>>only when parking or closing:BoundedReceiver(&mut self) through a cache-line-isolated single-consumerfree_ring(free_tailvs producerfree_headandcached_free_tail), while routing coldPermitcancellations throughMutex<State<T>>so cancelled permits hand off directly to waiting senders without ABA tag wraparound or store-load races.inboxstack withOrdering::Release, which the receiver detaches in batches and reverses into its privatelocal_headFIFO chain without spin loops or publication holes.Pool<T>arrays behind a wait-free lease count held only by the liveBoundedReceiverand activeacquire_slotorPermitoperations so dropping the receiver reclaims the slot and index allocations immediately even when idle disconnectedBoundedSenderhandles remain alive.Closes #401.
Benchmarks
5-run medians (
cargo bench --package benchmarks --bench ecosystemand--bench primitives, lower is better):main(f199ce9)19772b7)v1.53.1)mpsc/bounded/tasks-4p1c-cap64-4w-inline1k(#401)309 ms63 ms-79.6%94 msmpsc/bounded/tasks-4p1c-cap64-4w-boxed1k115 ms45 ms-60.9%99 msmpsc/bounded/threads-4p1c-cap64465 ms180 ms-61.3%248 msmpsc/bounded/tasks-1p1c-cap1024-4w182 ms112 ms-38.5%345 msmpsc/bounded/try-roundtrip317 ms240 ms-24.3%365 msmpsc/bounded/ready-roundtrip295 ms226 ms-23.4%522 msmpsc/bounded/tasks-1p1c-cap64-4w325 ms281 ms-13.5%516 msmpsc/bounded/ready-roundtrip-inline1k153 ms143 ms-6.5%148 msmpsc/bounded/ready-roundtrip-boxed1k51 ms48 ms-5.9%62 msmpsc/bounded/external-receiver-1p1c-cap64-4w284 ms263 ms-7.4%274 msmpsc/bounded/tasks-1p1c-cap64-current295 ms282 ms-4.4%497 msmpsc/bounded/tasks-1p1c-cap1024-current270 ms261 ms-3.3%498 msmpsc/bounded/tasks-4p1c-cap64-current284 ms277 ms-2.5%475 msmpsc/bounded/tasks-4p1c-cap64-4w298 ms308 ms+3.4%548 msmpsc/bounded/tasks-4p1c-cap1-4w304 ms354 ms+16.4%358 msmpsc/bounded/tasks-4p1c-cap1024-4w283 ms474 ms+67.5%552 msmpsc/reservation/reserve-publish-receive276 ms169 ms-38.8%357 msmpsc/reservation/tasks-4p1c-cap1-4w335 ms398 ms+18.8%374 msprimitives::mpsc::bounded::drain_backpressured_senders/1324 ms314 ms-3.1%primitives::mpsc::bounded::drain_backpressured_senders/4260 ms244 ms-6.2%primitives::mpsc::bounded::drain_backpressured_senders/16226 ms209 ms-7.5%primitives::mpsc::bounded::cancel_backpressured_senders/1429 ms415 ms-3.3%primitives::mpsc::bounded::cancel_backpressured_senders/4306 ms300 ms-2.0%primitives::mpsc::bounded::cancel_backpressured_senders/16262 ms241 ms-8.0%Tradeoffs:
tasks-4p1c-cap1024-4w(usizescalar 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 (still1.16xfaster than Tokio's552 ms).cap1-4w, every message parks and wakes senders throughMutex<State<T>>after an initial lock-free check, adding one extra atomic probe before entering the wait queue.O(batch)steps before subsequent receives pop inO(1)fromlocal_head.Verification
cargo x lintcargo x checkcargo x testcargo x bench -- --testcargo x semver --release-version 0.8.1cargo +1.86.0 check -p asyncband --all-featuresRUSTFLAGS="-Dwarnings" cargo +nightly check -Zbuild-std=std,panic_abort --target powerpc-unknown-linux-gnu -p asyncband --all-featuresMIRIFLAGS="-Zmiri-many-seeds=0..4" cargo +nightly miri test -p asyncband --lib mpsc::boundedMIRIFLAGS="-Zmiri-many-seeds=0..4" cargo +nightly miri test -p tests-integration --test mpsc_test