Scalable Concurrent Containers
A collection of high performance containers and utilities for concurrent and asynchronous programming.
Features
- Asynchronous counterparts of blocking and synchronous methods.
Equivalent
,Loom
andSerde
support:features = ["equivalent", "loom", "serde"]
.- Near-linear scalability.
- No spin-locks and no busy loops.
- SIMD lookup to scan multiple entries in parallel: require
RUSTFLAGS='-C target_feature=+avx2'
onx86_64
.
Concurrent and Asynchronous Containers
HashMap
is a concurrent and asynchronous hash map.HashSet
is a concurrent and asynchronous hash set.HashIndex
is a read-optimized concurrent and asynchronous hash map.HashCache
is a 32-way associative cache backed byHashMap
.TreeIndex
is a read-optimized concurrent and asynchronous B-plus tree.
Utilities for Concurrent Programming
LinkedList
is a type trait implementing a lock-free concurrent singly linked list.Queue
is a concurrent lock-free first-in-first-out container.Stack
is a concurrent lock-free last-in-first-out container.Bag
is a concurrent lock-free unordered opaque container.
HashMap
HashMap
is a concurrent hash map, optimized for highly parallel write-heavy workloads. HashMap
is structured as a lock-free stack of entry bucket arrays. The entry bucket array is managed by sdd
, thus enabling lock-free access to it and non-blocking container resizing. Each bucket is a fixed-size array of entries, and it is protected by a special read-write lock which provides both blocking and asynchronous methods.
Locking behavior
Entry access: fine-grained locking
Read/write access to an entry is serialized by the read-write lock in the bucket containing the entry. There are no container-level locks, therefore, the larger the container gets, the lower the chance of the bucket-level lock being contended.
Resize: lock-free
Resizing of a HashMap
is completely non-blocking and lock-free; resizing does not block any other read/write access to the container or resizing attempts. Resizing is analogous to pushing a new bucket array into a lock-free stack. Each entry in the old bucket array will be incrementally relocated to the new bucket array on future access to the container, and the old bucket array gets dropped eventually after it becomes empty.
Examples
An entry can be inserted if the key is unique. The inserted entry can be updated, read, and removed synchronously or asynchronously.
use HashMap;
let hashmap: = default;
assert!;
assert!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
hashmap.entry.or_insert;
assert_eq!;
let future_insert = hashmap.insert_async;
let future_remove = hashmap.remove_async;
The Entry
API of HashMap
is useful if the workflow is complicated.
use HashMap;
let hashmap: = default;
hashmap.entry.or_insert;
assert_eq!;
hashmap.entry.and_modify.or_insert;
assert_eq!;
HashMap
does not provide an Iterator
since it is impossible to confine the lifetime of Iterator::Item
to the Iterator. The limitation can be circumvented by relying on interior mutability, e.g., let the returned reference hold a lock, however it will easily lead to a deadlock if not correctly used, and frequent acquisition of locks may impact performance. Therefore, Iterator
is not implemented, instead, HashMap
provides a number of methods to iterate over entries synchronously or asynchronously: any
, any_async
, first_entry
, first_entry_async
, prune
, prune_async
, retain
, retain_async
, scan
, scan_async
, OccupiedEntry::next
, and OccupiedEntry::next_async
.
use HashMap;
let hashmap: = default;
assert!;
assert!;
// Entries can be modified or removed via `retain`.
let mut acc = 0;
hashmap.retain;
assert_eq!;
assert_eq!;
assert_eq!;
// `any` returns `true` as soon as an entry satisfying the predicate is found.
assert!;
assert!;
// Multiple entries can be removed through `retain`.
hashmap.retain;
// `hash_map::OccupiedEntry` also can return the next closest occupied entry.
let first_entry = hashmap.first_entry;
assert_eq!;
let second_entry = first_entry.and_then;
assert!;
// Asynchronous iteration over entries using `scan_async`.
let future_scan = hashmap.scan_async;
assert!;
// Asynchronous iteration over entries using the `Entry` API.
let future_iter = async ;
assert!;
HashSet
HashSet
is a special version of HashMap
where the value type is ()
.
Examples
Most HashSet
methods are identical to that of HashMap
except that they do not receive a value argument, and some HashMap
methods for value modification are not implemented for HashSet
.
use HashSet;
let hashset: = default;
assert!;
assert!;
assert!;
let future_insert = hashset.insert_async;
let future_remove = hashset.remove_async;
HashIndex
HashIndex
is a read-optimized version of HashMap
. In a HashIndex
, not only is the memory of the bucket array managed by sdd
, but also that of entry buckets is protected by sdd
, enabling lock-free read access to individual entries.
Entry lifetime
HashIndex
does not drop removed entries immediately, instead they are dropped when one of the following conditions is met.
Epoch
reaches the next generation since the last entry removal in a bucket, and the bucket is write-accessed.HashIndex
is cleared, or resized.- Buckets full of removed entries occupy 50% of the capacity.
Those conditions do not guarantee that the removed entry is dropped within a definite period of time, therefore HashIndex
would not be an optimal choice if the workload is write-heavy and the entry size is large.
Examples
The peek
and peek_with
methods are completely lock-free.
use HashIndex;
let hashindex: = default;
assert!;
// `peek` and `peek_with` are lock-free.
assert_eq!;
let future_insert = hashindex.insert_async;
let future_remove = hashindex.remove_if_async;
The Entry
API of HashIndex
can be used to update an entry in-place.
use HashIndex;
let hashindex: = default;
assert!;
if let Some = hashindex.get ;
if let Some = hashindex.get ;
An Iterator
is implemented for HashIndex
, because any derived references can survive as long as the associated ebr::Guard
lives.
use Guard;
use HashIndex;
let hashindex: = default;
assert!;
// Existing values can be replaced with a new one.
hashindex.get.unwrap.update;
let guard = new;
// An `Guard` has to be supplied to `iter`.
let mut iter = hashindex.iter;
// The derived reference can live as long as `guard`.
let entry_ref = iter.next.unwrap;
assert_eq!;
drop;
// The entry can be read after `hashindex` is dropped.
assert_eq!;
HashCache
HashCache
is a 32-way associative concurrent cache that is based on the HashMap
implementation. HashCache
does not keep track of the least recently used entry in the entire cache, instead each bucket maintains a doubly linked list of occupied entries which is updated on access to entries in order to keep track of the least recently used entry within the bucket.
Examples
The LRU entry in a bucket is evicted when a new entry is being inserted and the bucket is full.
use HashCache;
let hashcache: = with_capacity;
/// The capacity cannot exceed the maximum capacity.
assert_eq!;
/// If the bucket corresponding to `1` or `2` is full, the LRU entry will be evicted.
assert!;
assert!;
/// `1` becomes the most recently accessed entry in the bucket.
assert!;
/// An entry can be normally removed.
assert_eq!;
TreeIndex
TreeIndex
is a B-plus tree variant optimized for read operations. sdd
protects the memory used by individual entries, thus enabling lock-free read access to them.
Locking behavior
Read access is always lock-free and non-blocking. Write access to an entry is also lock-free and non-blocking as long as no structural changes are required. However, when nodes are being split or merged by a write operation, other write operations on keys in the affected range are blocked.
Entry lifetime
TreeIndex
does not drop removed entries immediately, instead they are dropped when the leaf node is cleared or split, and this makes TreeIndex
a sub-optimal choice if the workload is write-heavy.
Examples
An entry can be inserted if the key is unique, and it can be read, and removed afterwards. Locks are acquired or awaited only when internal nodes are split or merged.
use TreeIndex;
let treeindex: = new;
assert!;
// `peek` and `peek_with` are lock-free.
assert_eq!;
assert!;
let future_insert = treeindex.insert_async;
let future_remove = treeindex.remove_if_async;
Entries can be scanned without acquiring any locks.
use TreeIndex;
use Guard;
let treeindex: = new;
assert!;
assert!;
assert!;
let guard = new;
// `iter` iterates over entries without acquiring a lock.
let mut iter = treeindex.iter;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
A specific range of keys can be scanned.
use Guard;
use TreeIndex;
let treeindex: = new;
for i in 0..10
let guard = new;
assert_eq!;
assert_eq!;
assert_eq!;
Bag
Bag
is a concurrent lock-free unordered container. Bag
is completely opaque, disallowing access to contained instances until they are popped. Bag
is especially efficient if the number of contained instances can be maintained under ARRAY_LEN (default: usize::BITS / 2)
Examples
use Bag;
let bag: = default;
bag.push;
assert!;
assert_eq!;
assert!;
Queue
Queue is a concurrent lock-free first-in-first-out container backed by sdd
.
Examples
use Queue;
let queue: = default;
queue.push;
assert!;
assert!;
assert_eq!;
assert_eq!;
assert!;
Stack
Stack
is a concurrent lock-free last-in-first-out container backed by sdd
.
Examples
use Stack;
let stack: = default;
stack.push;
stack.push;
assert_eq!;
assert_eq!;
assert!;
LinkedList
LinkedList
is a type trait that implements lock-free concurrent singly linked list operations, backed by sdd
. It additionally provides a method for marking an entry of a linked list to denote a user-defined state.
Examples
use ;
use LinkedList;
use Relaxed;
;
let guard = new;
let head: L = default;
let tail: = new;
// A new entry is pushed.
assert!;
assert!;
// Users can mark a flag on an entry.
head.mark;
assert!;
// `next_ptr` traverses the linked list.
let next_ptr = head.next_ptr;
assert_eq!;
// Once `tail` is deleted, it becomes invisible.
tail.delete_self;
assert!;
Performance
HashMap
Tail Latency
The expected tail latency of a distribution of latencies of 1048576 insertion operations (K = u64, V = u64
) ranges from 180 microseconds to 200 microseconds on Apple M2 Max.