Expand description
Storing vectors within vectors is convenient but means that each
stored vector will allocate on the heap and drop when removed. SlicedVec
stores constant-length segments within a single vector so that push
within the storage capacity will not allocate and truncate
will not
deallocate from the heap. Benchmarks indicate that this strategy is not
always faster for repeated cycles of push
and swap_remove
. This is
likely because the overhead of swapping a larger number of elements. Vec
within Vec
only has to swap the pointers of the stored Vec
objects
whereas SlicedVec
has to swap an entire segment of values. In a few cases,
SlicedVec
has proven about twice as fast, but you will need to test your
cases. SlicedVec
is nonetheless convenient for organizing segmented storage,
such as a collection of image rows, and so on.
Example
use rand::{rngs::SmallRng, Rng, SeedableRng};
use slicedvec::SlicedVec;
let mut rng = SmallRng::from_entropy();
let mut x1 = SlicedVec::with_capacity(1000, 20);
x1.push_vec(
std::iter::repeat_with(|| rng.gen())
.take(20 * 1000)
.collect::<Vec<_>>(),
);
let x1_insert: Vec<Vec<usize>> =
std::iter::repeat_with(|| std::iter::repeat_with(|| rng.gen()).take(20).collect())
.take(500)
.collect();
for i in 0..500 { x1.swap_truncate(i) }
for i in 0..500 { x1.push(&x1_insert[i]) }
Macros
- Contruct a
SlicedVec
from a list of arrays
Structs
- A segmented vector for iterating over slices of constant length.