Thoughts on ECS
So I’ve been looking into adapting Unity’s new ECS system.
I like the idea and concept behind it. I like the fact that instead of optimizing based on a clever algorithm, ECS focuses on utilizing the hardware features.
It reminds me of something Jon Blow said in one of his streams, “90% percent of the programmers out there have no idea how a computer works”. when hearing about how Casey optimized a hashing algorithm by utilizing a better CPU instruction and achieved a significant amount of speedup, he said if Casey can easily find an opportunity like that, I’m sure there are 10 other areas that can easily be improved if people just understand how a computer functions. So ECS means separation of Entity, Component, and System. An Entity is nothing but an identifier. It’s not a container by any means(Mike Acton said that in his unite talk). A Component holds data. I mean like pure data. A System contains the logic that process component data.
And the efficiency and speedup come from the architecture simply utilizing the CPU cache properly. When a system looks up components to process, the data will come in as a batch of sequential data(stream). Random access through the memory is minimized, hence the speed up.
I’m still trying to have a better understanding of the overall picture of adapting ECS in projects. Conceptually it makes sense and the performance results are very appealing. But I still have confused and trying to figure out the standard way to communicate in between systems. for example, a follower system. let’s say we have a simple follower AI behavior to implement. we put follower data on the AI (FollowComponent). How should this component hold a reference to its target? In other words, how should the FollowSystem determine the target to follow for a follower? I have some rough ideas: Have a FollowTargetComponent on the target to follow. and look up entities that have FollowTargetComponent in FollowSystem. But then how do we match the follower with the follow target if there are multiple targets?
Also, a lot of the Unity built-in systems are currently not ECS compatible. which makes it harder to start using ECS in real projects. however, I feel it opens up more opportunities to implement your own systems and opt-out of built-in unity features. I remember when I was working on a simple mobile game. It was kind of an underwater infinite scrolling game where a starfish tries to eat as many pearls as possible. I felt like the built-in physics system was overkill for the simple collision detection requirements that the game had. So I decided to implement simple rect bounds checking to handle collision between starfish and pearl. The result, however, was not very good. It ran slower than using colliders. Now I think of it, I was iterating all the objects in the main thread and performing the bounds check calculation one by one. This can now be easily done in the ECS way and improved with the job system.
Originally published at https://dylanyasen.github.io on January 2, 2019.