From 3359aaa62dcfe4fbc60cd048cb4404f2ea0070b5 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 18 Nov 2024 16:42:23 +0300 Subject: [PATCH] Simplify ecs::query_cache_container: store caches directly instead of storing nodes with uuid duplicates --- .../include/psemek/ecs/detail/query_cache.hpp | 3 + .../ecs/detail/query_cache_container.hpp | 63 +++++++------------ 2 files changed, 24 insertions(+), 42 deletions(-) diff --git a/libs/ecs/include/psemek/ecs/detail/query_cache.hpp b/libs/ecs/include/psemek/ecs/detail/query_cache.hpp index 2875801c..3aab1fe1 100644 --- a/libs/ecs/include/psemek/ecs/detail/query_cache.hpp +++ b/libs/ecs/include/psemek/ecs/detail/query_cache.hpp @@ -22,6 +22,9 @@ namespace psemek::ecs::detail { std::vector with_uuids; std::vector without_uuids; + + std::size_t hash; + std::list entries; using callback_factory = util::function const &)>; diff --git a/libs/ecs/include/psemek/ecs/detail/query_cache_container.hpp b/libs/ecs/include/psemek/ecs/detail/query_cache_container.hpp index a5986cf4..f4b700ff 100644 --- a/libs/ecs/include/psemek/ecs/detail/query_cache_container.hpp +++ b/libs/ecs/include/psemek/ecs/detail/query_cache_container.hpp @@ -10,14 +10,6 @@ namespace psemek::ecs::detail { - struct query_cache_data - { - std::size_t hash; - std::vector with_uuids; - std::vector without_uuids; - std::shared_ptr cache; - }; - struct query_cache_hash { std::size_t operator()(std::pair, util::span> const & uuids) const @@ -25,34 +17,28 @@ namespace psemek::ecs::detail return component_hash(uuids.first, uuids.second); } - std::size_t operator()(std::unique_ptr const & set) const + std::size_t operator()(std::shared_ptr const & cache) const { - return set->hash; + return cache->hash; } }; struct query_cache_equal { - bool operator()(std::pair, util::span> const & uuids, std::unique_ptr const & set) const + bool operator()(std::pair, util::span> const & uuids, std::shared_ptr const & cache) const { - if (uuids.first.size() != set->with_uuids.size()) + if (!std::equal(uuids.first.begin(), uuids.first.end(), cache->with_uuids.begin(), cache->with_uuids.end())) return false; - if (uuids.second.size() != set->without_uuids.size()) - return false; - - if (!std::equal(uuids.first.begin(), uuids.first.end(), set->with_uuids.begin())) - return false; - - if (!std::equal(uuids.second.begin(), uuids.second.end(), set->without_uuids.begin())) + if (!std::equal(uuids.second.begin(), uuids.second.end(), cache->without_uuids.begin(), cache->without_uuids.end())) return false; return true; } - bool operator()(std::unique_ptr const & set1, std::unique_ptr const & set2) const + bool operator()(std::shared_ptr const & cache1, std::shared_ptr const & cache2) const { - return set1.get() == set2.get(); + return cache1.get() == cache2.get(); } }; @@ -64,44 +50,37 @@ namespace psemek::ecs::detail auto it = caches_.find(std::pair{with_uuids, without_uuids}); if (it == caches_.end()) { - auto value = std::make_unique(); - value->cache = std::make_shared(); - for (auto const & uuid : with_uuids) - { - value->with_uuids.push_back(uuid); - value->cache->with_uuids.push_back(uuid); - } - for (auto const & uuid : without_uuids) - { - value->without_uuids.push_back(uuid); - value->cache->without_uuids.push_back(uuid); - } - value->hash = component_hash(with_uuids, without_uuids); + auto cache = std::make_shared(); + + cache->with_uuids.assign(with_uuids.begin(), with_uuids.end()); + cache->without_uuids.assign(without_uuids.begin(), without_uuids.end()); + + cache->hash = component_hash(with_uuids, without_uuids); table_container.apply([&](table & table){ - value->cache->add(&table); + cache->add(&table); }, with_uuids, without_uuids); - it = caches_.insert(std::move(value)).first; + it = caches_.insert(std::move(cache)).first; } - return it->get()->cache; + return *it; } template void apply(Function && function, ContainsUUID && contains_uuid) { - for (auto & cache_data : caches_) + for (auto & cache : caches_) { bool good = true; - for (auto const & uuid : cache_data->with_uuids) + for (auto const & uuid : cache->with_uuids) if (!contains_uuid(uuid)) { good = false; break; } - for (auto const & uuid : cache_data->without_uuids) + for (auto const & uuid : cache->without_uuids) if (contains_uuid(uuid)) { good = false; @@ -111,7 +90,7 @@ namespace psemek::ecs::detail if (!good) continue; - function(*(cache_data->cache)); + function(*cache); } } @@ -121,7 +100,7 @@ namespace psemek::ecs::detail } private: - util::hash_set, query_cache_hash, query_cache_equal> caches_; + util::hash_set, query_cache_hash, query_cache_equal> caches_; }; }