pub struct ConcurrentPriorityQueue<K, P, const PAGE: usize = DEFAULT_PAGE, D: Directory = Flat> { /* private fields */ }concurrent only.Expand description
A concurrent indexed max-priority queue with relaxed and exact removal.
Each shard wraps a PagedPriorityQueue. The key’s index page determines
its shard, keeping all generations of a slot together and allocating that
index page in just one shard. PAGE must be nonzero. Workloads with fewer
key pages than shards can distribute entries unevenly and reduce the
effectiveness of relaxed popping. D selects the plain queue’s directory.
Each shard can hold at most u32::MAX entries.
pop returns the greatest of up to two sampled shard tops,
with a fallback scan for nonempty shards. It has no rank guarantee with
multiple shards. A key present throughout the call, with its insertion
happening-before the call, prevents None. pop_exact
and peek_exact select the global maximum. Ties have
unspecified order.
§Locking and panics
Per-key operations lock one shard. Operations holding several shard locks
at once acquire them in ascending shard order. Callbacks and user trait
implementations such as Ord, Clone, Debug and Serialize run under
shard locks and must avoid calling back into this queue, which can deadlock
even on reads. The spin lock used without std permits writer starvation.
Locks ignore poisoning. If a priority comparison or a mutation callback
panics, mutations already made remain in effect and all remaining entries
stay findable with their priorities. Heap order may be broken in the
modified shard, so subsequent maximum results may be unspecified.
See PagedPriorityQueue for its panic guarantees.
use paged_collections::concurrent::ConcurrentPriorityQueue;
let queue = ConcurrentPriorityQueue::<u32, i32, 4>::with_shards(3);
assert_eq!(queue.push(1, 10), None);
assert_eq!(queue.push(5, 20), None);
assert_eq!(queue.change_priority(&1, 30), Some(10));
assert_eq!(queue.pop_exact(), Some((1, 30)));
assert_eq!(queue.pop(), Some((5, 20)));
assert_eq!(queue.pop(), None);Implementations§
Source§impl<K: Key, P, const PAGE: usize, D: Directory> ConcurrentPriorityQueue<K, P, PAGE, D>
impl<K: Key, P, const PAGE: usize, D: Directory> ConcurrentPriorityQueue<K, P, PAGE, D>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty queue with the default shard count.
With std, the count is four times the available parallelism, capped
at 256, or four if parallelism is unknown. Without std it is eight.
§Panics
Panics if PAGE is zero.
Sourcepub fn with_shards(shards: usize) -> Self
pub fn with_shards(shards: usize) -> Self
Sourcepub fn with_shards_and_capacity(shards: usize, capacity: usize) -> Self
pub fn with_shards_and_capacity(shards: usize, capacity: usize) -> Self
Creates an empty queue with dense capacity distributed across shards
shards, rounding up per shard. Index pages are allocated on demand.
§Panics
Panics if shards or PAGE is zero, or the capacity cannot be allocated.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the length hints report an empty queue.
Like len, this is exact when no write is in flight.
Sourcepub fn allocated_pages(&self) -> usize
pub fn allocated_pages(&self) -> usize
Returns the total number of allocated index pages, reading one shard at a time. The total is weakly consistent across shards.
Sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Looks up the exact key, including its generation, under a read lock.
Sourcepub fn priority(&self, key: &K) -> Option<P>where
P: Clone,
pub fn priority(&self, key: &K) -> Option<P>where
P: Clone,
Clones the exact key’s priority under a read lock, or returns None
if absent. The clone must avoid calling back into this queue.
Sourcepub fn with_priority<R>(&self, key: &K, f: impl FnOnce(&P) -> R) -> Option<R>
pub fn with_priority<R>(&self, key: &K, f: impl FnOnce(&P) -> R) -> Option<R>
Calls f with the exact key’s priority under a read lock.
Returns None without calling f if absent. The callback must avoid
calling back into this queue.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the dense arrays and directories, freeing index pages of empty shards.
Source§impl<K: Key, P: Ord, const PAGE: usize, D: Directory> ConcurrentPriorityQueue<K, P, PAGE, D>
impl<K: Key, P: Ord, const PAGE: usize, D: Directory> ConcurrentPriorityQueue<K, P, PAGE, D>
Sourcepub fn push(&self, key: K, priority: P) -> Option<P>
pub fn push(&self, key: K, priority: P) -> Option<P>
Inserts a key or updates its priority under one shard write lock.
Returns the previous priority of the same key, keeping the stored
key. Another generation occupying the slot is evicted and yields
None.
Sourcepub fn try_push(&self, key: K, priority: P) -> Result<Option<P>, (K, P)>
pub fn try_push(&self, key: K, priority: P) -> Result<Option<P>, (K, P)>
Inserts or updates under one shard write lock, refusing an occupied slot held by another generation.
Returns the previous priority on update, or the supplied pair on refusal.
Sourcepub fn replace(&self, key: K, priority: P) -> Option<(K, P)>
pub fn replace(&self, key: K, priority: P) -> Option<(K, P)>
Stores the supplied key and priority under one shard write lock.
Returns the former occupant of the slot, including an evicted generation or an equal key.
Sourcepub fn push_increase(&self, key: K, priority: P) -> bool
pub fn push_increase(&self, key: K, priority: P) -> bool
Inserts an absent key or raises its priority if strictly greater,
returning whether the queue changed. An absent generation evicts the
slot’s occupant as in push.
Sourcepub fn push_decrease(&self, key: K, priority: P) -> bool
pub fn push_decrease(&self, key: K, priority: P) -> bool
Inserts an absent key or lowers its priority if strictly smaller,
returning whether the queue changed. An absent generation evicts the
slot’s occupant as in push.
Sourcepub fn change_priority(&self, key: &K, priority: P) -> Option<P>
pub fn change_priority(&self, key: &K, priority: P) -> Option<P>
Changes the priority of the exact key under a write lock, returning
its previous priority or None if absent.
Sourcepub fn change_priority_with(&self, key: &K, change: impl FnOnce(&mut P)) -> bool
pub fn change_priority_with(&self, key: &K, change: impl FnOnce(&mut P)) -> bool
Mutates the exact key’s priority under a write lock and repairs heap order. Returns whether the key was present.
The callback runs only for a present key and must avoid calling back into this queue. If it panics, its changes remain and heap order may be broken.
Sourcepub fn remove(&self, key: &K) -> Option<(K, P)>
pub fn remove(&self, key: &K) -> Option<(K, P)>
Removes and returns the stored pair for the exact key, or None if
absent, under one shard write lock.
Sourcepub fn pop(&self) -> Option<(K, P)>
pub fn pop(&self) -> Option<(K, P)>
Removes an entry using relaxed priority ordering.
Samples two distinct shards, preferring nonzero length hints. Locks both in ascending shard order and removes the greater top. If both are empty, visits every shard once from a random start, locking one at a time, and removes the first top it finds.
With one shard this removes the maximum. With multiple shards there is no rank guarantee. Rihani, Sanders and Dementiev (2015) report empirical expected rank O(shards) for MultiQueues.
Returns None only if every shard was empty when visited. An entry
present throughout the call, with its insertion happening-before the
call, therefore prevents None because entries never migrate.
Sourcepub fn pop_exact(&self) -> Option<(K, P)>
pub fn pop_exact(&self) -> Option<(K, P)>
Removes the global maximum, or returns None if empty.
Acquires all shard write locks in ascending order. This operation is linearizable.
Sourcepub fn peek_exact(&self) -> Option<(K, P)>where
P: Clone,
pub fn peek_exact(&self) -> Option<(K, P)>where
P: Clone,
Copies the key and clones the priority of the global maximum, or
returns None if empty.
Acquires all shard read locks in ascending order for a linearizable
view. The priority’s Clone must avoid calling back into this queue.
Sourcepub fn into_sorted_vec(self) -> Vec<(K, P)>
pub fn into_sorted_vec(self) -> Vec<(K, P)>
Consumes the queue into entries in ascending priority order.
Sourcepub fn into_queue(self) -> PagedPriorityQueue<K, P, PAGE, D>
pub fn into_queue(self) -> PagedPriorityQueue<K, P, PAGE, D>
Consumes the queue and merges its entries into one plain queue.
§Panics
Panics if the entries exceed the plain queue’s u32::MAX limit or
if comparing priorities panics.
Sourcepub fn snapshot(&self) -> PagedPriorityQueue<K, P, PAGE, D>where
P: Clone,
pub fn snapshot(&self) -> PagedPriorityQueue<K, P, PAGE, D>where
P: Clone,
Copies entries into a plain queue using one shard read lock at a time.
The snapshot is consistent within each shard and weakly consistent across shards. An entry that stays unchanged throughout the call is included if its insertion happens-before the call. Priority comparisons and clones must avoid calling back into this queue.
§Panics
Panics if the entries copied exceed the plain queue’s u32::MAX
limit or if cloning or comparing priorities panics.
Trait Implementations§
Source§impl<K: Key, P: Ord + Clone, const PAGE: usize, D: Directory> Clone for ConcurrentPriorityQueue<K, P, PAGE, D>
Rebuilds the queue from a weakly consistent snapshot with the same shard
count. See ConcurrentPriorityQueue::snapshot for locking.
impl<K: Key, P: Ord + Clone, const PAGE: usize, D: Directory> Clone for ConcurrentPriorityQueue<K, P, PAGE, D>
Rebuilds the queue from a weakly consistent snapshot with the same shard
count. See ConcurrentPriorityQueue::snapshot for locking.
Source§impl<K: Key + Debug, P: Debug, const PAGE: usize, D: Directory> Debug for ConcurrentPriorityQueue<K, P, PAGE, D>
Formats entries as a map using one shard read lock at a time, with the
same consistency as ConcurrentPriorityQueue::snapshot. User formatting
must avoid calling back into the queue.
impl<K: Key + Debug, P: Debug, const PAGE: usize, D: Directory> Debug for ConcurrentPriorityQueue<K, P, PAGE, D>
Formats entries as a map using one shard read lock at a time, with the
same consistency as ConcurrentPriorityQueue::snapshot. User formatting
must avoid calling back into the queue.
Source§impl<K: Key, P, const PAGE: usize, D: Directory> Default for ConcurrentPriorityQueue<K, P, PAGE, D>
impl<K: Key, P, const PAGE: usize, D: Directory> Default for ConcurrentPriorityQueue<K, P, PAGE, D>
Source§impl<'de, K: Key + Deserialize<'de>, P: Ord + Deserialize<'de>, const PAGE: usize, D: Directory> Deserialize<'de> for ConcurrentPriorityQueue<K, P, PAGE, D>
Reads the plain queue format and redistributes entries into the
default number of shards.
impl<'de, K: Key + Deserialize<'de>, P: Ord + Deserialize<'de>, const PAGE: usize, D: Directory> Deserialize<'de> for ConcurrentPriorityQueue<K, P, PAGE, D>
Reads the plain queue format and redistributes entries into the default number of shards.
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: Key, P: Ord, const PAGE: usize, D: Directory> Extend<(K, P)> for ConcurrentPriorityQueue<K, P, PAGE, D>
Pushes entries under exclusive access. Later entries in a slot win.
impl<K: Key, P: Ord, const PAGE: usize, D: Directory> Extend<(K, P)> for ConcurrentPriorityQueue<K, P, PAGE, D>
Pushes entries under exclusive access. Later entries in a slot win.
Source§fn extend<I: IntoIterator<Item = (K, P)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, P)>>(&mut self, iter: I)
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: Key, P: Ord, const PAGE: usize, D: Directory> From<PagedPriorityQueue<K, P, PAGE, D>> for ConcurrentPriorityQueue<K, P, PAGE, D>
Moves a plain queue into the default number of shards by re-pushing its
entries in O(n log n) time.
impl<K: Key, P: Ord, const PAGE: usize, D: Directory> From<PagedPriorityQueue<K, P, PAGE, D>> for ConcurrentPriorityQueue<K, P, PAGE, D>
Moves a plain queue into the default number of shards by re-pushing its entries in O(n log n) time.
Source§fn from(queue: PagedPriorityQueue<K, P, PAGE, D>) -> Self
fn from(queue: PagedPriorityQueue<K, P, PAGE, D>) -> Self
Source§impl<K: Key, P: Ord, const PAGE: usize, D: Directory> FromIterator<(K, P)> for ConcurrentPriorityQueue<K, P, PAGE, D>
Collects entries into the default number of shards. Later entries in a
slot win.
impl<K: Key, P: Ord, const PAGE: usize, D: Directory> FromIterator<(K, P)> for ConcurrentPriorityQueue<K, P, PAGE, D>
Collects entries into the default number of shards. Later entries in a slot win.
Source§impl<K: Key, P, const PAGE: usize, D: Directory> IntoIterator for ConcurrentPriorityQueue<K, P, PAGE, D>
Consumes the queue and yields entries in arbitrary order.
impl<K: Key, P, const PAGE: usize, D: Directory> IntoIterator for ConcurrentPriorityQueue<K, P, PAGE, D>
Consumes the queue and yields entries in arbitrary order.
Source§impl<K: Key + Serialize, P: Serialize, const PAGE: usize, D: Directory> Serialize for ConcurrentPriorityQueue<K, P, PAGE, D>
Serializes a sequence of key-priority pairs in the plain queue format,
holding one shard read lock at a time. User serialization must avoid
calling back into the queue.
impl<K: Key + Serialize, P: Serialize, const PAGE: usize, D: Directory> Serialize for ConcurrentPriorityQueue<K, P, PAGE, D>
Serializes a sequence of key-priority pairs in the plain queue format, holding one shard read lock at a time. User serialization must avoid calling back into the queue.
The length of the weakly consistent view is unknown upfront. For a
serializer that requires a sequence length, serialize
ConcurrentPriorityQueue::snapshot instead.
Auto Trait Implementations§
impl<K, P, const PAGE: usize, D> Forget for ConcurrentPriorityQueue<K, P, PAGE, D>
impl<K, P, const PAGE: usize, D> Freeze for ConcurrentPriorityQueue<K, P, PAGE, D>
impl<K, P, const PAGE: usize, D> Move for ConcurrentPriorityQueue<K, P, PAGE, D>
impl<K, P, const PAGE: usize, D> RefUnwindSafe for ConcurrentPriorityQueue<K, P, PAGE, D>where
Box<[CachePadded<Shard<K, P, PAGE, D>>]>: RefUnwindSafe,
impl<K, P, const PAGE: usize, D> Send for ConcurrentPriorityQueue<K, P, PAGE, D>
impl<K, P, const PAGE: usize, D> Sync for ConcurrentPriorityQueue<K, P, PAGE, D>
impl<K, P, const PAGE: usize, D> Unpin for ConcurrentPriorityQueue<K, P, PAGE, D>
impl<K, P, const PAGE: usize, D> UnsafeUnpin for ConcurrentPriorityQueue<K, P, PAGE, D>where
Box<[CachePadded<Shard<K, P, PAGE, D>>]>: UnsafeUnpin,
impl<K, P, const PAGE: usize, D> UnwindSafe for ConcurrentPriorityQueue<K, P, PAGE, D>where
Box<[CachePadded<Shard<K, P, 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