Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Overview

paged-collections provides sparse sets, maps, a priority queue and stable-address containers whose memory is split into fixed-size pages. The core is a no_std Rust crate (alloc only). The Python package wraps it with PyO3 and exchanges batches with NumPy.

A paged index can leave some pages unallocated. Paged storage can add a page while existing elements stay at their addresses.

Documentation

Two Uses of Paging

  • Paged index. A table indexed by key is split into pages allocated on first use. A write to an isolated key allocates only the page it touches. With the default Flat directory, the pointer table still grows up to the highest page number used. Sparse sets, sparse maps, direct maps and priority queues use a paged index.
  • Paged storage. Growth adds pages while existing elements stay at their addresses. The paged vector, deque and hive use paged storage.

See Paging Model for page sizes, directories and memory behaviour.

Example

A Hive stores elements at stable addresses and returns generational ids. Those ids can index the other maps directly to create side tables:

from paged_collections import Hive, IdMap, id_generation, id_index

hive = Hive()
a = hive.insert("a")
b = hive.insert("b")
score = IdMap([a, b], [10, 20])
hive.pop(a)
c = hive.insert("c")
print("reused slot =", (id_index(c), id_generation(c)))
print("new side entry =", score.get(c))
# Writing the new generation evicts the stale entry from the side table.
score[c] = 30
print("stale hive =", hive.get(a))
print("stale side =", score.get(a))
print("side values =", score.values())
use paged_collections::direct_map::PagedDirectMap;
use paged_collections::hive::Hive;
use paged_collections::id_alloc::Id;

fn main() {
    let mut hive: Hive<&str> = Hive::new();
    let a = hive.insert("a");
    let b = hive.insert("b");
    let mut score: PagedDirectMap<Id, u32> = PagedDirectMap::new();
    score.insert(a, 10);
    score.insert(b, 20);
    hive.remove(a);
    let c = hive.insert("c");
    println!("reused slot = {:?}", (c.index(), c.generation().get()));
    println!("new side entry = {:?}", score.get(c));
    // Writing the new generation evicts the stale entry from the side table.
    score.insert(c, 30);
    println!("stale hive = {:?}", hive.get(a));
    println!("stale side = {:?}", score.get(a));
    let values: Vec<_> = score.range(a..=b).map(|(_, &v)| v).collect();
    println!("range = {values:?}");
}

Python output:

reused slot = (0, 2)
new side entry = None
stale hive = None
stale side = None
side values = [30, 20]

The Rust example queries the direct map by key range and prints range = [30, 20] in place of the last line.

Containers

RustPythonPagingPurpose
PagedSparseSet<K>SparseSetindexInteger set: O(1) insert, remove, contains and clear; dense iteration
PagedSparseMap<K, V>SparseMap, SparseMapF64, …indexSparse set with a dense value array
PagedDirectMap<K, V>DirectMap, DirectMapF64, …indexValues stored in place; key order and range queries
PagedPriorityQueue<K, P>PriorityQueue, PriorityQueueF64indexIndexed heap with priority changes and removal by key
ConcurrentPriorityQueue<K, P>ConcurrentPriorityQueue, ConcurrentPriorityQueueF64indexSharded max-priority queue; relaxed pop, exact pop_exact
PagedVec<T>PagedList, PagedListF64, …storageGrowable vector with stable addresses
PagedDeque<T>PagedDequestorageDouble-ended queue with stable addresses
ConcurrentQueue<T>ConcurrentQueuestorageShared FIFO queue; waits at page boundaries
Hive<T>HivestorageUnordered slots with generational ids
IdAllocatorIdAllocator, IdMapnoneGenerational id allocation for use as keys

Thread-safe variants are listed under Concurrent Containers. The full Rust API is on docs.rs; the Python API is typed in _core.pyi.