I am currently learning more about the ECS pattern and have been attempting to create my own implementation as practice. I decided I want to make it more cache friendly when looping through the components by packing all my different components into vectors, instead of having a vector of pointers.

[A reference I've been reading](http://gameprogrammingpatterns.com/data-locality.html) suggests to put each different component type into a different array and to loop over it, like so:

```
AIComponent aiComponents[MAX_NUM];
PhysicsComponent physicsComponents[MAX_NUM];
RenderComponent renderComponents[MAX_NUM];

while (!gameOver)
{
  // Process AI.
  for (int i = 0; i < numEntities; i++)
  {
    aiComponents[i].update();
  }

  // Update physics.
  for (int i = 0; i < numEntities; i++)
  {
    physicsComponents[i].update();
  }

  // Draw to screen.
  for (int i = 0; i < numEntities; i++)
  {
    renderComponents[i].render();
  }

  // Other game loop machinery for timing...
}

```

I find this very limiting as it will require that everytime I create a new component that I will have to hand-create the array and the array loop.

I would much prefer a single field like so:

```
// A vector of pointers to other vectors of different types.
// For example, componentPool[0] could be RenderComponent and then
// componentPool[1] could be PhysicsComponent

vector< vector<AnyConcreteComponentType>* > componentPool;

for (int i = 0; i < componentPool.size(); i++)
{
    for (auto& component : componentPool[i]) {
        Update(component);
    }
}

```

This would allow me to keep adding new systems dynamically in my init(), like so:

```
AddComponent(entityId, RenderComponent());

```

Which will automatically expand the componentPool to add a new RenderComponent slot, which will then point to the newly created vector and which I could then iterate over efficiently.

Problem is that I don't know how you would do this, or even do it optimally. I'd imagine there would have to be templates, casting and a way to know, before accessing the vector, what type I require but other than that I have no clue.