crossbeam-rs / crossbeam

Tools for concurrent programming in Rust
Apache License 2.0
7.45k stars 470 forks source link

Can we avoid to call `try_advance` in some senarios? [crossbeam-epoch] #1001

Open Yriuns opened 1 year ago

Yriuns commented 1 year ago

Recently I'm working on a epoch-based memory reclamation library and learned a lot of from crossbeam-epoch. Thank you for your great work.

But I noticed that we will call global().collect() every PINNINGS_BETWEEN_COLLECT 1st pins. In the senario which defer_destroy happens rarely, there is nothing to collect in most time. But try_advance issues a SeqCst ordering and a traverse of all threads, which is heavy. I wonder if we can add something like:

if self.queue.empty() {
    return
}

at the beginning of collect(), to reduce the overhead?

https://github.com/crossbeam-rs/crossbeam/blob/ce31c18607c44d3d07fc3618f981e858b35e3828/crossbeam-epoch/src/internal.rs#L201-L219

loyd commented 7 months ago

I observed the same situation for my case (99.99% reads), especially with many threads that's related to https://github.com/crossbeam-rs/crossbeam/issues/852

Another approach is implemented in the scc crate, where scan is started ONLY (@wvwwvwwv, is it correct?) when:

@taiki-e, what do you think about it?

wvwwvwwv commented 7 months ago

@loyd yes, correct.