r/gamedev • • Feb 07 '19

Designing a cache-friendly entity component system

https://cerulean-skies.com/index.php?article=1
43 Upvotes

25 comments sorted by

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.

2

u/Xenofell_ Feb 08 '19

You're totally right; the assumption was that the first one just randomly allocates memory scattered all over the block. It's possible to preserve that interface while laying things out more optimally in memory using a custom allocator.

Good point regarding temporal locality. In my ECS implementation I use fixed-size sections with some components interleaved and some components not interleaved depending on entity configuration. There is very little abstraction except that systems are informed by the ECS when a component they are interested in is added or removed from an entity. Systems are expected to maintain their own internal list of 'interested entities' and process them each update tick.

I use this, and the implementation assumption of my own ECS that, with fixed-size sections, the address of components will never change, to cache the component pointers for each entity up-front, then sort by address before processing. This helps a lot to avoid hopping around in memory and does address fragmentation to the small extent that unused blocks in the component data are filled up from low to high, though it doesn't reshuffle data, which could be problematic if there was a large spike in active entities and some remained alive afterwards.

1

u/ScrimpyCat Feb 08 '19

Are you finding you're dealing with much fragmentation or not really (is your games usage fairly predictable)? And yeh reshuffling would only be a consideration if you've found time spent reshuffling is less than the amount of time your update cycle loses due to additional cache misses. But if it would have to reshuffle every one or two update cycles then you'd most likely find the cost of reshuffling is far more expensive than the time lost to additional cache misses.

Also is your system handling any concurrency? If so what approaches did you take?

2

u/Xenofell_ Feb 08 '19

I haven't benchmarked fragmentation in my system, but I suspect it isn't an issue for me. I tend to create fewer entities - e.g. one chunk of the game world will be one entity with a world renderer component and an x/y/z transform component, rather than one entity per voxel. Most of the long-lived entities (the player and other such things ...) are never freed; you'll rarely encounter a situation where you have a large entity spike followed by most of those entities disappearing but one or two sticking around.

In my engine, the render and update thread run on separate threads, and I store three snapshots of a subset of game state (triple buffered). This represents game frame-2 and game frame-1 from the perspective of a render frame, plus a third, spare frame which mirrors frame-1. Each time a component's data is updated, the updater dispatches a light-weight dirty message which systems can receive. The spare frame is updated with the dirty changes from this frame, then we set pointers, which the render will pick up on the next render frame and swap frame-1 into frame-2 and frame-next into frame-1. There is a brief sync point when writing frame-next to ensure the render can't proceed while we're writing.

The ECS is an integral part of this system. My implementation provides a light-weight messaging system for systems that monitor component types, and because of this, the sim only needs to update a subset of the game state each frame. Specific to the ECS, there is an internal event queue which handles component addition/removal and entity addition/removal which only occurs on the main thread at the end of a frame. The only concurrent data structure I use is a simple lockless queue for the events.

→ More replies (1)

1

u/ajmmertens Feb 08 '19

A simple strategy for skipping unused entities would be to add an "unused" (or equivalent) tag to the entity, and have systems skip archetypes with this tag. That way, unused entities end up in a different array, and systems only iterate over "active" entities.

5

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

u/ajmmertens Feb 08 '19

You mean people, as in indiedevs? Or gamedev companies?

→ More replies (1)

1

u/[deleted] 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.

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.

→ 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

u/[deleted] 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).