pub struct AtomicDirectMap<const PAGE: usize = DEFAULT_PAGE, D: SharedDirectory = Flat> { /* private fields */ }concurrent only.Expand description
A lock-free direct-address map from slot indices to one-word values.
The key is the slot index itself; callers with u32 keys pass key as usize. No key or generation is stored. Use AtomicIdMap for
generational keys. Values are encoded as value + 1, reserving zero for
an empty slot: u64::MAX cannot be stored; see MAX_VALUE.
Slot loads use acquire ordering, successful read-modify-write operations
use acquire-release, and failed compare-exchanges use acquire. Values may
publish other data indexed by them. Slot operations are lock-free; page
installation can allocate. Pages remain 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 rejects out-of-range
inserts with a panic and makes lookups/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 and its atomic storage must fit in isize::MAX bytes.
Length uses wrapping counters, one per thread (per page group without
std), updated after each occupancy change. A removal can be counted
before the insertion it undoes, or on another counter; len
reads their sum as signed and clamps negatives to zero. It is exact when no write is in flight. Traversals are weakly consistent, visit ascending
slots and load each visited slot once; they do not provide a snapshot.
use paged_collections::concurrent::AtomicDirectMap;
let counters = AtomicDirectMap::<4>::new();
// Increment a counter, initializing an absent counter to zero first.
assert_eq!(counters.fetch_update(7, |old| Some(Some(old.unwrap_or(0) + 1))), Ok(None));
assert_eq!(counters.fetch_update(7, |old| Some(Some(old.unwrap_or(0) + 1))), Ok(Some(1)));
assert_eq!(counters.get(7), Some(2));use paged_collections::{concurrent::AtomicDirectMap, page::Radix};
let map = AtomicDirectMap::<4, Radix>::new();
assert_eq!(map.insert(usize::MAX - 1, 42), None);
assert_eq!(map.get(usize::MAX - 1), Some(42));Implementations§
Source§impl<const PAGE: usize, D: SharedDirectory> AtomicDirectMap<PAGE, D>
impl<const PAGE: usize, D: SharedDirectory> AtomicDirectMap<PAGE, D>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty map without allocating.
Panics if PAGE is zero or its atomic 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.
Sums per-thread counters (per page group without std), reading the
sum as signed and clamping negatives to zero. While writes are in
flight, the counters may be
read at different moments, so the result can differ from every count
the map held, though it never exceeds the number of insertions.
Sourcepub fn allocated_pages(&self) -> usize
pub fn allocated_pages(&self) -> usize
Counts installed pages; weakly consistent during page installation.
Sourcepub fn get(&self, key: usize) -> Option<u64>
pub fn get(&self, key: usize) -> Option<u64>
Loads a value with acquire ordering. Out-of-range keys return None.
Sourcepub fn contains_key(&self, key: usize) -> bool
pub fn contains_key(&self, key: usize) -> bool
Whether this slot is occupied.
Sourcepub fn insert(&self, key: usize, value: u64) -> Option<u64>
pub fn insert(&self, key: usize, value: u64) -> Option<u64>
Swaps in value, returning the previous value.
Panics if value == u64::MAX or a Flat key exceeds the spine range.
Sourcepub fn try_insert(&self, key: usize, value: u64) -> Result<(), u64>
pub fn try_insert(&self, key: usize, value: u64) -> Result<(), u64>
Inserts into an empty slot, or returns Err(current).
Panics if value == u64::MAX or a Flat key exceeds the spine range.
Sourcepub fn remove(&self, key: usize) -> Option<u64>
pub fn remove(&self, key: usize) -> Option<u64>
Swaps the slot to empty, returning its previous value.
Out-of-range keys return None without allocating.
Sourcepub fn compare_exchange(
&self,
key: usize,
current: Option<u64>,
new: Option<u64>,
) -> Result<Option<u64>, Option<u64>>
pub fn compare_exchange( &self, key: usize, current: Option<u64>, new: Option<u64>, ) -> Result<Option<u64>, Option<u64>>
Stores new only if the current value equals current, without
spurious failure. None means empty. Returns Ok(previous) on success
or Err(actual) on mismatch. A None write never allocates a page.
Panics if new == Some(u64::MAX) or an inserting Flat key exceeds the spine
range. A nonempty write installs its page before checking current.
Sourcepub fn fetch_update(
&self,
key: usize,
f: impl FnMut(Option<u64>) -> Option<Option<u64>>,
) -> Result<Option<u64>, Option<u64>>
pub fn fetch_update( &self, key: usize, f: impl FnMut(Option<u64>) -> Option<Option<u64>>, ) -> Result<Option<u64>, Option<u64>>
Repeatedly calls f with the current value until a change succeeds or
f returns None. Returns Ok(previous) after storing Some(new);
Some(None) removes the entry. Returning None stops with Err(current).
The callback may run several times, but only one change is committed.
Removing from an absent page does not allocate it.
Panics if f requests storing u64::MAX, or a Flat insertion exceeds the
spine range. A panicking callback leaves the slot unchanged by this call.
Sourcepub fn iter(&self) -> AtomicDirectMapIter<'_, PAGE> ⓘ
pub fn iter(&self) -> AtomicDirectMapIter<'_, PAGE> ⓘ
Iterates in ascending slot order, loading each slot once with acquire ordering. Weakly consistent under mutation; not a snapshot.
Sourcepub fn range(
&self,
keys: impl RangeBounds<usize>,
) -> impl Iterator<Item = (usize, u64)>
pub fn range( &self, keys: impl RangeBounds<usize>, ) -> impl Iterator<Item = (usize, u64)>
Iterates over a key range in ascending order, loading each visited slot once with acquire ordering. Weakly consistent under mutation. Empty or inverted ranges yield nothing.
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.
Sourcepub fn snapshot(&self) -> PagedDirectMap<usize, u64, PAGE, D>
pub fn snapshot(&self) -> PagedDirectMap<usize, u64, PAGE, D>
Copies 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.
Sourcepub fn into_inner(self) -> PagedDirectMap<usize, u64, PAGE, D>
pub fn into_inner(self) -> PagedDirectMap<usize, u64, PAGE, D>
Moves entries into a plain map with the same directory in slot order.
Trait Implementations§
Source§impl<const PAGE: usize, D: SharedDirectory> Clone for AtomicDirectMap<PAGE, D>
Copies via AtomicDirectMap::snapshot.
impl<const PAGE: usize, D: SharedDirectory> Clone for AtomicDirectMap<PAGE, D>
Copies via AtomicDirectMap::snapshot.
Source§impl<const PAGE: usize, D: SharedDirectory> Debug for AtomicDirectMap<PAGE, D>
impl<const PAGE: usize, D: SharedDirectory> Debug for AtomicDirectMap<PAGE, D>
Source§impl<const PAGE: usize, D: SharedDirectory> Default for AtomicDirectMap<PAGE, D>
impl<const PAGE: usize, D: SharedDirectory> Default for AtomicDirectMap<PAGE, D>
Source§impl<'de, const PAGE: usize, D: SharedDirectory> Deserialize<'de> for AtomicDirectMap<PAGE, D>
Reads the plain PagedDirectMap format, rejecting u64::MAX
values and indices outside the concurrent directory’s range.
impl<'de, const PAGE: usize, D: SharedDirectory> Deserialize<'de> for AtomicDirectMap<PAGE, D>
Reads the plain PagedDirectMap format, rejecting u64::MAX
values and indices outside the concurrent directory’s range.
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<const PAGE: usize, D: SharedDirectory> Extend<(usize, u64)> for AtomicDirectMap<PAGE, D>
impl<const PAGE: usize, D: SharedDirectory> Extend<(usize, u64)> for AtomicDirectMap<PAGE, D>
Source§fn extend<T: IntoIterator<Item = (usize, u64)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (usize, u64)>>(&mut self, iter: T)
Inserts entries, panicking if a value is u64::MAX or a Flat key
exceeds the spine range. Entries inserted before a panic remain in the map.
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<const PAGE: usize, D: SharedDirectory> From<PagedDirectMap<usize, u64, PAGE, D>> for AtomicDirectMap<PAGE, D>
impl<const PAGE: usize, D: SharedDirectory> From<PagedDirectMap<usize, u64, PAGE, D>> for AtomicDirectMap<PAGE, D>
Source§impl<const PAGE: usize, D: SharedDirectory> FromIterator<(usize, u64)> for AtomicDirectMap<PAGE, D>
impl<const PAGE: usize, D: SharedDirectory> FromIterator<(usize, u64)> for AtomicDirectMap<PAGE, D>
Source§impl<'a, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a AtomicDirectMap<PAGE, D>
impl<'a, const PAGE: usize, D: SharedDirectory> IntoIterator for &'a AtomicDirectMap<PAGE, D>
Source§impl<const PAGE: usize, D: SharedDirectory> Serialize for AtomicDirectMap<PAGE, D>
Serializes a weakly consistent snapshot as a valid plain map in slot
order, with the same consistency as AtomicDirectMap::snapshot.
Scans the directory and allocated slots, buffering entries to
supply the exact serialized length.
impl<const PAGE: usize, D: SharedDirectory> Serialize for AtomicDirectMap<PAGE, D>
Serializes a weakly consistent snapshot as a valid plain map in slot
order, with the same consistency as AtomicDirectMap::snapshot.
Scans the directory and allocated slots, buffering entries to
supply the exact serialized length.
Auto Trait Implementations§
impl<const PAGE: usize = DEFAULT_PAGE, D = Flat> !Freeze for AtomicDirectMap<PAGE, D>
impl<const PAGE: usize, D> Forget for AtomicDirectMap<PAGE, D>
impl<const PAGE: usize, D> Move for AtomicDirectMap<PAGE, D>
impl<const PAGE: usize, D> RefUnwindSafe for AtomicDirectMap<PAGE, D>where
Words<PAGE, D>: RefUnwindSafe,
impl<const PAGE: usize, D> Send for AtomicDirectMap<PAGE, D>where
Words<PAGE, D>: Send,
impl<const PAGE: usize, D> Sync for AtomicDirectMap<PAGE, D>where
Words<PAGE, D>: Sync,
impl<const PAGE: usize, D> Unpin for AtomicDirectMap<PAGE, D>where
Words<PAGE, D>: Unpin,
impl<const PAGE: usize, D> UnsafeUnpin for AtomicDirectMap<PAGE, D>where
Words<PAGE, D>: UnsafeUnpin,
impl<const PAGE: usize, D> UnwindSafe for AtomicDirectMap<PAGE, D>where
Words<PAGE, D>: 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