A memory-efficient collection library providing ordered maps and sets built on contiguous vectors.
SortedVecMap and SortedVecSet are alternatives to standard BTreeMap and BTreeSet designed for scenarios where memory footprint and cache locality are critical, and data is rarely modified after creation. You should always benchmark since the type of the keys and values may change the performance characteristics.
Instead of allocating separate nodes with pointers (as a tree does), these collections store all entries in a single contiguous Vec. Lookups use binary search, taking O(log N) time, while benefiting significantly from CPU cache hits due to contiguous memory layout.
O(log N) but highly cache-friendly, often outperforming tree-based collections for smaller to medium-sized datasets.Builder types when data can be inserted in sorted order or built all at once.O(N) time. If your workload involves frequent incremental updates, use BTreeMap or HashMap.O(N) cost of single insertions becomes prohibitive as the collection grows large.| Operation | SortedVecMap / SortedVecSet |
|---|---|
Lookup (get/contains) | O(log N) |
Insertion (insert) | O(N) |
Removal (remove) | O(N) |
Iteration (iter) | O(1) per step |
| Batch Build (Sorted) | O(N) |
| Batch Build (Unsorted) | O(N log N) |
use sorted_vec_map::SortedVecMap; // Efficient batch construction let map = SortedVecMap::from([ ("apple", 1), ("banana", 2), ("cherry", 3), ]); assert_eq!(map.get("banana"), Some(&2));
use sorted_vec_map::SortedVecSet; let mut set = SortedVecSet::new(); set.insert(1); set.insert(2); assert!(set.contains(&1));
PackedMap: If your key type is a dynamically sized type (DST) such as str or [u8], consider using PackedMap from the packed collection library. It packs all keys into a single contiguous byte buffer, eliminating the pointer/length overhead of individual keys and offering even better memory compression.