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
3 times, most recently
from
November 27, 2026 05:57
4791c9a to
db86cd5
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. Co-authored-by: tison <[email protected]>
ariesdevil
force-pushed
the
dev_ariesdevil
branch
from
November 30, 2026 05:54
db86cd5 to
5828c62
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):
try_send/try_recvround trip by 14–26%, and the 1P4C task workload by 2.3% (23–37% in the pre-refactor(benchmarks): unify suite around hot-path workloads and clean timing boundaries #472 single-producer task scenarios). On Apple Silicon these are within 2% of main.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 from #472's suite (
cargo x bench -- mc/ --repeat 10 --raw), mainb6b8adf→ PR5828c62. Both revisions run the same benchmark code, built with rustc 1.99.0. Negative is faster; these are not tail-latency claims.AsyncChannelandFlumepeers were more than 10% slower than in their fastest run were dropped as externally perturbed, leaving 8–16 runs per cell. An 8% cutoff moves no Asyncband cell by more than 0.2 points.0,2,...,30. Each revision had its own build directory and hash-verified executables.mpmc/bounded/tasks-4p4c-cap64mpmc/bounded/tasks-4p4c-cap1mpmc/bounded/backpressure-handoffmpmc/bounded/try-roundtripspmc/bounded/tasks-1p4c-cap64AsyncbandMpmcspmc/bounded/try-roundtripAsyncbandMpmcThe
spmc/boundedrows measure the dedicated SPMC queue (AsyncbandSpmc). The ↳ rows are single-producermpmc::boundedin the same scenarios. Capacity-one MPMC queues keep lazy storage, sotasks-4p4c-cap1andbackpressure-handoffdo not use the ring. The unbounded scenarios, which keep main's backend, moved −0.2% to +0.3% on Linux and −0.6% to +3.1% on macOS. The peers moved by up to 3.6% on Linux and 4.4% on macOS.#472's suite has no thread-blocking, spinning, or
reservescenarios. The pre-#472 harness measured those ata1284ba: threaded MPMC 2P2C took 34% (macOS) and 36% (Linux) less time, and threaded 1P8C 27% and 62% less. With that harness, 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
try_send/try_recvround trip takes 34.9 ns inmpmc/bounded/try-roundtripand 35.1 ns in theAsyncbandMpmcrow ofspmc/bounded/try-roundtrip, where two of six runs took 38–39 ns. Main takes 30.7 ns (+13.9%) and 27.8 ns (+26.2%).AsyncbandMpmcinspmc/bounded/tasks-1p4c-cap64) is +2.3%. Main's per-run medians split between 1213 ms (two runs) and 1302–1334 ms (four), and the PR's are 1320–1346 ms: 9% above main's fast mode and 1% above its slow mode. The pre-refactor(benchmarks): unify suite around hot-path workloads and clean timing boundaries #472 harness measured +23–37% in single-producer 1P1C, 1P4C, andcurrent_threadtask scenarios. refactor(benchmarks): unify suite around hot-path workloads and clean timing boundaries #472's suite keeps only the 1P4C multi-thread topology.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.tasks-4p4c-cap1andbackpressure-handoffare within 1% (+1.0% and −0.8%).tasks-4p4c-cap1per-run medians overlap. Inbackpressure-handoff, only one main run, whose peers were nearly 10% slow, overlaps the PR's.send_then_recvscenario. With the pre-refactor(benchmarks): unify suite around hot-path workloads and clean timing boundaries #472 harness on Linux with a fixed single CPU, its 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.reserveworkloads. All Linux results come from one VM.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
b6b8adf. The module declarations, re-export grouping, and internal helper visibility follow refactor: standardize module layout, visibility, and lint expectations #486'sAGENTS.mdconventions. refactor(benchmarks): unify suite around hot-path workloads and clean timing boundaries #472 replaced the benchmark suite, so this PR does not changebenchmarks/or add a benchmark dependency. The changelog entries follow theAGENTS.mdguidelines. The ring code and tests are otherwise unchanged.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: 820 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 on all integration targets (test(miri): run all integration test targets in cargo x miri #480), including 96 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.