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

Paged Vector

PagedVec<T, PAGE = 1024> is a growable vector stored in fixed-size pages. A page is allocated once and never reallocated, so pushing never moves or copies existing elements.

See how paged storage grows.

Python: PagedList (objects) and PagedListF64, PagedListF32, PagedListI64, PagedListI32, PagedListU64, PagedListU32, PagedListBool. These classes implement the list protocol. paged_list(dtype) creates an instance of the class for a dtype.

Example

from paged_collections import PagedList

values = PagedList([1, 2, 3])
values.extend(range(4, 10_001))
print("len =", len(values))
print("first =", values[0])
print("last =", values[-1])
print("popped =", values.pop())
values.insert(1, 99)
print("removed =", values.pop(1))
print("prefix =", list(values[:3]))
use paged_collections::paged_vec::PagedVec;

fn main() {
    let mut values: PagedVec<i32> = PagedVec::new();
    values.extend([1, 2, 3]);
    let first: *const i32 = &values[0];
    for value in 4..=10_000 {
        values.push(value);
    }
    println!("len = {}", values.len());
    println!("first = {}", values[0]);
    println!("last = {}", values[values.len() - 1]);
    println!("same address = {}", std::ptr::eq(first, &values[0]));
    println!("popped = {}", values.pop().unwrap());
    values.insert(1, 99);
    println!("removed = {}", values.remove(1));
    println!("prefix = {:?}", values.iter().take(3).collect::<Vec<_>>());
}

Python output:

len = 10000
first = 1
last = 10000
popped = 10000
removed = 99
prefix = [1, 2, 3]

The Rust example also prints same address = true. Element 0 kept its address through 10,000 pushes.

Address Stability

  • An element keeps its address while it stays at the same index, through any number of push, extend and reserve calls.
  • Operations that change which element sits at an index (insert, remove, swap, swap_remove, retain, dedup, drain, extract_if) move elements, as in a Vec.
  • split_off at a page boundary transfers whole pages without moving their elements.

Complexity

OperationCost
get, indexing, popO(1)
pushamortized O(1): one page allocation every PAGE pushes, rare directory growth
insert, removeO(len − index)
truncate, clearO(removed) drops; pages are kept
extract_ifO(visited), plus shifting the tail after a removal. No allocation.
split_offO(transferred pages) at a page boundary, else O(len − at)
binary_search, partition_pointO(log n), no allocation
sort and variantsthe slice sort, plus moving all elements out to a temporary buffer and back

The worst-case push costs one page allocation plus a copy of the page directory, which is PAGE times shorter than the elements. A Vec instead copies all elements on growth.

In Rust, extract_if(range, filter) visits that range in order and yields the elements for which the filter returns true. The filter can mutate each element. Dropping the iterator early keeps the unvisited elements in order. If the filter panics, the current element is kept with its mutations.

No Contiguous Slice

There is no as_slice or Deref<Target = [T]>. Use pages() and pages_mut() for per-page slices, or with_contiguous_mut to run a slice algorithm on all elements through a temporary buffer. Typed Python lists expose pages as NumPy views (page_view(i), page_views()) and copy everything with to_numpy().

Rust and Python

RustPython
push, extendappend, extend, +=
pop, insert, removepop([i]), insert(i, v), del l[i]; remove(value) searches by value
indexing, rangel[i], l[a:b] (slices copy)
extract_if(range, filter)
sort*sort(key=, reverse=) (typed lists: reverse= only)
pages()typed: page_view(i), page_views()
typed: get_many(indices), set_many(indices, values), to_numpy(), np.asarray(l)
reserve, shrink_to_fit, capacitysame names; capacity, allocated_pages are properties

API: PagedVec.