The aspect which bugs me here is that programming languages have no features to address this, preventing optimization which should be easy but isn't. So i'm curious what parctices people use.
Let's make an example of a particle system. A simple particle data structure could like like so:
struct Particle
{
vec3 position;
vec3 velocity;
vec3 color;
float age;
};
Then we implement our stuff, and it works. Could look like this:
Simulate (particles);
Render (particles);
And we see for example simulation takes longer than render, and we ask: Should we optimize our data for better access?
And we try something like this:
struct ParticlePhysicsComponents
{
vec3 position;
vec3 velocity;
};
struct ParticleOtherComponents
{
vec3 color;
float age;
};
We decided to keep age away from the most important data, because we access it only once, while we read pos and vel multiple times. So we want to optimize just for that. And it is faster so we are happy.
But it's ugly. We already have issues with naming things, it's harder to maintain, and in real life we might want to split into 3 or 4 parts, and we may want to profile a lot of options on data layout, and eventually make different decisions later.
This sucks, so mostly we never make it to this point, and keep the first inefficient but practical approach instead.
Or we try to solve it with some nice abstraction:
class Particles
{
struct A
{
vec3 position;
vec3 velocity;
} *a;
struct B
{
vec3 color;
float age;
} *b;
public:
vec3 GetPosition(int index) {return a[index].position;}
vec3 GetVelocity(int index) {return a[index].velocity;}
vec3 GetColor(int index) {return b[index].color;}
// ... add getters and setters for all our data
};
After that, we can change data layout in memory without breaking code which processes it.
So we can find the best compromise between SoA and AoS, but at the cost of some extra work and complexity.
Now i wonder is there a better way to handle this (speaking of C++), and do we have a name for exactly this problem?
Is this also something ECS tries to handle? (I don't think so, as my example is meant about some very low level - only about hardware issues, but not related to any ‘software design and architecture’.)
And finally i hope i'm on topic at all here. ; )