-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Entity Component System? #10
Comments
There is a third option, which I call half-baked ECS. The components can be grouped together in the same allocation unit. pub(crate) struct StructureComponents {
pub position: Option<Position>,
pub rotation: Option<Rotation>,
pub burner: Option<Burner>,
pub energy: Option<Energy>,
pub factory: Option<Factory>,
pub fluid_boxes: Vec<FluidBox>,
} It is not very good at utilizing memory, since it has a lot of wasted space in absent components, but it performs marginally better than dynamic polymorphism, probably due to less invocations of virtual functions. Also we can always reduce an empty component by putting in a Box or Vec. And we can optimize the fluid simulation even further by calculating connections only something has changed. And dynamic polymorphism is still slower after the optimization, probably due to the virtual function call: |
Now I implemented generational id so that each structure can hold references to each other that can be dereferenced in constant time. pub(crate) struct StructureId {
pub id: u32,
pub gen: u32,
} Now the performance with 400 pipes is unmeasurable. (We don't care too much about rendering time here because we won't render the whole map at once and ECS's goal is not to improve rendering performance, and we will migrate to WebGL or WebGPU if we really need rendering performance.) |
Now testing a lot of wire connections We optimize electricity grid calculation by using a cache data structure called PowerNetwork. Each PowerNetwork contains a list of power sources and sinks as StructureId lists, so the sinks can quickly query sources. pub(crate) struct PowerNetwork {
pub wires: Vec<PowerWire>,
pub sources: HashSet<StructureId>,
pub sinks: HashSet<StructureId>,
} Each color of the wires indicate distinct networks in the picture below. with dynamic polymorphism: |
ECS and Rust seems to get along well:
https://youtu.be/aKLntZcp27M
Why not implement it in our game?
I tested with 100 pipes and measured performance in ecs-specs-structure-slice
![image](https://user-images.githubusercontent.com/2798715/124491289-0e7d9c00-ddee-11eb-8123-43dccfb65d83.png)
and this is pre-ECS (Box dynamic polymorphism) :
![image](https://user-images.githubusercontent.com/2798715/124494393-bd6fa700-ddf1-11eb-9b8e-b3dd5f16893f.png)
The text was updated successfully, but these errors were encountered: