fn main() {
let vec0 = vec![0; 10];
let mut vec1 = vec![];
for _ in 0..10 {
vec1.push(0);
}
assert_eq!(vec0.len(), vec1.len());
}
In this example, vec0
and vec1
are identical when accessing their items with index. But do these 2 approaches of initializing make different memory layout?
More background: I'm building a (hopefully) cache friendly container (Vec<T>
so far) to exploit cache locality. Usually in C++ you can just allocate a new array with dynamic length (auto array = new DataType[length];
) to enforce compact memory layout, yet variant length array is simply impossible in Rust. So I'm looking for a way to build Vec<T>
that could improve cache hit/miss ratio during execution.