I've been reading up about entity component systems as a design pattern for an OpenGL engine. The style I'm trying to implement has entities only being integers, and components being long contiguous arrays of, for example glm::vec3
for a world-position component.
The entity integers then work as indices into those arrays. All of this described best by this post.
As I understand, the main benefits of an ECS would include:
- Simplification and elimination of unnecessary dependencies and spaghetti code
- Performance boost: contiguous component arrays translate into fewer cache misses and an overall de-bloating of complex inheritance
While I do agree with the first point, I don't understand how the second could be true.
On a regular draw call, you'd need almost always the majority of components. You need position, scale, rotate for loading a MVP matrix into the shaders. You need the mesh and textures for rendering itself. You even need details like light color or whether or not it casts shadows. Everything! This means that you will inevitably encounter cache misses in some form, since you can't fit your component arrays in cache.
Add to that the fact that since the entity integers are indices into the component arrays, all of the component arrays need to grow in size whenever you add an entity - independent of it's type. This means that adding 30 entities with no mesh will inevitable generate 30 empty mesh allocations that will never be used.
How would you implement such a system to truly minimize cache misses without incurring in unnecessary memory waste?