Skip to content

refactor(channels): simplify bounded mpsc and unbounded broadcast internals - #473

Open
biaoma-ty wants to merge 2 commits into
apache:mainfrom
biaoma-ty:refactor/deep-channel-primitives
Open

biaoma-ty wants to merge 2 commits into
apache:mainfrom
biaoma-ty:refactor/deep-channel-primitives

Conversation

@biaoma-ty

@biaoma-ty biaoma-ty commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Simplify broadcast::mpmc::unbounded onto Arc-owned segments and an epoch-free sharded rwlock (broadcast::mpmc::unbounded): Remove segment.rs along with manual alloc_zeroed/dealloc heap segments, custom segment refcounting, spare pooling, prev back-pointers, unlinked_next sentinels, OnceLock<Box<ExtraShards>>, and 3-epoch garbage collection. Replace them with uniform Arc<Segment<T>> blocks (with iterative Drop and 8 inline genesis slots in Shared<T>), a 4-shard ShardedRwLock<State<T>> that immediately releases retired tail segments on write-guard handoff, and a single-mutex BroadcastWake coordinator in broadcast::mpmc::unbounded::waiters that preserves the sync-before-async wakeup order from fix(broadcast): notify sync in-flight slot waiters before async wakers #470.
  • Simplify mpsc::bounded receiver waits and share GrantWaiter (mpsc::bounded, mpmc, internal::grant_waiter): Unify non-ZST receiver empty and in-flight waiting onto slot[head].state = SLOT_RX_WAITING, co-locate hot producer counters onto one cache line, relax proven orderings on acquired and slot_state, unify try_acquire_slow<const SEQCST> and release_locked/release_slow, and extract the shared GrantWaiter wait-list helper across mpsc and mpmc.

Overall diff removes 766 net lines against main (+1,482 / -2,248), cuts broadcast::mpmc::unbounded unsafe blocks from 58 to 18 (unsafe fn from 11 to 1), and improves multi-task mpsc::bounded (tasks-1p1c-cap1024-4w and tasks-4p1c-cap64-4w) and concurrent broadcast::mpmc::unbounded (publish-4p-drain-1r and drain-4r) throughput.

Follows up on #465, #469, and #470. Part of #396.

Design Notes

  • ShardedRwLock<State<T>> without epochs: Publishers take a per-sender shard read guard (&State<T>) with zero reader cache-line contention across 4 producers and zero AtomicPtr / epoch retirement machinery. Subscription changes and tail segment advances take exclusive write access so superseded Arc<Segment<T>> handles drop immediately outside the lock; advance_receiver_seg (reachable from Future::poll) only uses non-blocking try_write() so poll_recv never spins or yields.
  • Unified non-ZST slot waiting in mpsc::bounded: Non-ZST receivers arm SLOT_RX_WAITING directly on slot[head] instead of setting and clearing RX_WAITING_BIT on the producer-hot tail atomic, avoiding receiver-induced tail cache-line bounces while keeping RX_WAITING_BIT solely for zero-sized channels without slot storage.

Validation

  • cargo x check (across --no-default-features, every feature individually, and --all-features)
  • cargo x test (full workspace)
  • cargo x lint (nightly clippy, rustfmt, taplo, typos, hawkeye, docsrs rustdoc -D warnings)
  • cargo +1.86.0 check --package asyncband --all-features --tests
  • MIRIFLAGS="-Zmiri-strict-provenance -Zmiri-symbolic-alignment-check -Zmiri-many-seeds=0..4" cargo +nightly miri test --package asyncband --lib --features broadcast,mpsc,mpmc (broadcast::mpmc::unbounded and mpsc::bounded)
  • MIRIFLAGS="-Zmiri-strict-provenance -Zmiri-symbolic-alignment-check" cargo +nightly miri test --package tests-integration --test broadcast_test --test mpsc_test --test mpmc_test --test traits_test

Comment thread asyncband/src/internal/counter_u64.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a random internal utility. Why do we need to generalize it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to dedup the local target_has_atomic = "64" wrappers (watch, mpsc, broadcast), but you're right — mixing broadcast's claim_next with the rest isn't worth generalizing. I'll drop it, keep them local, and move broadcast_wake back under broadcast/mpmc/unbounded.

@biaoma-ty

Copy link
Copy Markdown
Contributor Author

