Skip to content

Commit

Permalink
Auto merge of #93563 - ibraheemdev:crossbeam-channel, r=Amanieu
Browse files Browse the repository at this point in the history
Merge crossbeam-channel into `std::sync::mpsc`

This PR imports the [`crossbeam-channel`](https://github.com/crossbeam-rs/crossbeam/tree/master/crossbeam-channel#crossbeam-channel) crate into the standard library as a private module, `sync::mpmc`. `sync::mpsc` is now implemented as a thin wrapper around `sync::mpmc`. The primary purpose of this PR is to resolve #39364. The public API intentionally remains the same.

The reason #39364 has not been fixed in over 5 years is that the current channel is *incredibly* complex. It was written many years ago and has sat mostly untouched since. `crossbeam-channel` has become the most popular alternative on crates.io, amassing over 30 million downloads. While crossbeam's channel is also complex, like all fast concurrent data structures, it avoids some of the major issues with the current implementation around dynamic flavor upgrades. The new implementation decides on the datastructure to be used when the channel is created, and the channel retains that structure until it is dropped.

Replacing `sync::mpsc` with a simpler, less performant implementation has been discussed as an alternative. However, Rust touts itself as enabling *fearless concurrency*, and having the standard library feature a subpar implementation of a core concurrency primitive doesn't feel right. The argument is that slower is better than broken, but this PR shows that we can do better.

As mentioned before, the primary purpose of this PR is to fix #39364, and so the public API intentionally remains the same. *After* that problem is fixed, the fact that `sync::mpmc` now exists makes it easier to fix the primary limitation of `mpsc`, the fact that it only supports a single consumer. spmc and mpmc are two other common concurrency patterns, and this change enables a path to deprecating `mpsc` and exposing a general `sync::channel` module that supports multiple consumers. It also implements other useful methods such as `send_timeout`. That said, exposing MPMC and other new functionality is mostly out of scope for this PR, and it would be helpful if discussion stays on topic :)

For what it's worth, the new implementation has also been shown to be more performant in [some basic benchmarks](https://github.com/crossbeam-rs/crossbeam/tree/master/crossbeam-channel/benchmarks#results).

cc `@taiki-e`

r? rust-lang/libs
  • Loading branch information
bors committed Nov 13, 2022
2 parents 928d14b + a2f58ab commit afd7977
Show file tree
Hide file tree
Showing 26 changed files with 2,739 additions and 2,862 deletions.
5 changes: 0 additions & 5 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ Files: *
Copyright: The Rust Project Developers (see https://thanks.rust-lang.org)
License: MIT or Apache-2.0

Files: library/std/src/sync/mpsc/mpsc_queue.rs
library/std/src/sync/mpsc/spsc_queue.rs
Copyright: 2010-2011 Dmitry Vyukov
License: BSD-2-Clause

Files: src/librustdoc/html/static/fonts/FiraSans*
Copyright: 2014, Mozilla Foundation, 2014, Telefonica S.A.
License: OFL-1.1
Expand Down
9 changes: 0 additions & 9 deletions LICENSES/BSD-2-Clause.txt

This file was deleted.

1 change: 1 addition & 0 deletions library/std/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ pub mod mpsc;
mod barrier;
mod condvar;
mod lazy_lock;
mod mpmc;
mod mutex;
mod once;
mod once_lock;
Expand Down
Loading

0 comments on commit afd7977

Please sign in to comment.