Skip to main content

ConcurrentSparseMap

Struct ConcurrentSparseMap 

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

A paged sparse map with insert-only shared access and exclusive editing.

Slots, prefix snapshots and directory selection follow ConcurrentSparseSet. An occupied slot refuses a new entry, including another generation. Existing values are never replaced while shared, so lookups return stable &V references without guards. Shared insertion can interleave dense order; exclusive removal uses swap-remove.

Same-slot insertions wait for the winning writer, including its factory. Factories must not recursively insert into their own reserved slot, and factories on two threads that each insert the other’s pending key deadlock (each waits on the other’s reservation). Sync requires both K and V to be Send + Sync. Uses core and alloc only.

With Flat, memory follows the highest page and lookups cost O(1); its finite index range can panic. Radix accepts any usize index, uses directory memory proportional to installed pages, and looks up pages in O(depth).

use paged_collections::{concurrent::ConcurrentSparseMap, page::Radix};
let map = ConcurrentSparseMap::<usize, _, 4, Radix>::new();
assert_eq!(map.try_insert(usize::MAX - 1, "wide"), Ok(&"wide"));
assert_eq!(map.get(&(usize::MAX - 1)), Some(&"wide"));

Implementations§

Source§

impl<K: Key, V, const PAGE: usize, D: SharedDirectory> ConcurrentSparseMap<K, V, PAGE, D>

Source

pub const PAGE_SIZE: usize = PAGE

Number of slots per sparse page and entries per dense page.

Source

pub fn new() -> Self

Creates an empty map without allocating.

§Panics

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

Source

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

Adds an entry if its slot is vacant, returning a shared value reference. Returns Err((key, value)) for a duplicate or another generation. Waits for a writer already reserving that slot to publish or roll back.

§Panics

Panics at u32::MAX - 1 entries, for indices outside the Flat spine range, or on allocation capacity overflow. Allocation can also fail.

Source

pub fn try_insert_many( &self, entries: impl IntoIterator<Item = (K, V)>, ) -> usize

Adds every entry whose slot is vacant, dropping the others, and returns how many were added. Existing entries and earlier duplicates win. Batches share one dense reservation, which scales across threads far better than repeated try_insert.

Each batch is read and its keys validated before any slots are reserved. If the iterator panics, earlier batches stay inserted.

§Panics

As try_insert.

Source

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

The value for this key, including completed appends beyond len. Does not wait for in-flight insertions. With Flat, panics for an out-of-range index.

Source

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

Whether this exact key is present, without waiting for insertions. With Flat, panics for an out-of-range key index.

Source

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

Gets this key’s value, or inserts make() if its slot is vacant. Returns None only if another generation occupies the slot.

Only the RESERVED winner calls make, at most once per call. If it panics, a guard restores the slot to empty and other writers may retry. The factory must not insert recursively into this same slot: it would wait on its own reservation. Nor may two threads’ factories insert each other’s pending keys: each would wait on the other’s reservation. Factories run before dense reservation.

§Panics

As try_insert, or if make panics.

Source

pub fn len(&self) -> usize

Length of the committed prefix; can lag while an earlier append runs.

Source

pub fn is_empty(&self) -> bool

Whether the committed prefix is empty, subject to the lag of len.

Source

pub fn iter(&self) -> SparseMapIter<'_, K, V, PAGE> ⓘ

Iterates over a snapshot of committed (&key, &value) pairs.

Source

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

Mutable access to a matching key’s value under exclusive access. With Flat, panics for an out-of-range key index.

Source

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

Removes a matching entry and returns its value, moving the last entry into its position. With Flat, panics for an out-of-range key index.

Source

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

Iterates over (&key, &mut value) pairs under exclusive access. Keys cannot be changed, preserving the sparse index.

Source

pub fn clear(&mut self)

Drops every entry and clears readiness, retaining pages. Takes O(len). If a destructor panics, that entry is gone; remaining entries keep their sparse positions and may still be used.

