Skip to main content

ConcurrentDirectMap

Struct ConcurrentDirectMap 

Source
pub struct ConcurrentDirectMap<K: DirectKey, V, const PAGE: usize = DEFAULT_PAGE, D: SharedDirectory = Flat> { /* private fields */ }
Available on crate feature concurrent only.
Expand description

A shared direct-address map with one reader-writer lock per page.

Values live at Key::index; each slot stores the key’s DirectKey::Tag, as in PagedDirectMap, and keys are rebuilt from their slot index. Neighbouring keys on the same page contend; operations on different pages lock independently. Pages remain allocated until shrink_to_fit, reset or destruction. The default Flat directory has O(1) page lookups and memory following the highest page. Its finite range makes out-of-range insertions panic; lookups and removals return absent. Radix accepts any usize index, uses directory memory proportional to installed pages, and looks up pages in O(depth). PAGE must be nonzero.

Holding a guard or entry and calling any method that locks the same page from the same thread deadlocks (or panics on std). This also applies to make, with, retain, formatting and traversal callbacks touching the same map. Release guards before making another call on their page. Holding locks on two pages at once can also deadlock across threads: if one thread holds page A and waits for page B while another holds B and waits for A (for example, two factories each looking up the other’s key). Nested calls must lock pages in one global order (such as ascending Key::index), or callbacks must not touch the map at all.

Traversals visit ascending slot indices, holding one page lock at a time. They are consistent per page only and weakly consistent across pages. Length is exact when no write is in flight (see len). The no_std spin lock permits writer starvation; the std lock ignores poisoning because mutations keep slots and counts valid even when user code panics.

use paged_collections::{concurrent::ConcurrentDirectMap, page::Radix};
let map = ConcurrentDirectMap::<usize, _, 4, Radix>::new();
assert_eq!(map.insert(usize::MAX - 1, "wide"), None);
assert_eq!(map.get_cloned(usize::MAX - 1), Some("wide"));

Implementations§

Source§

impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> ConcurrentDirectMap<K, V, PAGE, D>

Source

pub const PAGE_SIZE: usize = PAGE

Number of slots protected by each page lock.

Source

pub fn snapshot(&self) -> PagedDirectMap<K, V, PAGE, D>
where V: Clone,

Clones a weakly consistent snapshot into a valid plain map, scanning the directory and allocated slots. Includes entries that remain unchanged throughout the call if their writes happen-before it.

Value cloning runs under a page read lock. The callback must avoid all access to that page, including reads, which can deadlock behind a queued writer.

Source

pub fn new() -> Self

Creates an empty map without allocating.

§Panics

Panics if PAGE is zero or its slot storage exceeds isize::MAX bytes.

Source

pub fn len(&self) -> usize

Number of entries; exact when no write is in flight.

The count is split over per-thread counters (per page group without std) so that writers do not contend on one cache line. While writes are in flight, the sum may combine counters read at different moments and so differ from every count the map actually held (for example, 0 for a map that was never empty), though it never exceeds the number of entries inserted.

Source

pub fn is_empty(&self) -> bool

Whether len() is zero; like len, exact only with no write in flight.

Source

pub fn allocated_pages(&self) -> usize

Counts installed pages by scanning the directory without page locks. Weakly consistent while pages are being installed.

Source

pub fn contains_key(&self, key: K) -> bool

Whether the requested key itself is present.

Source

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

Acquires a read guard if this key itself is present. An out-of-range index returns None.

Source

pub fn get_mut(&self, key: K) -> Option<DirectMapRefMut<'_, K, V>>

Acquires a write guard if this key itself is present. An out-of-range index returns None.

Source

pub fn with<R>(&self, key: K, f: impl FnOnce(&V) -> R) -> Option<R>

Calls f under the page’s read lock if this key is present.

Source

pub fn get_cloned(&self, key: K) -> Option<V>
where V: Clone,

Clones a present value under its page’s read lock.

Source

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

Inserts a value, returning the old value from this slot, if any. Keeps an equal stored key; evicts a different generation. Panics for indices outside the Flat spine range or allocation overflow.

Source

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

Replaces this slot, always storing key, and returns its previous pair. Evicts another generation. With Flat, panics for an out-of-range insertion.

Source

pub fn try_insert(&self, key: K, value: V) -> Result<(), V>

Inserts only into an empty slot; returns value if any generation holds it. With Flat, panics for an out-of-range insertion.

Source

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

Removes this key itself, returning its value after releasing the lock. An out-of-range index returns None.

Source

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

Removes this key itself, returning its stored pair after releasing the lock. An out-of-range index returns None.

Source

pub fn remove_occupant(&self, index: usize) -> Option<(K, V)>

Removes whichever generation occupies index, releasing the lock first. An out-of-range index returns None.

Source

pub fn get_or_insert_with( &self, key: K, make: impl FnOnce() -> V, ) -> DirectMapRefMut<'_, K, V>

Returns this key’s value, constructing it under the page write lock if absent. Another generation is evicted. A panicking factory leaves entries unchanged. At most one competing factory runs before the key is removed or evicted. With Flat, panics for an out-of-range insertion.

Source

pub fn entry(&self, key: K) -> Entry<'_, K, V>

Locks the key’s page for an entry operation, installing an empty page if needed. Another generation makes the entry vacant; inserting through it evicts that pair. With Flat, panics for an out-of-range index.

Source

pub fn retain(&self, keep: impl FnMut(&K, &mut V) -> bool)

