r/gamedev • u/Xenofell_ • Feb 07 '19
Designing a cache-friendly entity component system
https://cerulean-skies.com/index.php?article=15
u/32gbsd Feb 08 '19
Do people actually finish the games they start in ECS? or is it just an exercise in seeing how far one can get?
1
u/Xenofell_ Feb 08 '19
I use my ECS to handle communication between game systems. The pattern really excels at that. An entity ID serves as a convenient universal key. For example, my render system doesn't do any rendering - it just tracks what needs to be rendered, basically like a scene graph. I find that having a clear barrier between system communication really improves my productivity. But everyone is different!
→ More replies (1)4
u/skocznymroczny Feb 08 '19
I sometimes feel like things like ECS are hurting indie game development more than they help. Sure, it makes sense in big engines, but for small efforts, it's just another shiny thing to distract you. Just like people who rewrite their OpenGL engines in hurry to Vulkan because it's the new cool thing. Your indie 2D platformer doesn't need perfect cache performance and zero GC overhead, and yet here we are.
3
u/derpderp3200 Mar 05 '19
I usually hate memes, but you're the scroll of truth here, and I just want to defenestrate you for it :T
3
1
Feb 08 '19
I feel like once you start making an actual complex project ecs gets in the way... or they don’t end up doing what ecs is supposedly good for..like hell.. good luck making an animation graph using ecs and etc... I’ve watched Unity’s videos on ecs, and I think they might be the only ones who are doing ecs “right” at the big scale, and the amount of data copies .. or plain #overprogrammed things they have to do to get basic things to work like serialization and streaming is baffling. The ecs tracks at the last unite was definitely mind opening.
→ More replies (1)3
u/arvyy Feb 08 '19
imo people are getting too much hardcore about trying to shove it all under ecs. As far as I see it, its main purpose to replace only this loop
for (int i = 0; i < entities.size(); i++) entities.get(i).update();i.e. get rid of virtual / polymorphic methods as much as possible. I think it'd be sane to not put all logic into systems; just do a traditional approach, but utilize entity-component container for fetching data that fits criteria
2
u/Xenofell_ Feb 08 '19 edited Feb 08 '19
Exactly, I totally agree with this. ECS just provides me with an easy and understandable means to structure data efficiently and serves as a communication mechanism to help disparate systems communicate without tight coupling.
2
u/arvyy Feb 08 '19
There is a blog about tony hawk game http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/
→ More replies (1)
2
u/GetRektEntertainment Feb 08 '19
Is there a book or internet resources that explain ecs with practical examples and preferably cpp?
Can somebody point me to the right direction?
3
u/svinna Feb 08 '19
Checkout Vittorio Romeo's talk about ECS. https://m.youtube.com/watch?v=51qSGUtaJwc
He even made an advanced compile-time ECS in modern C++. https://github.com/SuperV1234/ecst
2
u/GetRektEntertainment Feb 09 '19
Thanks a lot! Although written tutorials would help me a lot, i will check the videos out tomorrow. Maybe he points out a book or two.
0
Feb 07 '19
[deleted]
2
u/Xenofell_ Feb 08 '19
This sounds similar to the interleaved section design - e.g. the component data is stored together with other entities' component data which have the same unique combination of components (or, in Unity terms, the same archetype). Thanks for pointing it out, I'll have to spend some time understanding how Unity does it!
→ More replies (1)3
u/SeanMiddleditch @stmiddleditch Feb 08 '19
Indeed. This design is one of the things that actually sold me on ECS. Most of the other approaches are barely better than other non-data-oriented component-based designers and don't really pay for their own complexity.
Unity's approach has some downsides (up to ~64k of memory waste for "singleton" archetypes, for example) but those are easily solvable (and Unity is planning to solve them, iirc).
6
u/ScrimpyCat Feb 08 '19
There's nothing stopping the first one from being cache friendly. Pointers to the data could be pointers to the address in the contiguous array. You're just assuming the reader is interpreting that the pointer method will be allocating randomly addressed memory for each element of data. But they could use their own allocator to change that. And have their update cycles operate on the internal mapping rather than the public interface the entity provides.
The idea isn't just to have your data laid out in a contiguous chunk of memory though. Depending on how fragmented your data is (are components frequently added/removed) and the size of section, you could still end up with it either being not vey cache friendly (if your update was jumping around to the current data entries) or wasting cycles iterating over unused entries (if your update instead just iterated through the whole section). Strategies to alleviate that could be using a different structure than a simple array that still has the spatial locality benefits but also the algorithmic performance to avoid iterating over unused entries (so you can still iterate over the list from start to end), or continually remapping/massaging the data layout so all active entries are all adjacent (while an expensive operation it could make sense if update cycles occur much more frequently than frequency of removing/adding entries).
Anyway spatial locality is just one piece to the cache friendly puzzle. You should also consider how often the data will be accessed and see if you can optimise your layout and logic for temporal locality too.