Giter Club home page Giter Club logo

Comments (2)

SanderMertens avatar SanderMertens commented on May 24, 2024 1

If you connect your application to the explorer, you'll see that this is due to a growth in the number of empty tables:

Screenshot 2023-11-23 at 12 46 48 AM

The reason this is happening is because of these three lines:

attack.add<CastOf>(e);
attack.add<Target>(monsters[rand()%monsters.size()]);
e.add<Damage>(monsters[rand()%monsters.size()]);

You have 1000 monsters, which means there are in total 1000 * 1000 * 1000 unique combinations of pairs. Unique combinations of components (and pairs) create tables in Flecs. So given enough time, your application will create 1 billion tables, which is a lot.

The memory growth can be solved by periodically cleaning up empty tables, which you can do by adding the following system to your application:

    world.system()
      .interval(0.1)
      .no_readonly()
      .iter([](flecs::iter& it){
        ecs_delete_empty_tables(it.world(), 0, 1, 1, 0, 0);
      });

That doesn't solve the high CPU issue. Here there are a few things you can improve:

  • The system that prints the attack count creates a filter each frame, which is a bit wasteful. Filters are also not as fast as queries, so to speed this up you can do this instead:
    flecs::query<Attack> q = world.query<Attack>();

    world.set<Root>({0});
    world.system<Root>()
        .each([=](flecs::entity e, Root& root){
            // ...
            std::cout << "tick : attack count" << q.count() << std::endl;
    });
  • The CastOf, Target and Attack relationships fragment your attack entities to the point where you only have a single attack entity per table. To fix this, you can turn these into union relationships (docs on unions):
    world.component<Attack>().add(flecs::Union);
    world.component<Damage>().add(flecs::Union);
    world.component<CastOf>().add(flecs::Union);

You could also consider making them regular components, like:

struct Attack {
  flecs::entity entity;
};

After these fixes, I can easily run the application at ~1000 FPS.

from flecs.

Esp9527 avatar Esp9527 commented on May 24, 2024

Thank you very much!

from flecs.

Related Issues (20)

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.