Просмотр исходного кода

registry: ctor that reserves enough memory for count pools

Michele Caini 3 лет назад
Родитель
Сommit
4e13529adb
2 измененных файлов с 35 добавлено и 5 удалено
  1. 23 5
      src/entt/entity/registry.hpp
  2. 12 0
      test/entt/entity/registry.cpp

+ 23 - 5
src/entt/entity/registry.hpp

@@ -333,7 +333,25 @@ public:
     using context = internal::registry_context;
 
     /*! @brief Default constructor. */
-    basic_registry() = default;
+    basic_registry()
+        : pools{},
+          groups{},
+          entities{},
+          free_list{tombstone},
+          vars{} {}
+
+    /**
+     * @brief Allocates enough memory upon construction to store `count` pools.
+     * @param count The number of pools to allocate memory for.
+     */
+    basic_registry(const size_type count)
+        : pools{},
+          groups{},
+          entities{},
+          free_list{tombstone},
+          vars{} {
+        pools.reserve(count);
+    }
 
     /**
      * @brief Move constructor.
@@ -1465,10 +1483,10 @@ public:
     }
 
 private:
-    dense_map<id_type, std::unique_ptr<base_type>, identity> pools{};
-    std::vector<group_data> groups{};
-    std::vector<entity_type> entities{};
-    entity_type free_list{tombstone};
+    dense_map<id_type, std::unique_ptr<base_type>, identity> pools;
+    std::vector<group_data> groups;
+    std::vector<entity_type> entities;
+    entity_type free_list;
     context vars;
 };
 

+ 12 - 0
test/entt/entity/registry.cpp

@@ -337,6 +337,18 @@ TEST(Registry, Functionalities) {
     ASSERT_TRUE(registry.storage<int>().empty());
 }
 
+TEST(Registry, Constructors) {
+    entt::registry registry;
+    entt::registry other{42};
+    const entt::entity entity = entt::tombstone;
+
+    ASSERT_TRUE(registry.empty());
+    ASSERT_TRUE(other.empty());
+
+    ASSERT_EQ(registry.released(), entity);
+    ASSERT_EQ(other.released(), entity);
+}
+
 TEST(Registry, Move) {
     entt::registry registry;
     const auto entity = registry.create();