๐ฆ pagedvector (early experimental crate)
A lightweight Rust crate implementing a paged virtual vector optimized for sparse data.
It mimics how operating systems manage memory: instead of allocating a full vector, it only allocates memory for portions ("pages") that actually contain non-default values.
๐ Features
- ๐พ Memory-efficient for sparse data
- ๐ Fixed-size paging system
- โก Lazy allocation (allocate on write)
- ๐งน Automatic deallocation when using
set() - ๐ Familiar API (
get,set, indexing) - ๐ฆ Serialization support (
serde,bincode)
๐ง Concept
Instead of storing a full vector of size N, PagedVec:
- Splits the vector into equal-sized pages
- Stores pages as
Option<Page<T>> - Allocates a page only when needed
- Deallocates when all values return to default
๐ Example
use PagedVec;
๐ API
Create
let vec = new;
Access
vec.get;
vec.set;
vec;
Modify
vec.set;
Inspect
vec.len;
vec.is_default;
vec.number_pages_total;
vec.number_pages_alloc;
โ ๏ธ Notes
- Requires
T: Clone + PartialEq - Uses
assert!for bounds (panics if invalid index) - Performance depends on
page_size
๐ก Use Cases
- Sparse arrays
- Simulations
- Game worlds
- Scientific data
- Memory-constrained environments
RoadMap
Implement more methods to get Vec<T>-like feel:
-
is_empty()
-
page_size()
-
capacity_pages()
-
clear()
-
fill(value)
-
contains(&value)
-
get(index) -> Option<&T>
-
get_mut(index) -> Option<&mut T>
-
v[i] // panics
-
v.get(i) // returns Option
-
iter()
-
iter_mut()
-
enumerate_non_default()
-
count_non_default()
-
allocated_pages()
-
deallocate_empty_pages()
-
resize(new_len, default)
-
truncate(new_len)
-
push(value)
-
pop()
-
extend(iter)
-
Default
-
From<Vec>
-
FromIterator
-
Extend
-
IntoIterator
-
is_allocated(index) -> bool
-
is_page_allocated(page_index) -> bool
-
page_index(index) -> usize
-
page_offset(index) -> usize
-
allocated_page_indices()
-
non_default_len()
-
sparsity()
-
memory_len_allocated()
-
allocated_fraction()
-
default_fraction()
-
to_vec()
-
into_vec()
-
from_vec(vec, default, psize)
-
shrink_pages()
-
recount_page(page_index)
-
recount_all_pages()
-
deallocate_default_pages()
๐ฆ Dependencies
= { = "1", = ["derive"] }
= "2"
๐ License
MIT License (see LICENSE file)