From 12d177a6c877918f809710a2caeb1664db5e5ec8 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 13 Dec 2021 10:00:32 +0100 Subject: [PATCH] registry: removed ::orphans, use ::each/::orphan instead (no way to further optimize it) --- docs/md/entity.md | 29 ++++++++++++----------------- src/entt/entity/registry.hpp | 23 ----------------------- src/entt/entity/snapshot.hpp | 12 ++++++++---- test/entt/entity/registry.cpp | 20 ++++++-------------- 4 files changed, 26 insertions(+), 58 deletions(-) diff --git a/docs/md/entity.md b/docs/md/entity.md index d99d1026a..d5aedf803 100644 --- a/docs/md/entity.md +++ b/docs/md/entity.md @@ -2038,30 +2038,25 @@ registry.each([](auto entity) { }); ``` -It returns to the caller all the entities that are still in use.
As a rule of thumb, consider using a view or a group if the goal is to iterate entities that have a determinate set of components. These tools are usually much -faster than combining this function with a bunch of custom tests.
-In all the other cases, this is the way to go. - -There exists also another member function to use to retrieve orphans. An orphan -is an entity that is still in use and has no assigned components.
-The signature of the function is the same of `each`: +faster than combining the `each` function with a bunch of custom tests.
+In all the other cases, this is the way to go. For example, it's possible to +combine `each` with the `orphan` member function to clean up orphan entities +(that is, entities that are still in use and have no assigned components): ```cpp -registry.orphans([](auto entity) { - // ... +registry.each([®istry](auto entity) { + if(registry.orphan(entity)) { + registry.release(entity); + } }); ``` -To test the _orphanity_ of a single entity, use the member function `orphan` -instead. It accepts a valid entity identifer as an argument and returns true in -case the entity is an orphan, false otherwise. - -In general, all these functions can result in poor performance.
-`each` is fairly slow because of some checks it performs on each and every -entity. For similar reasons, `orphans` can be even slower. Both functions should -not be used frequently to avoid the risk of a performance hit. +In general, iterating all entities can result in poor performance. It should not +be done frequently to avoid the risk of a performance hit.
+However, it can be convenient when initializing an editor or to reclaim pending +identifiers. ## What is allowed and what is not diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index eead1635b..1d730dfa0 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -900,29 +900,6 @@ public: return std::none_of(pools.cbegin(), pools.cend(), [entity](auto &&curr) { return curr.second->contains(entity); }); } - /** - * @brief Iterates orphans and applies them the given function object. - * - * The signature of the function should be equivalent to the following: - * - * @code{.cpp} - * void(const Entity); - * @endcode - * - * This function can be very slow and should not be used frequently. - * - * @tparam Func Type of the function object to invoke. - * @param func A valid function object. - */ - template - void orphans(Func func) const { - each([this, &func](const auto entity) { - if(orphan(entity)) { - func(entity); - } - }); - } - /** * @brief Returns a sink object for the given component. * diff --git a/src/entt/entity/snapshot.hpp b/src/entt/entity/snapshot.hpp index e86a07f17..6a9ed86d6 100644 --- a/src/entt/entity/snapshot.hpp +++ b/src/entt/entity/snapshot.hpp @@ -263,8 +263,10 @@ public: * @return A valid loader to continue restoring data. */ const basic_snapshot_loader &orphans() const { - reg->orphans([this](const auto entt) { - reg->release(entt); + reg->each([this](const auto entt) { + if(reg->orphan(entt)) { + reg->release(entt); + } }); return *this; @@ -516,8 +518,10 @@ public: * @return A non-const reference to this loader. */ basic_continuous_loader &orphans() { - reg->orphans([this](const auto entt) { - reg->release(entt); + reg->each([this](const auto entt) { + if(reg->orphan(entt)) { + reg->release(entt); + } }); return *this; diff --git a/test/entt/entity/registry.cpp b/test/entt/entity/registry.cpp index bd201b42a..52961e9ec 100644 --- a/test/entt/entity/registry.cpp +++ b/test/entt/entity/registry.cpp @@ -781,30 +781,22 @@ TEST(Registry, Each) { TEST(Registry, Orphans) { entt::registry registry; - entt::registry::size_type tot{}; entt::entity entities[3u]{}; registry.create(std::begin(entities), std::end(entities)); registry.emplace(entities[0u]); registry.emplace(entities[2u]); - registry.orphans([&](auto) { ++tot; }); - - ASSERT_EQ(tot, 1u); + registry.each([&](const auto entt) { + ASSERT_TRUE(entt != entities[1u] || registry.orphan(entt)); + }); registry.erase(entities[0u]); registry.erase(entities[2u]); - tot = {}; - registry.orphans([&](auto) { ++tot; }); - - ASSERT_EQ(tot, 3u); - - registry.clear(); - tot = {}; - - registry.orphans([&](auto) { ++tot; }); - ASSERT_EQ(tot, 0u); + registry.each([&](const auto entt) { + ASSERT_TRUE(registry.orphan(entt)); + }); } TEST(Registry, View) {