From ca1069e182b058914ef02a9d228977af3f6d70f9 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 27 Mar 2023 14:41:46 +0200 Subject: [PATCH] snapshot: avoid allocations if possible --- src/entt/entity/snapshot.hpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/entt/entity/snapshot.hpp b/src/entt/entity/snapshot.hpp index 9379b7a5a..72e6900c7 100644 --- a/src/entt/entity/snapshot.hpp +++ b/src/entt/entity/snapshot.hpp @@ -87,13 +87,11 @@ public: template const basic_snapshot &entities(Archive &archive) const { const auto &storage = reg->template storage(); - const auto sz = static_cast(storage.size()); - const auto released = static_cast(sz - storage.in_use()); - archive(sz); - archive(released); + archive(static_cast(storage.size())); + archive(static_cast(storage.in_use())); - for(auto first = storage.data(), last = first + sz; first != last; ++first) { + for(auto first = storage.data(), last = first + storage.size(); first != last; ++first) { archive(*first); } @@ -224,18 +222,22 @@ public: */ template const basic_snapshot_loader &entities(Archive &archive) const { + auto &storage = reg->template storage(); typename traits_type::entity_type length{}; - typename traits_type::entity_type released{}; + typename traits_type::entity_type in_use{}; + entity_type entity = null; archive(length); - archive(released); - std::vector all(length); + archive(in_use); + + storage.reserve(length); for(std::size_t pos{}; pos < length; ++pos) { - archive(all[pos]); + archive(entity); + storage.emplace(entity); } - reg->assign(all.cbegin(), all.cend(), released); + storage.in_use(in_use); return *this; } @@ -441,15 +443,15 @@ public: template basic_continuous_loader &entities(Archive &archive) { typename traits_type::entity_type length{}; - typename traits_type::entity_type released{}; + typename traits_type::entity_type in_use{}; archive(length); - archive(released); + archive(in_use); entity_type entt{null}; std::size_t pos{}; - for(const auto last = length - released; pos < last; ++pos) { + for(; pos < in_use; ++pos) { archive(entt); restore(entt); }