Coder Social home page Coder Social logo

Entity Component System? about factorishwasm HOT 7 OPEN

msakuta avatar msakuta commented on June 24, 2024
Entity Component System?

from factorishwasm.

Comments (7)

msakuta avatar msakuta commented on June 24, 2024

I increased number of pipes to 400 to measure differences more accurately.

ECS:
image

pre-ECS (dynamic polymorphism):
image

Clearly both of them scale poorly, but pre-ECS is an order of magnitude faster.

from factorishwasm.

msakuta avatar msakuta commented on June 24, 2024

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.

image

And we can optimize the fluid simulation even further by calculating connections only something has changed.

image

And dynamic polymorphism is still slower after the optimization, probably due to the virtual function call:

image

from factorishwasm.

msakuta avatar msakuta commented on June 24, 2024

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.)

image

from factorishwasm.

msakuta avatar msakuta commented on June 24, 2024

Now I test with 400 chests and inserters that move items in circle!

image

With dynamic polymorphism without generational id optimization:

image

With generational id optimization:

image

So much improvement!

In fact, the most effective optimization is generational id, rather than ECS.

from factorishwasm.

msakuta avatar msakuta commented on June 24, 2024

Now testing a lot of wire connections

image

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.

image

with dynamic polymorphism:

image

from factorishwasm.

msakuta avatar msakuta commented on June 24, 2024

I ported 400 chests and inserters test with ECS + generational id, and it shows pretty similar performance to dynamic polymorphism.

image

from factorishwasm.

msakuta avatar msakuta commented on June 24, 2024

And finally, 400 assemblers and electric wires on half-baked ECS + generational id

image

In conclusion, generational ids improves so much that we can't miss it, and half-baked ECS can further improve the performance 1-2 times.

from factorishwasm.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.