0
\$\begingroup\$

I'm currently working on a little ECS project and a couple doubts came to my head when dealing with systems.

Let's say I have a model component, a position component and a scale component. Each component has its own component array and each entity has a number of indices to access those components. Also, every component has a field for the entityID. The reason why I went with this design over using the entityID as the index to every component array was to both be more memory efficient and more time efficient when looping over the less populated component arrays.

Now lets say I'm designing my rendering system. This system needs to use the model, position and scale components, but these have different indices (because they are different components) so in order to get them the system goes through every element in the model component array, and for each component it uses the entityID to get handles for the other components it needs.

Now this works, but the issue that I'm having is that this completely invalidates the cache advantages that you would get by using a structure of arrays design, since you are now using the entity ID to index into another array. On the other hand, putting all of these fields into the same component would be better for optimizing the render system, but then something like the physics system that might only need the position would be wasting cache space on useless data like the model, the scale, etc. Are there any good solutions for this, or is this a loose loose scenario where one is forced to make tradeoffs?

\$\endgroup\$
2
  • \$\begingroup\$ You may be interested in How do you handle entity life-time and entities without all components in an ECS? My answer there explains how some ECS systems solve this through archetypes. Instead of having one array per component type, you divide your entities between archetypes defined by a specific subset of components. Each archetype has its own array of those component types, and can store components for its entities densely, at the same index for each of the entity's components. Iteration gaps happen only between archetypes. \$\endgroup\$
    – DMGregory
    Dec 17, 2023 at 3:00
  • \$\begingroup\$ I will be taking a look into archetypes and other ways to solve these issues. Thanks! \$\endgroup\$
    – Torino R6
    Dec 18, 2023 at 17:42

1 Answer 1

0
\$\begingroup\$

The advantage to the ECS model is the parallel direct indexing of data.

If the sparcity of the data elements, not usage, becomes to great, less than half on memory constrained systems, split the single ECS into 2 or more mutually exclusive ECS`s, and loop through each in applicable system code.

but then something like the physics system that might only need the position would be wasting cache space on useless data like the model, the scale, etc.

No, any data not needed by any particular system is not a waste, it is simply not accessed, and becomes stale in the cache and frees up more cache space for currently used data. (Not necessarily always true, but CPU-Memory caching techniques are quite complex and architecture specific, beyond the scope of this answer)

Expansion on the caching point: Yes misses will occur on the less frequented arrays, but the overall cache will suffer more by using indirect references (both the address value region and the pointed to data region must also be cached).

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for the insights, it really does help. Have a good one mate \$\endgroup\$
    – Torino R6
    Dec 18, 2023 at 17:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .