vorner / arc-swap

Support atomic operations on Arc itself

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to implement a concurrent hashmap?

lithbitren opened this issue · comments

use arc_swap::ArcSwap;
fn arc_swap_hashmap(map: HashMap<usize, usize>, getn: usize, setn: usize, workers: usize) {
    let map = Arc::new(ArcSwap::from_pointee(map));
    let start = Instant::now();
    let threads: Vec<_> = (0..workers)
        .map(|_| {
            let map = map.clone();
            thread::spawn(move || {
                for i in 0..getn / workers {
                    let _ = map.load().get(&i).unwrap();
                }
                for i in 0..setn / workers {
                    map.load().insert(i, i); // error[E0596]: cannot borrow data in an `Arc` as mutable
                }
            })
        }).collect();
    for thread in threads {
        thread.join().unwrap();
    }
    print!("arc_swap_hashmap: {:?}, ", start.elapsed());
}

How to modify this code so that it can be compiled

Hello

You seem to be missing the point / principle what arc-swap is and what it isn't. It in no way allows you to modify the „insides“. It only allows you to replace it as a whole.

So, you could do some of these:

  • Pick an already existing concurrent hashmap implementation ‒ for example dashmap. It's not lock-free, but AFAIK might be reasonably fast. You don't need arc-swap for that.
  • Clone the map at each insertion and replace it with the new one, something with the rcu method. For that, I'd also suggest using some form of immutable data structure instead of a the „stock“ hashmap (eg. something from the im crate). This might be also be reasonably fast, depending on the use case (when it would be leaning a lot towards reading).
  • Build a hash-map-like data structure from many arc-swaps, so only small bits of it can be replaced. I'd guess such thing would be memory-expensive and unlikely to speed things up.
  • Revive this failed experiment, bring it up to date and try to make it actually perform somewhat fast: https://github.com/vorner/contrie

As there's nothing that's an issue related to arc-swap itself, I'm going to close this… this isn't really supposed to be a „help with Rust borrow checker forum“.