skypjack / entt

Gaming meets modern C++ - a fast and reliable entity component system (ECS) and much more
https://github.com/skypjack/entt/wiki
MIT License
9.6k stars 844 forks source link

Efficiently iterating over a set of groups? #1121

Closed reworks-org closed 2 months ago

reworks-org commented 2 months ago

I've been working on a renderer for a project and I've written some code using entt::group, but I feel like it should be more efficient, rather than fetching each group one at a time and iterating over each.

I was wondering if entt has any way to improve lookup speed if I say, added a renderable component with a pointer to another component. I know I could deletegate the task to threads, but I dont know if entt::registry::group is thread safe when looking up a group and creating one.

my code below:

const auto sprites   = scene->m_registry.group<components::Sprite>(entt::get<components::Transform>, entt::exclude<flags::Disabled>);
const auto texts     = scene->m_registry.group<components::Text>(entt::get<components::Transform>, entt::exclude<flags::Disabled>);
const auto circles   = scene->m_registry.group<components::Circle>(entt::get<components::Transform>, entt::exclude<flags::Disabled>);
const auto ellipses  = scene->m_registry.group<components::Ellipse>(entt::get<components::Transform>, entt::exclude<flags::Disabled>);
const auto points    = scene->m_registry.group<components::Point>(entt::get<components::Transform>, entt::exclude<flags::Disabled>);
const auto polygons  = scene->m_registry.group<components::Polygon>(entt::get<components::Transform>, entt::exclude<flags::Disabled>);
const auto polylines = scene->m_registry.group<components::Polyline>(entt::get<components::Transform>, entt::exclude<flags::Disabled>);

for (auto&& [entity, sprite, transform] : sprites.each())
{

}

// Rest of groups here....
skypjack commented 2 months ago

To be honest, I'm not sure I get correctly what you're looking for. Anyway:

I was wondering if entt has any way to improve lookup speed if I say, added a renderable component with a pointer to another component

Well, if you enable pointer stability for a given type, you can store the pointer directly and avoid the lookup as a whole. This is a very convenient way to create parent-child relationsips for example.

I dont know if entt::registry::group is thread safe when looking up a group and creating one.

After the warm-up, yes. The very first use they could run into problems though, because they both want to register a descriptor in the same container under the hood.

skypjack commented 2 months ago

Ping. 🙂