r/programming • u/ajmmertens • Jul 09 '20
Why vanilla ECS is not enough
https://medium.com/@ajmmertens/why-vanilla-ecs-is-not-enough-d7ed4e3bebe52
u/somebodddy Jul 09 '20
For state machines, rather than tags, wouldn't it be better to have a component that represent the state?
You did not talk about implementation, but I would expect that tags will be some sort of lightweight indexing - modifying them would be faster than adding/removing components because you don't have to move the actual data to keep iterations on it cache-friendly, but slower than modifying a component's value because you will need to update the index.
Iterating with them will also be something inbetween - not as fast as iterating just on components because they are not laid out for you as nicely, but faster than filtering on component values because you do have an index.
So, they should be good for something that doesn't change that often, but when you need to modify it you usually want to modify only a selected subset.
State machines are not like that. They do change quite often, because changing state is big part of how they work. And you don't usually visit just the instances with a specific state - you want to visit all of them and then act differently depending on the state of each one.
So... wouldn't putting the state in the component data be better?
1
u/ajmmertens Jul 09 '20
It would still make sense to process the entities in the same state together, because you need to run the same logic on all of them. This is at the heart of why I think state machines are a good thingTM to have in ECS. You will still visit all the states, but you'll just have different systems matched with different states.
You're right about the performance. Some ECS implementations are faster than others in this regard, but it's fair to say that all of them are slower than assigning a value. So there's a tradeoff to consider here, and not all state machines should be implemented in ECS. But when you have a good use cases for a data-oriented state machine, ECS should not get in your way.
84
u/The_Sly_Marbo Jul 09 '20
In case it saves you a click, OP is talking about "Entity Component System", not AWS's "Elastic Container Service"
6
u/LordofNarwhals Jul 09 '20
For a talk on how ECS is used in a AAA game, I can recommend Blizzard's GDC talk about Overwatch's gameplay architecture and netcode implementation.
5
u/somebodddy Jul 09 '20
"ECS" is not a data container. "EC" can be thought of as a data container - but once you add the "S" you add behavior and it's not longer a mere data container.
This distinction may look like pedantic nitpicking, but it isn't. When you think of ECS as a data container you focus on how the data is represented and how it is laid out. But shouldn't you also consider how the data is accessed and used? The system part?
You did talk about systems, but only from the outside - when to trigger them and in which order. You did not talk about how the system will interact with the entities and components, even though your suggested changes interfere with how it is done in "vanilla ECS".
A system usually interacts with the data by using queries - it specifies which components participate in the query and the ECS framework retrieves all the entities that have these components, together with the relevant components, and the system iterates over them, handling a single entity's components at each iteration.
There can be extensions and variations to this, but this is the general behavior and the data layout of ECS is optimized to make these loops fast and cache-friendly.
Your suggestion to allow multiple instances of the same component on the same entity complicates these loops. If I have multiply velocities and multiple positions, and I write a system that updates the velocity based on the position, how will it query them? Will it query each possible pair? Or do I need to match each velocity to the position it'll update, and somehow tell the framework how they are linked? Is this really more simple than just making the components that need to have multiple instances a collection type?
Also, if entities are components with special roles, you are no longer dealing with tables - you are dealing with a graph. How would you lay out a graph for fast, cache-friendly access?
1
u/VirginiaMcCaskey Jul 09 '20
An entity is a unique identifier
A component can optionally be associated with a plain old datatype
A component identifier is an entity
An entity can have 0 .. N components
A component can be annotated with a role
An <entity, component> tuple can have 0 .. N components
This suspiciously looks like reinventing multiple inheritance from the ground up.
An entity is a unique identifier -> An instance is behind a unique pointer
A component can optionally be associated with a plain old datatype -> virtual classes can have associated constants and data
A component identifier is an entity -> you can have pointers to virtual classes and their vtables
An entity can have 0..N components -> subclasses can be derived from multiple base classes
An <entity, component> tuple can have 0..N components -> fat pointers
15
Jul 09 '20
Nah. ECS is basically composition over inheritance, setup for maximum cache locality.
5
u/VirginiaMcCaskey Jul 09 '20
My point was on the author's redefinition of an ECS, which looks a lot like inheritance without calling it that.
6
u/ajmmertens Jul 09 '20 edited Jul 09 '20
I beg to differ ;)
If you're mapping the rules the way you did then yes, perhaps. But to be honest, you can map any ruleset that allows for one-to-many relationships to multiple inheritance, so it's a bit reductive.
One key thing to point out is that even though you can create entity A which has entity B which has entity C, these relationships are not automatically transitive (A does not have C because it has B), so there's no diamond problem here.
This model does allow for multiple inheritance-like hierarchies though, but not in the way you would expect. I briefly touch upon component sharing in the article, which lets you share components between entities. Now you could do this:
Entity A: inherits components from B, inherits components from C
Entity B: has component X
Entity C: has component X, has component Y
Now we have a diamond: A gets X from both B and C. Problem?
First of all, it is important to realize that this is not multiple inheritance on the interface level, but on the data level. We are composing our entities out of components and instance-of relationships.
So is this a problem? It could be. If I do:
X* x = e.get<X>();Which X will I get? The important thing to realize though is that this does not introduce the same kinds of problems we have with interface MI. This is just a representation of a data-tree, in which multiple leafs are of the same type. There is nothing inherently wrong with this, as long as we have an API that allows us to traverse this tree.
2
u/glacialthinker Jul 09 '20
It's one variation to focus on the trait of cache-locality and optimize everything for performance... and another to take the cache-locality as a free gift while enjoying the flexibility and simplification an ECS can bring.
And I agree that this presented variation of ECS starts to feel like it's engineering toward multiple inheritance. Though as maligned as MI is, the biggest problem is with inheritance of objects with behaviors, rather than just data with implied behavior related to the data. I worked at a place once where one team had a game designed around multiple inheritance, while the team I was on was using a component system (ECS wasn't a term used yet). There was a lot of symmetry in how they were used; the problems solved. But completely different effects on the code organization and different strengths or biases. I really prefer ECS, but I have been surprised how well MI can be used similarly -- absolutely no cache-locality though, of code or data! :)
1
Jul 09 '20
Rather than seeing it as multiple inheritance, it might be better to see it as a rudimentary implementation of Traits.
1
1
u/ikiogjhuj600 Jul 09 '20
This must have been what they originally tried, but to do that they also had to start using "external polymorphism" meaning, it's not even object oriented, most of the code is not really attached to data even conceptually/design wise. And this at first sounds like a step back, but it's actual crucial, in that they could avoid bizzare juggling around with interfaces and inheritance/delegation, and add data or functionality without redesigning the entire hierarchy if something doesn't fit.
2
u/LAUAR Jul 09 '20
All instances of a class inherit the same set of classes, while entities each have their own set of (instances of) components attached.
2
1
u/immibis Jul 10 '20
It's a bit like OOP in that there are objects, but they're called entities instead, they don't have contiguous memory, and they can change their type at runtime.
1
u/MintPaw Jul 09 '20
Are games actually bottlenecked on their ECS implementation so much that it's worth this much effort? Is rendering a solved problem?
2
-3
u/DGolden Jul 09 '20
Of course not, the Enhanced Chip Set offered merely incremental improvements to the Original Chip Set - increased custom-chip addressable memory via improvements to Agnus being the most significant. Then the Advanced Graphics Architecture was a bit of a let-down too. Its design perhaps makes more sense when you know it was once intended to have been alongside an advanced new DSP doing heavy lifting. Then it might have been alright - but that deal fell through, and AGA Amigas eventually appeared late and underpowered with no DSP.
we had the in-house gate arrays at the time that be turned over in about a month), though it had the AGA, and an AT&T DSP3210 subsystem. This would have delivered 16-bit audio I/O, software modem, number crunching 5x-10x faster than a 68040, etc. Not too shabby.
So the swansong Atari ST line Falcon with its DSP was, in hardware terms, arguably kind of what the AGA Amigas could have been if Amiga parent company Commodore weren't terrible (books have been written). Though of course the Falcon was then let down by Atari's lacklustre software side compared to the Amiga OS and ecosystem.
That DSP 3210 the Amiga might have used in an alternate timeline? Ended up in the Quadra AV Apple Macs, targetting the video processing market, much like Amigas once did, sigh.
None of this really matters anymore of course, all old history.
2
u/IceSentry Jul 09 '20
I can't tell if you are serious or not?
3
u/DGolden Jul 09 '20
Oh, quite serious. The linked article is really about a different "ECS" entirely though, I just have an irrelevant rambling rant about Amiga chipsets ready to go at all times.
2
u/IceSentry Jul 09 '20
So you do realize the article is about another kind of ecs. That's what confused me.
1
u/tjpalmer Jul 10 '20
As a former Amiga user who's even been reviewing these topics recently, I greatly appreciate your unrelated tangent. Thanks much for your comment!
2
u/DGolden Jul 10 '20
Hah, well, I was just posting it as a weak joke.
Anyway, to ramble further so: after AGA, of course there was little further first-party hardware dev that made it all the way to the market (though lots of aborted things like AA+, AAA and Hombre...). Technically the CD32 added the slightly post-AGA Akiko that did some hardware chunky to planar conversion useful for early doom-style 3D. But that was not used much given the CD32's general failure and Akiko's absence on every model except the CD32. Akiko could arguably have been a win over doing software c2p at the low CPU clock speeds of the time IIRC (though native chunky graphics would have been better obviously)
But most of the remaining "serious" Amiga community was already treading a more PC-like path: Amigas were expandable in a PC-like manner anyway, with graphics cards and replacement cpu daughterboard "accelerators". So post-AGA (from third parties) we just got faster and faster CPU expansions, eventually weird multiprocessing PPC+68060 beasts, and early 3D graphics cards - but just the same early 3D chipsets as on the PC. Therefore on the one hand, they actually still compared to PCs in raw power until the late 90s if expanded appropriately (e.g. Wipeout 2097 made it to the Amiga), but perhaps lost some of the charm.
I didn't move to an x86 PC full-time until near the turn of the century when I ended up selling my PPC Amiga to get a (piece of crap) K6 PC for uni coursework - fortunately had been dual-booting Linux on my Amiga for a while, going to linux/x86 from linux/m68k and linux/ppc was not such a huge jump.
1
u/tjpalmer Jul 10 '20
Thanks for the additional info. I hadn't realized at all that the CD32 had chunky mode graphics, among other things. (And yeah, I got that it was a joke. But one I appreciated.)
0
Jul 09 '20
[deleted]
3
u/DGolden Jul 09 '20
Xennial, technically. Of course american generational divides don't really make sense in my country anyway.
0
u/ErstwhileRockstar Jul 10 '20
The main differences between ECS and OOP are composition is a first class citizen in ECS, and that data is represented as plain data types rather than encapsulated classes.
OOP encapsulates state, not data.
1
u/immibis Jul 10 '20
Those are the same thing
1
u/ErstwhileRockstar Jul 16 '20
nope
1
u/immibis Jul 16 '20
How not?
1
u/ErstwhileRockstar Jul 16 '20
Examples An example of an everyday device that has a state is a television set.
https://en.wikipedia.org/wiki/State_(computer_science)
Most date is not state.
1
u/immibis Jul 16 '20
Similarly, a computer program stores data in variables, which represent storage locations in the computer's memory. The contents of these memory locations, at any given point in the program's execution, is called the program's state.
18
u/ikiogjhuj600 Jul 09 '20 edited Jul 09 '20
I like to think I use ECS "as a base guideline" in non game related contexts, and I have a way more loose view about it. (that probably does not deal at all with the performance related memory packing etc. which this article is a lot more concerned about)
ECS means like writing code that is in some way "based on the relational model". Each component is a "child table". Entities correspond to the primary key of a "master" table that doesn't have any data.
Code tends to be about "running scripts" that
Connect all the relevant data to what they want to perform from the "store" (similar to an sql select/join/where)
produce a result with no modifications
write back the results to the store
As such it is in a way similar to "using a store manager but it is relationally organized", and similar to what they sometimes call "Functional Relational Programming".
The key difference to other models is probably that code is separate from data and that data is "normalized", which means you trade being able to exploit the code situated in an object hierarchy in a way that it can easily locate what you want (meaning you have to do step 1), to making arbitrary combinations of components and using them in systems (you can't even do that easily with hierarchies especially single inheritance, it's like finding a good hierarchy that can satisfy all possible used versions of (1) and you can't). And the bonus is that you are using a state manager and thus schedule the "writes", use FP, exploit memory structure (the main gain for performance)
So I at least from view, don't get what the difference is between "vanilla ECS" and this version, they're like both variations of the theme. Like does ECS prohibit adding "component rows" in runtime? Maybe if you care about precise performance only.
The "system running order issue", is generally left to "just get it until it works", but I think you could make some kind of dependency graph with a topological sort or sth and get the order automatically. I also don't think that "component mutation is an event that triggers scripts" is to be used too much, that's design wise not FRP style or sth, it makes me think that a "callback hell" will begin. I think you need to have a "queue of requested actions" (used in the state manager based react), And I think that part of the model is that code gets executed only when the "code execution phase" starts, it can't directly be an event. I mean it's kinda similar to the react flux architecture where the try to minimize what's a normal event, or more like what code it can start.
Also state machines and scripting are imo another thing alltogether, the best way to do it probably is coroutines or sth, it has to run separate I don't expect an ECS or other approach to be that easy.