alecthomas / entityx

EntityX - A fast, type-safe C++ Entity-Component system
MIT License
2.23k stars 295 forks source link

Are memory requirements O(entities * component types)? #201

Open newobj opened 7 years ago

newobj commented 7 years ago

Reading the code, it seems to me like components share the index of their Entity, e.g. the Position Component for Entity #1,000,000 will the 1,000,000th Position Component in the Position Component Pool.

Am I understanding this correctly? If I have 1,000,000 entities, and there is one instance of the Foobar component, but it happens to be on entity 999,999, then we'll have to eat the memory of unused 999,998 unused components in the Foobar pool?

Am I reading the code correctly? If I'm wrong, can you help me understand how?

I'm mainly looking at EntityManager::assign to make this determination, specifically in the placement new:

::new(pool->get(id.index())) C(std::forward<Args>(args) ...);

where pool is the component pool, and id is the entity id.

alecthomas commented 7 years ago

With the default storage implementation, that is correct, yes.

newobj commented 7 years ago

Thanks Alec. So, taking this a bit further, am I correct in understanding that iterating (EM::each) over a particular component type is linear in the number of entities, not in the number of component type instances?

I'm curious whether this strategy was the simplest thing that worked generally, and/or if you put a lot of effort into other approaches that didn't pan out? I'm writing my own ECS framework as a learning exercise and trying to keep scale of component instances separate from entity instances. The exercise is a bit silly since EntityX is so awesome, but never the less... :P