Source

pub fn shrink_to_fit(&mut self)

Frees index pages with no entry and dense pages beyond the last entry, keeping directory chunks. Stored entries keep their addresses and positions. Shared inserts can allocate the released pages again.

Source

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

Removes all entries and returns them as (key, value) pairs in dense order. Keeps allocated pages. Takes O(len) time and temporary storage.

Source

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

Clones the committed prefix into a plain map in O(prefix length). Like len and iter, this can omit completed inserts beyond an earlier unpublished reservation. The entries form a valid plain map.

Source

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

Moves entries into a plain map with the same directory in dense order, in O(len). Values move without cloning; their addresses can change.

Trait Implementations§

Source§

impl<K: Key, V: Clone, const PAGE: usize, D: SharedDirectory> Clone for ConcurrentSparseMap<K, V, PAGE, D>

Clones the committed prefix via ConcurrentSparseMap::snapshot, which can omit completed inserts beyond an unpublished reservation.

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: Key + Debug, V: Debug, const PAGE: usize, D: SharedDirectory> Debug for ConcurrentSparseMap<K, V, PAGE, D>

Source§

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

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

impl<K: Key, V, const PAGE: usize, D: SharedDirectory> Default for ConcurrentSparseMap<K, V, PAGE, D>

Source§

fn default() -> Self

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

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

Reads the plain PagedSparseMap format, rejecting keys and lengths outside the concurrent map’s capacity. Later entries replace earlier entries at the same index in place, as in the plain map.

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: Key, V, const PAGE: usize, D: SharedDirectory> Extend<(K, V)> for ConcurrentSparseMap<K, V, PAGE, D>

Inserts entries in input order, keeping the first occupant of each slot.

Source§

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

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: Key, V, const PAGE: usize, D: SharedDirectory> From<PagedSparseMap<K, V, PAGE, D>> for ConcurrentSparseMap<K, V, PAGE, D>

Source§

fn from(inner: PagedSparseMap<K, V, PAGE, D>) -> Self

Converts to this type from the input type.
Source§

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

Collects entries in input order, keeping the first occupant of each slot.

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, K: Key, V, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a ConcurrentSparseMap<K, V, PAGE, D>

Source§

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

The type of the elements being iterated over.
Source§

type IntoIter = SparseMapIter<'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<'a, K: Key, V, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a mut ConcurrentSparseMap<K, V, PAGE, D>

Source§

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

The type of the elements being iterated over.
Source§

type IntoIter = SparseMapIterMut<'a, K, V>

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: Key + Send, V: Send, const PAGE: usize, D: SharedDirectory> Send for ConcurrentSparseMap<K, V, PAGE, D>

Source§

impl<K: Key + Serialize, V: Serialize, const PAGE: usize, D: SharedDirectory> Serialize for ConcurrentSparseMap<K, V, PAGE, D>

Serializes the committed entries in dense order as a valid plain map in O(prefix length). The prefix can omit completed inserts beyond an earlier unpublished reservation, as in ConcurrentSparseMap::iter.

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: Key + Send + Sync, V: Send + Sync, const PAGE: usize, D: SharedDirectory> Sync for ConcurrentSparseMap<K, V, PAGE, D>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

impl<K, V, const PAGE: usize, D> Unpin for ConcurrentSparseMap<K, V, PAGE, D>
where Index<PAGE, D>: Unpin, AppendLog<(K, V), PAGE>: Unpin,

§

impl<K, V, const PAGE: usize, D> UnsafeUnpin for ConcurrentSparseMap<K, V, PAGE, D>
where Index<PAGE, D>: UnsafeUnpin, AppendLog<(K, V), PAGE>: UnsafeUnpin,

§

impl<K, V, const PAGE: usize, D> UnwindSafe for ConcurrentSparseMap<K, V, PAGE, D>
where Index<PAGE, D>: UnwindSafe, AppendLog<(K, V), PAGE>: 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.