The SoA Vector – Part 2: Implementation in C++--Sidney Congard

The series continues and end.

The SoA Vector – Part 2: Implementation in C++

by Sidney Congard

From the article:

Like we saw in the first part of this series on SoA, the SoA is a way to organise the data of a collection of objects to optimise the performance of certain use cases: traversing the collection by accessing the same data member of all the objects:

struct person {
   std::string name;
   int age;
};

std::vector<person> persons = ...

for (auto& person : persons)
{
   ++person.age;
}
struct person {
   std::string name;
   int age;
};

std::vector<person> persons = ...

for (auto& person : persons)
{
   ++person.age;
}

The SoA in its barest expression is this:

struct persons {
    std::vector<std::string> names;
    std::vector<int> ages;
};
struct persons {
    std::vector<std::string> names;
    std::vector<int> ages;
};

By putting all the ages next to each other in memory, we optimise the performance of the traversal. But such a structure is not a container in itself, and is in particular not compatible with the STL.

Let’s design an SoA collection with an interface as close as possible to std::vector<persons>, but with the SoA structure of components stored in separate arrays...

Add a Comment

Comments are closed.

Comments (1)

0 0

Paolo said on Jan 4, 2019 03:28 AM:

Nice implementation. A similar solution is implemented and explained here https://github.com/crosetto/SoAvsAoS , which has zero overhead wrt a hardcoded SoA layout on most compilers.