Keeps entries for which keep returns true, in ascending slot order. Holds one page write lock at a time; atomic only within each page. Counts remain correct if the callback or a removed value’s destructor panics.

Source

pub fn for_each(&self, f: impl FnMut(&K, &V))

Calls f in ascending slot order, holding one page read lock at a time. Consistent per page only, weakly consistent across pages.

Source

pub fn range_for_each(&self, keys: impl RangeBounds<K>, f: impl FnMut(&K, &V))

Calls f for a range bounded by Key::index, in ascending slot order. Empty or inverted ranges visit nothing. Holds one page read lock at a time; consistent per page only, weakly consistent across pages.

Source

pub fn iter_mut(&mut self) -> DirectMapIterMut<'_, K, V, PAGE> ⓘ

Iterates exclusively in ascending slot order without acquiring page locks.

Source

pub fn clear(&mut self)

Removes entries without locking, retaining pages for reuse. If a destructor panics, unvisited entries remain and counts stay correct.

Source

pub fn drain(&mut self) -> IntoIter<(K, V)> ⓘ

Removes all entries and returns owned pairs in ascending key-index order. Keeps allocated pages for reuse.

Source

pub fn shrink_to_fit(&mut self)

Frees pages that contain no entries. Flat directory chunks stay allocated. Shared insertions can allocate those pages again.

Source

pub fn reset(&mut self)

Detaches and frees all pages. The map is empty even if a destructor panics.

Source§

impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> ConcurrentDirectMap<K, V, PAGE, D>

Source

pub fn into_inner(self) -> PagedDirectMap<K, V, PAGE, D>

Moves entries into a plain map in slot order, scanning allocated slots. Each occupied entry is moved once; no values are cloned.

Trait Implementations§

Source§

impl<K: DirectKey, V: Clone, const PAGE: usize, D: SharedDirectory> Clone for ConcurrentDirectMap<K, V, PAGE, D>

Clones via ConcurrentDirectMap::snapshot. Value cloning runs under a page read lock and must avoid all access to that page.

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K: DirectKey + Debug, V: Debug, const PAGE: usize, D: SharedDirectory> Debug for ConcurrentDirectMap<K, V, PAGE, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> Default for ConcurrentDirectMap<K, V, PAGE, D>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, K: DirectKey + Deserialize<'de>, V: Deserialize<'de>, const PAGE: usize, D: SharedDirectory> Deserialize<'de> for ConcurrentDirectMap<K, V, PAGE, D>

Reads the plain PagedDirectMap format, rejecting indices outside the concurrent directory’s range before allocating a page.

Source§

fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> Extend<(K, V)> for ConcurrentDirectMap<K, V, PAGE, D>

Source§

fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> From<PagedDirectMap<K, V, PAGE, D>> for ConcurrentDirectMap<K, V, PAGE, D>

Source§

fn from(map: PagedDirectMap<K, V, PAGE, D>) -> Self

Converts to this type from the input type.
Source§

impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> FromIterator<(K, V)> for ConcurrentDirectMap<K, V, PAGE, D>

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, K: DirectKey + 'a, V: 'a, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a mut ConcurrentDirectMap<K, V, PAGE, D>

Source§

type Item = (K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = DirectMapIterMut<'a, K, V, PAGE>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, const PAGE: usize, D> Send for ConcurrentDirectMap<K, V, PAGE, D>
where K: DirectKey + Send, K::Tag: Send, V: Send, D: SharedDirectory,

Source§

impl<K: DirectKey + Serialize, V: Serialize, const PAGE: usize, D: SharedDirectory> Serialize for ConcurrentDirectMap<K, V, PAGE, D>

Serializes a weakly consistent snapshot as a valid plain map in slot order, with the same consistency as ConcurrentDirectMap::snapshot. Scans the directory and allocated slots. JSON, CBOR and serializers that buffer unknown-length MessagePack maps can accept the missing length. For serializers that require the length upfront, such as bincode, serialize snapshot() instead.

User Serialize runs under a page read lock. The callback must avoid all access to that page, including reads, which can deadlock behind a queued writer.

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<K, V, const PAGE: usize, D> Sync for ConcurrentDirectMap<K, V, PAGE, D>
where K: DirectKey + Send + Sync, K::Tag: Send + Sync, V: Send + Sync, D: SharedDirectory,

Auto Trait Implementations§

§

impl<K, V, const PAGE: usize = DEFAULT_PAGE, D = Flat> !Freeze for ConcurrentDirectMap<K, V, PAGE, D>

§

impl<K, V, const PAGE: usize, D> Forget for ConcurrentDirectMap<K, V, PAGE, D>

§

impl<K, V, const PAGE: usize, D> Move for ConcurrentDirectMap<K, V, PAGE, D>

§

impl<K, V, const PAGE: usize, D> RefUnwindSafe for ConcurrentDirectMap<K, V, PAGE, D>
where <D as SharedDirectory>::Pages<Page<K, V>>: RefUnwindSafe,

§

impl<K, V, const PAGE: usize, D> Unpin for ConcurrentDirectMap<K, V, PAGE, D>
where <D as SharedDirectory>::Pages<Page<K, V>>: Unpin,

§

impl<K, V, const PAGE: usize, D> UnsafeUnpin for ConcurrentDirectMap<K, V, PAGE, D>
where <D as SharedDirectory>::Pages<Page<K, V>>: UnsafeUnpin,

§

impl<K, V, const PAGE: usize, D> UnwindSafe for ConcurrentDirectMap<K, V, PAGE, D>
where <D as SharedDirectory>::Pages<Page<K, V>>: 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.