Struct concurrent_hash_map::ConcurrentHashMap [] [src]

pub struct ConcurrentHashMap<K: Eq + Hash + Sync + Clone, V: Sync + Clone> {
    // some fields omitted
}

This is a simple concurrent hash map. It uses a design that's lock free on gets, and locking on inserts/removals. In order to maintain concurrency on insert/removal operations, the map is segmented into several sub-maps, each of which has its own write lock.

This code is currently extremely pre-alpha. Most particularly, it leaks memory on table growth and drop, as well as when using keys or values that (even transitively) use custom Drop implementations. It should be possible to fix this, but a clean solution will require support for running destructors in crossbeam (see crossbeam issue #13).

For now it may be useful for long lived hashmaps with a relatively steady size, but I don't recommend using it for anything important :-).

Methods

impl<K: Eq + Hash + Sync + Clone, V: Sync + Clone> ConcurrentHashMap<K, V>
[src]

fn new() -> ConcurrentHashMap<K, V>

Creates a new HashMap with default options (segment count = 8, capacity = 16, load factor = 0.8)

fn new_with_options(capacity: u32, segments: u32, load_factor: f32) -> ConcurrentHashMap<K, V>

Creates a new HashMap. There are three tuning options: capacity, segment count, and load factor. Load factor and capacity are pretty much what you'd expect: load factor describes the level of table full-ness of the table at which we grow to avoid excess collisions. Capacity is simply initial capacity.

Segment count describes the number of segments the table is divided into. Each segment is essentially an autonomous hash table that receives a division of the hash space. Each segment has a write lock, so only one write can happen per segment at any one time. Reads can still proceed while writes are in progress. For tables with a lot of write activity, a higher segment count will be beneficial.

Both capacity and segment count must be powers of two - if they're not, each number is raised to the next power of two. Capacity must be >= segment count, and again is increased as necessary.

fn insert(&mut self, key: K, value: V) -> Option<V>

Inserts a key-value pair into the map. If the key was already present, the previous value is returned. Otherwise, None is returned.

fn get(&self, key: K) -> Option<V>

Gets a value from the map. If it's not present, None is returned.

fn remove(&mut self, key: K) -> Option<V>

Removes a key-value pair from the map. If the key was found, the value associated with it is returned. Otherwise, None is returned.

fn len(&self) -> u32

Returns the size of the map. Note that this size is a best-effort approximation, as concurrent activity may lead to an inaccurate count.

Trait Implementations

impl<K: Eq + Hash + Sync + Clone, V: Sync + Clone> Clone for ConcurrentHashMap<K, V>
[src]

Gives a new handle onto the HashMap (much like clone on an Arc). Does not copy the contents of the map.

fn clone(&self) -> ConcurrentHashMap<K, V>

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)
1.0.0

Performs copy-assignment from source. Read more