Struct ConcurrentHashMap

Source
pub struct ConcurrentHashMap<K: Eq + Hash + Sync + Clone, V: Sync + Clone, H: BuildHasher> { /* private fields */ }
Expand description

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 :-).

Implementations§

Source§

impl<K: Eq + Hash + Sync + Clone, V: Sync + Clone> ConcurrentHashMap<K, V, RandomState>

Source

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

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

Source§

impl<K: Eq + Hash + Sync + Clone, V: Sync + Clone, H: BuildHasher> ConcurrentHashMap<K, V, H>

Source

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

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.

Source

pub 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.

Source

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

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

Source

pub 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.

Source

pub 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.

Source

pub fn entries(&self) -> Vec<(K, V)>

Trait Implementations§

Source§

impl<K: Eq + Hash + Sync + Clone, V: Sync + Clone, H: BuildHasher + Clone> Clone for ConcurrentHashMap<K, V, H>

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

Source§

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

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<K, V, H> Freeze for ConcurrentHashMap<K, V, H>
where H: Freeze,

§

impl<K, V, H> RefUnwindSafe for ConcurrentHashMap<K, V, H>
where H: RefUnwindSafe,

§

impl<K, V, H> Send for ConcurrentHashMap<K, V, H>
where H: Send,

§

impl<K, V, H> Sync for ConcurrentHashMap<K, V, H>
where H: Sync,

§

impl<K, V, H> Unpin for ConcurrentHashMap<K, V, H>
where H: Unpin,

§

impl<K, V, H> UnwindSafe for ConcurrentHashMap<K, V, H>
where H: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.