feat(broadcast): add an SPMC specialization - #317
Open
onenewcode wants to merge 10 commits into
Open
onenewcode wants to merge 10 commits into
onenewcode wants to merge 10 commits into
Conversation
Summary Add `broadcast::spmc` with a non-cloneable sender whose publish methods take `&mut self`. Bounded waits for the slowest active subscription; unbounded never waits. Existing `broadcast::mpmc` is unchanged.
Receivers clone T from a single-writer log without taking the waiter mutex. The last remaining reader takes the payload.
Native-thread send_blocking/recv_blocking wait on one epoch condvar instead of an async waker per task.
Unbounded send writes the slot and publishes tail without the waiter mutex when receivers exist. Fan-out at 32 receivers is now under Tokio's lossy ring.
disconnect() notifies the shared condvar, and recv_blocking rechecks disconnection after taking that lock so a parked native-thread receive cannot hang.
Drop the extra wait helper. Clear send_in_progress once, park without cloning the channel Arc, and recover condvar poison the same way as Mutex.
onenewcode
force-pushed
the
refactor/broadcast-spmc-internals
branch
from
September 15, 2026 05:44
b072b5e to
43a8da8
Compare
Keep slot-log types family-private. Compile-fail examples show the sender is not Clone and publish takes &mut self. Cover unbounded fan-out, late subscribe, and slow-subscription growth.
onenewcode
force-pushed
the
refactor/broadcast-spmc-internals
branch
from
September 15, 2026 05:49
43a8da8 to
a2081b9
Compare
Hold the waiter mutex across publication so a slot's remaining-reader count matches live subscriptions and a parking receiver cannot miss a send. Release unbounded chunk storage as head advances, and wake the producer if a panicking clone is the last reader. Drop native-thread send_blocking/recv_blocking; FutureExt::block_on covers that path.
Reload tail after observing a dropped sender so try_recv cannot report Disconnected while a published value is still unread. Discard unbounded sends without advancing the version log so empty chunks are not allocated for messages that were never retained.
onenewcode
marked this pull request as ready for review
September 16, 2026 03:59
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
Closes #214.
broadcast::spmc, a lossless single-producer broadcast family with a non-cloneable sender whose publish methods take&mut self.broadcast::mpmc: bounded waits at capacity for the slowest active subscription, unbounded never waits, and a new subscription starts at the committed tail.broadcast::mpmcpublic surface method for method, except the sender is notClone. A receive drains accepted values before reporting disconnection. Unbounded value storage tracks the live window: discarded sends with no subscribers do not grow the log, and retained storage is released as the backlog drains.asyncband/src/broadcast/spmc.Design Notes
Public retention matches
broadcast::mpmc: lossless fan-out, bounded wait-at-capacity, unbounded growth, join at the committed tail, and discard with no subscribers.try_recvdrains accepted values beforeDisconnected. Unbounded discarded sends do not grow the log.The sender is not
Clone; publish takes&mut self. A blocked boundedsendcannotsubscribe; useresubscribe. The slot log stays family-private. Publication shares the waiter mutex with subscribe and parking; receivers drain published slots without that lock.Benchmark comparison
One producer.
broadcast::spmcsenders are not cloned.mpmc 1Pisbroadcast::mpmcwith one sender.Bounded is the lossless wait-at-capacity path (
async-broadcastoverflow off). Tokio is omitted: it overwrites at capacity. Unbounded is the non-blocking batch path; Tokio and async-broadcast get capacity 4096 so the batch stays offLagged. Native-threadconcurrentwaits useFutureExt::block_on. Figures aredivanmedians from one run on Apple M4 (cargo bench -p benchmarks --bench ecosystem -- broadcast::spmc).Bounded (lossless wait-at-capacity)
try_round_tripready_round_tripNative threads (
concurrent). Lower time is better.Tokio 4-worker (
scheduled):Unbounded (non-blocking batch)
try_round_tripready_round_tripFan-out (publish 4096, then every subscription drains):