How to improve performance in entity relation scenario? #963
-
BackgroundI want to create a world in C++ with many cells, each with its own actors. I'm using entity relations like this: // Add a new actor to cell
world.entity().set<EcsActor>({}).add<EcsRelationBelongsToCell>(cell_entity); then save the query in each cell: cell.actors_query = world.query_builder<EcsActor>().with<EcsRelationBelongsToCell>(cell_entity).build(); In the update process, I reuse the query to loop on all actors in each cell: cell.actors_query.each([&counter](EcsActor &actor) {
actor.is_active = !actor.is_active;
++counter.calc_actors;
}); I used Performance Data
It seems that my implementation via I have these questions:
Profile ResultHere is the profile data: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Flecs version: Release v3.2.0, compiled from source code with the project. |
Beta Was this translation helpful? Give feedback.
-
Take a look at query groups: https://github.com/SanderMertens/flecs/blob/master/examples/cpp/queries/group_iter/src/main.cpp They'll let you create a single query that can be "grouped by" cell. You can then iterate the query for a specific cell, which should be a lot more efficient than creating lots of different queries. |
Beta Was this translation helpful? Give feedback.
Try upgrading to 3.2.1, it can improve query performance in some cases by 20-40% :)
Because you're using the
iter
function to iterate the query, adding a singleton will iterate entities one by one. The reason for this is query instancing. To fix this, you can either replaceiter
witheach
(easiest) or add.instanced()
to the system builder (check the docs/examples to see how to iterate an instanced query).Yeah I need to update the examples to remove the cust…