Repository navigation
perf(channels): separate producer and consumer progress in bounded MPMC and SPMC queues - #382
Open
ariesdevil wants to merge 1 commit into
Open
ariesdevil wants to merge 1 commit into
ariesdevil wants to merge 1 commit into
Conversation
ariesdevil
force-pushed
the
dev_ariesdevil
branch
4 times, most recently
from
October 21, 2026 08:39
94d174b to
4ea81c4
Compare
tisonkun
reviewed
Oct 21, 2026
tisonkun
reviewed
Oct 21, 2026
tisonkun
reviewed
Oct 21, 2026
tisonkun
reviewed
Oct 21, 2026
Member
|
@ariesdevil You may update the branch to resolve the conflict with the main branch, and I will also adopt the new benchmark best practice that you can leverage. Once this pull request is clean, I will do another review. |
ariesdevil
force-pushed
the
dev_ariesdevil
branch
from
November 5, 2026 07:27
9600dab to
065940a
Compare
tisonkun
reviewed
Nov 5, 2026
ariesdevil
force-pushed
the
dev_ariesdevil
branch
from
November 18, 2026 05:55
3b0752c to
a1284ba
Compare
ariesdevil
force-pushed
the
dev_ariesdevil
branch
2 times, most recently
from
November 19, 2026 06:35
e224174 to
9dafdbb
Compare
ariesdevil
force-pushed
the
dev_ariesdevil
branch
2 times, most recently
from
November 19, 2026 09:09
a7b742b to
e62283f
Compare
8 tasks done
ariesdevil
force-pushed
the
dev_ariesdevil
branch
from
November 22, 2026 12:24
e62283f to
d09d839
Compare
Back bounded MPMC and SPMC queues with preallocated rings that track producer and consumer progress separately and keep each side's hot state on its own cache line. Cursors publish completed payload transfers and cache peer progress, with no per-slot stamps or packed capacity bits. MPMC reservations and detached grants belong to producer state under the producer lock, and waiters use stable IDs instead of pointers into pending futures. Receiver registration is separate from the consumer cursor, and wake callbacks and waker destruction run outside locks. SPMC keeps a specialized single-producer ring. Exact logical capacity, reservation ordering, cancellation, disconnection, and endpoint traits are unchanged. Unbounded queues keep the mutex backend, which bounded queues also use at capacity one and beyond the ring allocation budget. Copy CachePadded from crossbeam-utils and record the incorporated crossbeam portions in LICENSE. Each transfer publishes its cursor with a SeqCst store, an xchg on x86-64, so single-producer capacity-64 MPMC workloads are slower than main on Linux; the changelog records this. The benchmarks gain reserved-send and crossbeam-channel MPMC adapters, fixed-work 1P/8C, capacity-one, spinning, and SPMC scenarios, and reservation micro-benchmarks. Co-authored-by: tison <wander4096@gmail.com>
ariesdevil
force-pushed
the
dev_ariesdevil
branch
from
November 23, 2026 06:04
d09d839 to
4791c9a
Compare
This branch has not been deployed
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.
Related: #298
Summary
Optimize bounded MPMC and SPMC queues with a ring that keeps producer and consumer progress separate and isolates them on their own cache lines. Exact logical capacity, reservation ordering, cancellation, disconnection, and endpoint traits are preserved. Unbounded queues keep their mutex-based backend. Capacity-one queues and capacities beyond the ring's allocation budget keep lazy storage, so there is no new public capacity limit. These implementations are not lock-free.
Ring-backed queues preallocate up to 64 KiB of message storage, which stays allocated until every handle is dropped; see Memory.
Known limitations (details under Regressions and limits):
Design Notes
RetryWaiter.VecDeque'sDropdoes, so closing does not allocate.spmc::boundeddocs note this exception.reserve/try_reservefrom the producer's cached reclamation cursor. Only the producer consumes capacity, so reserved capacity stays available untilPermit::send.try_reservechecks for disconnection after reading capacity, astry_senddoes.sendpublishes and notifies under the control lock. Publishing outside the lock and relocking to notify cost 10% on macOS.CachePaddedhelper. Crossbeam provenance is retained in source and inLICENSE.Benchmarks
The table gives median fixed-work elapsed times, main
ae99c15→ PRa1284ba, built with the same harness and rustc 1.99.0. Negative is faster; these are not tail-latency claims.0,2,...,30. Each revision had its own build directory and hash-verified executables.mpmc/bounded/tasks-2p2c-cap64-4wAsyncbandMpmcReservempmc/bounded/threads-2p2c-cap64AsyncbandMpmcReservempmc/bounded/threads-1p8c-cap64mpmc/bounded/threads-2p2c-cap1spmc/bounded/tasks-1p4c-cap64-4wspmc/bounded/tasks-1p4c-cap1024-4wThe
spmc/boundedrows measure the dedicated SPMC queue (AsyncbandSpmc). TheAsyncbandMpmcrows of those scenarios are under regressions.The earlier stamped-slot revision (
c766001) was faster on Linux in the 2P2C async and threaded cases, where this PR takes 30% and 155% longer. In threaded 1P8C, this PR takes 61% less time. This PR does not claim that revision's peak throughput.Regressions and limits
send_then_recv(lazy storage, not the ring): on Linux with a fixed single CPU, the median for 24 million round trips went from 755.5 ms to 1095 ms (+44.9%). Main also has a slower mode near 924 ms, and the PR is slower than that too. The cause is unresolved; async backend dispatch and future construction are a follow-up investigation.AsyncbandMpmcrows ofspmc/bounded/tasks-1p1c-cap64-4w,tasks-1p4c-cap64-4w, andtasks-1p4c-cap64-current.SeqCststore, which is anxchgon x86-64. Each side therefore does three locked instructions per operation, where main's mutex does two.Releasestore followed byfence(SeqCst)is no faster.Memory
Measured with a counting global allocator on a full capacity-4096 queue of
[u64; 2], keeping the sender while the last receiver drops:ae99c15The retained memory is the ring itself. The SPMC producer accesses slots without a lock, so freeing them at receiver close would need extra synchronization on its hot path. On macOS, both close paths show one 64 B allocation, which is std lazily boxing the pthread mutex rather than ring storage.
Validation
1cadff0(0.8.1 release preparation). Only the changelog entries moved, to Unreleased.try_reserveused to check for disconnection before reading capacity. If the last receiver dropped between the two reads, a full queue could hand out a permit for the discarded slots.receiver_close_racing_reservation_cannot_reserve_discarded_slotsraces that drop againsttry_reserveandreserve.cargo x test: 787 passed on macOS and on Linux.cargo x lintandcargo x checkpassed on macOS.cargo x miriwith the CI flags (-Zmiri-many-seeds=0..4) passed: 82 library tests (15 of them ring tests), MPMC integration 33 passed/2 ignored, and SPMC integration 33 passed/2 ignored.13b13e4), a Linux rotation reproduced the profile:send_then_recv: +37%.-Zmiri-many-seeds=0..640. Removing the cursor/flag StoreLoad ordering makes them report deadlocks.0..128seeds.powerpc-unknown-linux-gnuMPMC Miri run passed (33 passed, 2 ignored).spin-2p2c-cap64(+3.4%) and SPMCtasks-1p4c-cap64-4w(+3.2%). Both are attributed to code layout, since a build with instruction-identical hot functions reproduced the shift.