Skip to content

refactor(mpsc): replace the legacy queue backend - #247

Open
mxsm wants to merge 2 commits into
apache:mainfrom
mxsm:mxsm-209
Open

refactor(mpsc): replace the legacy queue backend#247
mxsm wants to merge 2 commits into
apache:mainfrom
mxsm:mxsm-209

Conversation

@mxsm

@mxsm mxsm commented Aug 30, 2026

Copy link
Copy Markdown
Member

Summary

  • Replace the bounded and unbounded std::sync::mpsc backends with MPSC-owned queue implementations while preserving the public sender, receiver, and error contracts.
  • Use receiver-local batching for the unbounded queue and a preallocated, generation-stamped slot ring for the bounded queue.
  • Keep backpressure, cancellation safety, FIFO ordering, disconnection, and buffered-message draining behavior covered by tests.
  • Remove the receiver types' manual Sync implementations and document the bounded slot's localized unsafe invariant.
  • Update the changelog for the backend replacement.

Closes #209.

Design Notes

The unbounded queue keeps sender-visible storage and receiver liveness behind one mutex, while the single receiver moves messages into a local batch to reduce shared-lock contention without introducing unsafe code.

The bounded queue allocates its capacity up front and uses per-slot generation stamps. Producers reserve slots through the tail cursor, while the single consumer advances the head cursor; cache padding prevents producer and consumer cursor updates from sharing a cache line.

Unsafe code is confined to initialized slot access and Slot<T>: Sync. Each unsafe operation is paired with a documented reservation, publication, acquisition, and reuse invariant, and the queue is exercised under Miri with multiple producers.

Validation

  • cargo x test
  • cargo x check
  • cargo x miri
  • cargo x lint
  • cargo x bench

Benchmark Comparison

The following bounded MPSC medians were measured in adjacent runs on the same machine with 16,384 messages per sample.

Producers Legacy backend New backend Change
1 2.235 ms 1.567 ms -29.9%
2 1.601 ms 1.541 ms -3.7%
4 1.888 ms 2.095 ms +11.0%
8 2.235 ms 2.292 ms +2.6%

The existing ecosystem benchmark continues to cover bounded and unbounded channels with 1, 2, 4, and 8 producers. The full benchmark suite completes without an order-of-magnitude regression or a material bounded-contention regression.

@tisonkun
tisonkun requested a review from orthur2 August 30, 2026 17:26
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.

refactor(mpsc): replace the legacy queue backend

2 participants