Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crossbeam channel is very slow if restricted to a single core #1031

Open
matklad opened this issue Oct 17, 2023 · 4 comments
Open

crossbeam channel is very slow if restricted to a single core #1031

matklad opened this issue Oct 17, 2023 · 4 comments

Comments

@matklad
Copy link
Contributor

matklad commented Oct 17, 2023

TL;DR: restricting a simple essentially sequential ping-pong program to run on a single core reduces performance by an order of magnitude.

Full reproduction here:

https://github.com/matklad/repros/tree/master/ping-pong

The program in question:

const N: u64 = 1_000_000;

use crossbeam_channel::{Receiver, Sender};

pub fn main() {
    let t = std::time::Instant::now();
    let (a_to_b, b_from_a) = crossbeam_channel::bounded(0);
    let (b_to_a, a_from_b) = crossbeam_channel::bounded(0);
    std::thread::scope(|scope| {
        scope.spawn(move || worker(true, a_from_b, a_to_b));
        scope.spawn(move || worker(false, b_from_a, b_to_a));
    });
    eprintln!("{:?}", t.elapsed());
}

fn worker(init: bool, receiver: Receiver<u64>, sender: Sender<u64>) {
    if init {
        sender.send(0).unwrap();
    }
    for x in receiver {
        if x == N {
            break;
        }
        sender.send(x + 1).unwrap();
    }
}
$ ./target/release/ping-pong
377.409207ms
$ taskset --cpu-list 1 ./target/release/ping-pong
5.058068466s

As a sanity check, the equivalent Go program (in the repro repo) takes approximately the same time on any number of cores.

@matklad
Copy link
Contributor Author

matklad commented Oct 17, 2023

Ahhh, I think I've rediscovered #821 basically!

@taiki-e
Copy link
Member

taiki-e commented Oct 28, 2023

Does std mpsc has the same issue?

@taiki-e
Copy link
Member

taiki-e commented Nov 5, 2023

cc @ibraheemdev

@ibraheemdev
Copy link
Contributor

I'm working on an idea that should hopefully fix a lot of these related issues.

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

No branches or pull requests

3 participants