-
Notifications
You must be signed in to change notification settings - Fork 546
Description
In order to gain a performance boost from auto-vectorization, I'm storing data in multiple Vec
s. Think struct of arrays instead of array of structs. In the array of structs case, it's very easy to simply extend the vector with whatever the parallel iterator provides. However, in the other case I don't have this luxury.
I came up with two solutions:
vec1.reserve(...);
...
vecN.reserve(...);
/// initialize Vec with Default data; but this is visibly expensive in my benchmarks
vec1.par_iter_mut().zip(...).map(|(val, ...)| *val = ...)
Or use uninitialized memory:
let vec1: Vec<MaybeUninit<...>> = ...;
...
let vecN: Vec<MaybeUninit<...>> = ...;
vec1.reserve(...);
...
vecN.reserve(...);
vec1.par_iter_mut().zip(...).map(|(val, ...)| unsafe { ... })
The problem I have with the second solution is that it's very painful to use and it litters all my code with a lot of unsafe. However, I don't see any solution to remove it since initializing the vector with default values is visibly costly. Not zipping the iterators together also doesn't work because it will have to iterate through the data multiple times.
Any ideas on how to improve this?