pub struct ConcurrentDirectMap<K: DirectKey, V, const PAGE: usize = DEFAULT_PAGE, D: SharedDirectory = Flat> { /* private fields */ }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>
impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> ConcurrentDirectMap<K, V, PAGE, D>
Sourcepub fn snapshot(&self) -> PagedDirectMap<K, V, PAGE, D>where
V: Clone,
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.
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty map without allocating.
§Panics
Panics if PAGE is zero or its slot storage exceeds isize::MAX bytes.
Sourcepub fn len(&self) -> usize
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.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether len() is zero; like len, exact only with no write in flight.
Sourcepub fn allocated_pages(&self) -> usize
pub fn allocated_pages(&self) -> usize
Counts installed pages by scanning the directory without page locks. Weakly consistent while pages are being installed.
Sourcepub fn contains_key(&self, key: K) -> bool
pub fn contains_key(&self, key: K) -> bool
Whether the requested key itself is present.
Sourcepub fn get(&self, key: K) -> Option<DirectMapRef<'_, K, V>>
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.
Sourcepub fn get_mut(&self, key: K) -> Option<DirectMapRefMut<'_, K, V>>
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.
Sourcepub fn with<R>(&self, key: K, f: impl FnOnce(&V) -> R) -> Option<R>
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.
Sourcepub fn get_cloned(&self, key: K) -> Option<V>where
V: Clone,
pub fn get_cloned(&self, key: K) -> Option<V>where
V: Clone,
Clones a present value under its page’s read lock.
Sourcepub fn insert(&self, key: K, value: V) -> Option<V>
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.
Sourcepub fn replace(&self, key: K, value: V) -> Option<(K, V)>
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.
Sourcepub fn try_insert(&self, key: K, value: V) -> Result<(), V>
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.
Sourcepub fn remove(&self, key: K) -> Option<V>
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.
Sourcepub fn remove_entry(&self, key: K) -> Option<(K, V)>
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.
Sourcepub fn remove_occupant(&self, index: usize) -> Option<(K, V)>
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.
Sourcepub fn get_or_insert_with(
&self,
key: K,
make: impl FnOnce() -> V,
) -> DirectMapRefMut<'_, K, V>
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.
Sourcepub fn entry(&self, key: K) -> Entry<'_, K, V>
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.
Sourcepub fn retain(&self, keep: impl FnMut(&K, &mut V) -> bool)
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.
Sourcepub fn for_each(&self, f: impl FnMut(&K, &V))
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.
Sourcepub fn range_for_each(&self, keys: impl RangeBounds<K>, f: impl FnMut(&K, &V))
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.
Sourcepub fn iter_mut(&mut self) -> DirectMapIterMut<'_, K, V, PAGE> ⓘ
pub fn iter_mut(&mut self) -> DirectMapIterMut<'_, K, V, PAGE> ⓘ
Iterates exclusively in ascending slot order without acquiring page locks.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes entries without locking, retaining pages for reuse. If a destructor panics, unvisited entries remain and counts stay correct.
Sourcepub fn drain(&mut self) -> IntoIter<(K, V)> ⓘ
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.
Sourcepub fn shrink_to_fit(&mut self)
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§impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> ConcurrentDirectMap<K, V, PAGE, D>
impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> ConcurrentDirectMap<K, V, PAGE, D>
Sourcepub fn into_inner(self) -> PagedDirectMap<K, V, PAGE, D>
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.
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§impl<K: DirectKey + Debug, V: Debug, const PAGE: usize, D: SharedDirectory> Debug for ConcurrentDirectMap<K, V, PAGE, D>
impl<K: DirectKey + Debug, V: Debug, const PAGE: usize, D: SharedDirectory> Debug for ConcurrentDirectMap<K, V, PAGE, D>
Source§impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> Default for ConcurrentDirectMap<K, V, PAGE, D>
impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> Default for ConcurrentDirectMap<K, V, PAGE, D>
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.
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>
fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>
Source§impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> Extend<(K, V)> for ConcurrentDirectMap<K, V, PAGE, D>
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)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> From<PagedDirectMap<K, V, PAGE, D>> for ConcurrentDirectMap<K, V, PAGE, D>
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
fn from(map: PagedDirectMap<K, V, PAGE, D>) -> Self
Source§impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> FromIterator<(K, V)> for ConcurrentDirectMap<K, V, PAGE, D>
impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> FromIterator<(K, V)> for ConcurrentDirectMap<K, V, PAGE, D>
Source§impl<'a, K: DirectKey + 'a, V: 'a, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a mut ConcurrentDirectMap<K, V, PAGE, D>
impl<'a, K: DirectKey + 'a, V: 'a, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a mut ConcurrentDirectMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> Send for ConcurrentDirectMap<K, V, PAGE, D>
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.
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.
impl<K, V, const PAGE: usize, D> Sync for ConcurrentDirectMap<K, V, PAGE, D>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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