Benchmark Results (origin/main 294eb67 vs PR #473 c1ff9dd)

Environment: Darwin arm64, Apple M4 Pro, rustc 1.95.0 (59807616e 2026-04-14). Both revisions were pre-built and run back-to-back on the same benchmark suite (5 runs per case, median ms reported).

broadcast::mpmc::unbounded (Ecosystem)

Benchmark origin/main PR #473 Delta Tokio AsyncBroadcast
broadcast/no-backpressure/try-roundtrip 152 ms 153 ms +0.7% 106 ms 234 ms
broadcast/no-backpressure/try-roundtrip-shared 151 ms 153 ms +1.3% 200 ms 362 ms
broadcast/no-backpressure/ready-roundtrip 138 ms 139 ms +0.7% 129 ms 224 ms
broadcast/no-backpressure/publish-4p-drain-1r 343 ms 329 ms -4.1% 157 ms 406 ms
broadcast/no-backpressure/sequential-drain-4r 296 ms 288 ms -2.7% 238 ms 444 ms
broadcast/no-backpressure/drain-4r 279 ms 301 ms +7.9% 242 ms 1,127 ms
broadcast/no-backpressure/drain-4r-shared 280 ms 283 ms +1.1% 363 ms 1,522 ms

broadcast::mpmc::unbounded (Primitives)

Benchmark origin/main PR #473 Delta
send_and_try_recv_shared 101 ms 101 ms 0.0%
send_and_try_recv_string_shared 140 ms 140 ms 0.0%
send_and_try_recv_string_cloned_sole 143 ms 146 ms +2.1%
send_and_try_recv_string_cloned_fanout 192 ms 195 ms +1.6%
deliver_to_waiting_receiver 187 ms 180 ms -3.7%
register_and_deliver_fanout/1 578 ms 616 ms +6.6%
register_and_deliver_fanout/4 258 ms 261 ms +1.2%
register_and_deliver_fanout/16 193 ms 181 ms -6.2%
send_without_receivers 43 ms 45 ms +4.7%
try_recv_empty 63 ms 63 ms 0.0%
drain_with_receivers/peak 1 live 1 160 ms 161 ms +0.6%
drain_with_receivers/peak 8 live 8 36 ms 34 ms -5.6%
drain_with_receivers/peak 8 live 1 162 ms 160 ms -1.2%
drain_with_receivers/peak 32 live 32 36 ms 34 ms -5.6%
drain_with_receivers/peak 32 live 4 63 ms 49 ms -22.2%
drain_with_receivers/peak 32 live 1 162 ms 160 ms -1.2%
drain_with_receivers/peak 256 live 32 35 ms 34 ms -2.9%
drain_with_receivers/peak 256 live 1 161 ms 160 ms -0.6%
cancel_pending 263 ms 252 ms -4.2%

mpsc::bounded & mpsc::reservation (Ecosystem, --baseline-only)

Benchmark origin/main PR #473 Delta
mpsc/bounded/try-roundtrip 148 ms 148 ms 0.0%
mpsc/bounded/ready-roundtrip 142 ms 146 ms +2.8%
mpsc/bounded/ready-roundtrip-inline1k 125 ms 124 ms -0.8%
mpsc/bounded/ready-roundtrip-boxed1k 45 ms 45 ms 0.0%
mpsc/bounded/threads-4p1c-cap64 200 ms 227 ms +13.5%
mpsc/bounded/tasks-1p1c-cap64-4w 211 ms 217 ms +2.8%
mpsc/bounded/tasks-4p1c-cap64-4w 262 ms 203 ms -22.5%
mpsc/bounded/tasks-1p1c-cap1024-4w 138 ms 61 ms -55.8%
mpsc/bounded/tasks-4p1c-cap1024-4w 302 ms 409 ms +35.4%
mpsc/bounded/tasks-1p1c-cap64-current 184 ms 188 ms +2.2%
mpsc/bounded/tasks-1p1c-cap1024-current 165 ms 166 ms +0.6%
mpsc/bounded/tasks-4p1c-cap64-current 189 ms 189 ms 0.0%
mpsc/bounded/tasks-4p1c-cap64-4w-inline1k 83 ms 91 ms +9.6%
mpsc/bounded/tasks-4p1c-cap64-4w-boxed1k 57 ms 70 ms +22.8%
mpsc/bounded/tasks-4p1c-cap1-4w 288 ms 292 ms +1.4%
mpsc/bounded/external-receiver-1p1c-cap64-4w 237 ms 240 ms +1.3%
mpsc/reservation/reserve-publish-receive 104 ms 104 ms 0.0%
mpsc/reservation/tasks-4p1c-cap1-4w 298 ms 327 ms +9.7%

Notes:

  • mpsc/bounded/tasks-1p1c-cap1024-4w drops from 138 ms to 61 ms (-55.8%) and tasks-4p1c-cap64-4w drops from 262 ms to 203 ms (-22.5%) because non-ZST receivers arm SLOT_RX_WAITING on the head slot rather than stealing the producer-hot tail cache line via RX_WAITING_BIT.
  • mpsc/bounded/tasks-4p1c-cap1024-4w shows a bistable 5-task-on-4-worker scheduling regime (min observed at ~291 ms vs origin/main ~268 ms, while median shifts from 302 ms to 409 ms as the 4 producers synchronize on acquired CAS retries; still well ahead of Tokio ~620 ms).
  • broadcast::mpmc::unbounded remains at parity or slightly faster across steady-state, fan-out, and cancellation benchmarks (publish-4p-drain-1r -4.1%, deliver_to_waiting_receiver -3.7%, cancel_pending -4.2%, register_and_deliver_fanout/16 -6.2%) while eliminating 58 -> 18 unsafe blocks (unsafe fn 11 -> 1) and all epoch/pool raw-pointer management.

@biaoma-ty
biaoma-ty force-pushed the refactor/deep-channel-primitives branch 4 times, most recently from 9bc0da1 to 7b0f5bb Compare November 28, 2026 10:23
Unify non-ZST receiver slot waiting on the head slot state, co-locate hot producer counters onto one cache line, relax proven orderings, and extract the shared GrantWaiter helper used across mpsc and mpmc.

Part of apache#396

Signed-off-by: Biao Ma <[email protected]>
…nd sharded rwlock

Replace manual raw-pointer segment allocation, reference counting, spare pooling, and 3-epoch garbage collection in broadcast::mpmc::unbounded with uniform Arc-owned segments (plus 8 inline genesis slots), an epoch-free 4-shard reader-writer lock, and a single-lock BroadcastWake coordinator.

Part of apache#396

Signed-off-by: Biao Ma <[email protected]>
@biaoma-ty
biaoma-ty force-pushed the refactor/deep-channel-primitives branch from 7b0f5bb to 5ec7b5a Compare November 28, 2026 10:28

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants