Skip to content

Reduce unbounded MPMC broadcast publish and receive overhead #402

Open

Description

@tisonkun

Asyncband's unbounded MPMC broadcast channel retains each value for every eligible receiver. In these comparisons the bounded peers had room for the whole batch and every receiver saw every value. The single-receiver and four-receiver rows measure different costs.

Apple M4 Max, macOS arm64, rustc 1.101.0-nightly. Each cell gives rank, elapsed time relative to Asyncband and the whole-batch median over 10 interleaved runs. Lower is faster. Ecosystem measurements

Scenario Fixed rounds Asyncband Tokio async-broadcast
Publish and try-receive once 9,100,000 #3, 1.00× (300 ms) #1, 0.35× (105 ms) #2, 0.77× (231 ms)
Publish and await a ready receive 8,000,000 #3, 1.00× (273 ms) #1, 0.42× (113 ms) #2, 0.79× (215 ms)
Four publishers, one receiver 810 #3, 1.00× (302 ms) #1, 0.37× (111 ms) #2, 0.78× (236 ms)
Four receivers drain the batch 500 #3, 1.00× (326 ms) #1, 0.22× (72 ms) #2, 0.84× (275 ms)

With usize, allocation counting found one 24-byte allocation per Asyncband publication and zero for Tokio's inline value. In a matched single-receiver run, Asyncband took 292 ms, Tokio with usize took 101 ms, and Tokio publishing a newly allocated Arc<usize> took 245 ms. That control isolates much of the one-receiver gap. The four-reader path also serializes cursor advancement in Backlog::receive under one lock. Its contribution to the larger gap is still unmeasured.

First contribution

Run the command below, then repeat the Tokio comparison with an Arc<usize> payload. Use one and four receiving threads to isolate cursor contention. Keep lossless retention for slow subscribers, delivery to independent subscriptions, and the ability for received values to outlive the backlog or contain a non-Clone payload.

cargo x bench --bench ecosystem -- broadcast/no-backpressure/ --repeat 10 --raw

The existing SPMC specialization work tracks a different sender contract. SPMC issue #214 · SPMC PR #317

Performance tracking issue #396

Activity

  1. rokadepiyush49-rgb commented on Nov 6, 2026

    @rokadepiyush49-rgb

    Could you assign this to me? I'd run broadcast/no-backpressure/ on an Apple M4 (10 cores, macOS 27.0.1), repeat Tokio with Arc<usize>, and use one and four receiving threads to isolate cursor contention.

    Since recv returns Arc<T>, the per-message allocation comes with the contract. Your control puts about 144 ms of the 191 ms gap there, so I'd focus on the remaining 47 ms and the four-receiver row. If that's mostly contract too, I'd document it, as #396 allows.

    I haven't run these yet. I used Claude to help me read the code.

  2. tisonkun commented on Nov 6, 2026

    @tisonkun
    MemberAuthor

    Thanks for your interest! We don't assign issues to individual contributors. Anyone is welcome to submit a PR directly for review.

  3. nanami-0713 commented on Nov 10, 2026

    @nanami-0713
    Contributor

    Reproduced all four rows on Apple M5 (10 cores), macOS 27.0.1, rustc 1.99.1 stable, in a disposable checkout of b1354ae (current main), cargo x bench --bench ecosystem -- broadcast/no-backpressure/ --repeat 10, medians with min..max.

    Scenario Rounds Asyncband Tokio async-broadcast
    Publish and try-receive 9,100,000 176 ms (174..200) 95 ms (95..97) 191 ms (190..201)
    Publish and await receive 8,000,000 163 ms (162..180) 105 ms (104..111) 182 ms (181..192)
    Four publishers, 1 receiver 810 232 ms (226..242) 99 ms (98..100) 222 ms (220..225)
    Four receivers drain 500 246 ms (239..254) 80 ms (73..85) 228 ms (224..251)

    Against Tokio, the orderings match the issue on every row. The direct-peer picture differs from the issue's host: there async-broadcast led Asyncband on all four rows, while here the two split — async-broadcast ahead on the two multi-party rows, behind on the two single-receiver roundtrips.

    I then repeated the comparison with the issue's Arc<usize> control — Tokio publishing a freshly allocated Arc<usize> per message (pointer-sized payload, one allocation per publication) — in the same checkout, interleaved with the other implementations. Adapter registered as a third implementation beside the stock ones; the two-file change is excerpted below.

    Control row Rounds Asyncband Tokio (usize) Tokio (Arc<usize>)
    try-receive roundtrip 9,100,000 173 ms (173..216) 95 ms (94..96) 162 ms (161..163)
    await-receive roundtrip 8,000,000 162 ms (161..163) 112 ms (111..112) 168 ms (168..169)
    Four receivers drain 500 241 ms (239..245) 79 ms (72..93) 91 ms (89..93)

    With Tokio allocating once per publish, the try-receive gap shrinks from 1.82x (173/95) to 1.07x (173/162) and the await-receive gap reverses to 0.96x (162/168) — the same direction as the issue's own single-receiver control (292 vs 101 vs 245 ms on M4 Max, where the allocating Tokio run also lands close to Asyncband). The four-receiver drain behaves differently: the control narrows it only from 3.05x (241/79) to 2.65x (241/91). The single-receiver gap shrinks with the allocation control and the drain gap shrinks much less; that difference is consistent with the issue's note that cursor advancement in Backlog::receive serializes under one lock.

    Arc control adapter (excerpt; applied to benchmarks/benches/ecosystem/broadcast/mpmc/)
    // adapters.rs — a third implementation of the existing BroadcastMpmc trait:
    // Tokio with T = Arc<usize>, one fresh allocation per send.
    
    pub struct TokioArc;
    
    impl BroadcastMpmc for TokioArc {
        type Receiver = tokio::sync::broadcast::Receiver<std::sync::Arc<usize>>;
        type Sender = tokio::sync::broadcast::Sender<std::sync::Arc<usize>>;
    
        fn channel(capacity: usize, receiver_count: usize) -> (Self::Sender, Vec<Self::Receiver>) {
            let (sender, receiver) = tokio::sync::broadcast::channel(capacity);
            let mut receivers = Vec::with_capacity(receiver_count);
            receivers.push(receiver);
            for _ in 1..receiver_count {
                receivers.push(sender.subscribe());
            }
            (sender, receivers)
        }
    
        fn send(sender: &Self::Sender, value: usize) {
            sender.send(std::sync::Arc::new(value)).unwrap();
        }
    
        fn try_recv(receiver: &mut Self::Receiver) -> Option<usize> {
            match receiver.try_recv() {
                Ok(value) => Some(*value),
                Err(tokio::sync::broadcast::error::TryRecvError::Empty) => None,
                Err(error) => panic!("unexpected Tokio receive error: {error}"),
            }
        }
    
        fn recv_ready(receiver: &mut Self::Receiver, context: &mut Context<'_>) -> usize {
            *poll_ready(receiver.recv(), context).unwrap()
        }
    }
    // no_backpressure.rs — the control rows reuse the scenario bodies unchanged:
    runner.compare(
        "broadcast/no-backpressure/arc-control/try-roundtrip",
        9_100_000,
        implementations!([Asyncband, Tokio, TokioArc], |C, b| {
            try_send_receive::<C>(b)
        }),
    );
    // likewise arc-control/ready-roundtrip (8_000_000) and arc-control/drain-4r
    // (500, publish_then_parallel_drain::<C>(b, 4))

    Performance tracking issue #396

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions