Skip to content

Extending some number of Vecs #721

@dragostis

Description

@dragostis

In order to gain a performance boost from auto-vectorization, I'm storing data in multiple Vecs. 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions