Browse Source

core: added entt::page_size

Michele Caini 4 years ago
parent
commit
7c2bce8baf

+ 3 - 0
src/entt/core/fwd.hpp

@@ -17,6 +17,9 @@ class basic_any;
 using id_type = ENTT_ID_TYPE;
 
 
+/*! @brief Default page size. */
+inline static constexpr auto page_size = ENTT_PAGE_SIZE;
+
 /*! @brief Alias declaration for the most common use case. */
 using any = basic_any<>;
 

+ 3 - 2
src/entt/entity/helper.hpp

@@ -4,6 +4,7 @@
 
 #include <type_traits>
 #include "../config/config.h"
+#include "../core/fwd.hpp"
 #include "../core/type_traits.hpp"
 #include "../signal/delegate.hpp"
 #include "registry.hpp"
@@ -151,8 +152,8 @@ Entity to_entity(const basic_registry<Entity> &reg, const Component &instance) {
     const auto view = reg.template view<const Component>();
     const auto *addr = std::addressof(instance);
 
-    for(auto it = view.rbegin(), last = view.rend(); it < last; it += ENTT_PAGE_SIZE) {
-        if(const auto dist = (addr - std::addressof(view.template get<const Component>(*it))); dist >= 0 && dist < ENTT_PAGE_SIZE) {
+    for(auto it = view.rbegin(), last = view.rend(); it < last; it += page_size) {
+        if(const auto dist = (addr - std::addressof(view.template get<const Component>(*it))); dist >= 0 && dist < page_size) {
             return *(it + dist);
         }
     }

+ 1 - 1
src/entt/entity/sparse_set.hpp

@@ -9,6 +9,7 @@
 #include <utility>
 #include "../config/config.h"
 #include "../core/algorithm.hpp"
+#include "../core/fwd.hpp"
 #include "entity.hpp"
 #include "fwd.hpp"
 
@@ -42,7 +43,6 @@ namespace entt {
 template<typename Entity, typename Allocator>
 class basic_sparse_set {
     static constexpr auto growth_factor = 1.5;
-    static constexpr auto page_size = ENTT_PAGE_SIZE;
 
     using traits_type = entt_traits<Entity>;
 

+ 1 - 1
src/entt/entity/storage.hpp

@@ -10,6 +10,7 @@
 #include <utility>
 #include "../config/config.h"
 #include "../core/algorithm.hpp"
+#include "../core/fwd.hpp"
 #include "../core/type_traits.hpp"
 #include "../signal/sigh.hpp"
 #include "entity.hpp"
@@ -51,7 +52,6 @@ class basic_storage: public basic_sparse_set<Entity, typename std::allocator_tra
     static_assert(std::is_move_constructible_v<Type> && std::is_move_assignable_v<Type>, "The managed type must be at least move constructible and assignable");
 
     static constexpr auto growth_factor = 1.5;
-    static constexpr auto page_size = ENTT_PAGE_SIZE;
 
     using underlying_type = basic_sparse_set<Entity>;
     using traits_type = entt_traits<Entity>;

+ 4 - 4
test/entt/entity/helper.cpp

@@ -50,7 +50,7 @@ TEST(Helper, ToEntity) {
     const auto entity = registry.create();
     registry.emplace<int>(entity);
 
-    while(registry.size<int>() < (ENTT_PAGE_SIZE - 1u)) {
+    while(registry.size<int>() < (entt::page_size - 1u)) {
         registry.emplace<int>(registry.create(), value);
     }
 
@@ -64,15 +64,15 @@ TEST(Helper, ToEntity) {
     ASSERT_EQ(entt::to_entity(registry, registry.get<int>(other)), other);
     ASSERT_EQ(entt::to_entity(registry, registry.get<int>(next)), next);
 
-    ASSERT_EQ(&registry.get<int>(entity) + ENTT_PAGE_SIZE - 1u, &registry.get<int>(other));
-    ASSERT_NE(&registry.get<int>(entity) + ENTT_PAGE_SIZE, &registry.get<int>(next));
+    ASSERT_EQ(&registry.get<int>(entity) + entt::page_size - 1u, &registry.get<int>(other));
+    ASSERT_NE(&registry.get<int>(entity) + entt::page_size, &registry.get<int>(next));
 
     registry.destroy(other);
 
     ASSERT_EQ(entt::to_entity(registry, registry.get<int>(entity)), entity);
     ASSERT_EQ(entt::to_entity(registry, registry.get<int>(next)), next);
 
-    ASSERT_EQ(&registry.get<int>(entity) + ENTT_PAGE_SIZE - 1u, &registry.get<int>(next));
+    ASSERT_EQ(&registry.get<int>(entity) + entt::page_size - 1u, &registry.get<int>(next));
 
     ASSERT_EQ(entt::to_entity(registry, 42), null);
     ASSERT_EQ(entt::to_entity(registry, value), null);

+ 4 - 4
test/entt/entity/registry.cpp

@@ -154,8 +154,8 @@ TEST(Registry, Functionalities) {
     ASSERT_TRUE(registry.empty());
 
     ASSERT_EQ(registry.capacity(), 42u);
-    ASSERT_EQ(registry.capacity<int>(), ENTT_PAGE_SIZE);
-    ASSERT_EQ(registry.capacity<char>(), ENTT_PAGE_SIZE);
+    ASSERT_EQ(registry.capacity<int>(), entt::page_size);
+    ASSERT_EQ(registry.capacity<char>(), entt::page_size);
     ASSERT_EQ(registry.size<int>(), 0u);
     ASSERT_EQ(registry.size<char>(), 0u);
     ASSERT_TRUE((registry.empty<int, char>()));
@@ -295,8 +295,8 @@ TEST(Registry, Functionalities) {
     ASSERT_EQ(registry.size<char>(), 0u);
     ASSERT_TRUE(registry.empty<int>());
 
-    ASSERT_EQ(registry.capacity<int>(), ENTT_PAGE_SIZE);
-    ASSERT_EQ(registry.capacity<char>(), ENTT_PAGE_SIZE);
+    ASSERT_EQ(registry.capacity<int>(), entt::page_size);
+    ASSERT_EQ(registry.capacity<char>(), entt::page_size);
 
     registry.shrink_to_fit<int, char>();
 

+ 16 - 17
test/entt/entity/sparse_set.cpp

@@ -85,34 +85,33 @@ TEST(SparseSet, Functionalities) {
 
 TEST(SparseSet, Pagination) {
     entt::sparse_set set;
-    constexpr auto page_size = ENTT_PAGE_SIZE;
 
     ASSERT_EQ(set.extent(), 0u);
 
-    set.emplace(entt::entity{page_size-1});
+    set.emplace(entt::entity{entt::page_size-1u});
 
-    ASSERT_EQ(set.extent(), page_size);
-    ASSERT_TRUE(set.contains(entt::entity{page_size-1}));
+    ASSERT_EQ(set.extent(), entt::page_size);
+    ASSERT_TRUE(set.contains(entt::entity{entt::page_size-1u}));
 
-    set.emplace(entt::entity{page_size});
+    set.emplace(entt::entity{entt::page_size});
 
-    ASSERT_EQ(set.extent(), 2 * page_size);
-    ASSERT_TRUE(set.contains(entt::entity{page_size-1}));
-    ASSERT_TRUE(set.contains(entt::entity{page_size}));
-    ASSERT_FALSE(set.contains(entt::entity{page_size+1}));
+    ASSERT_EQ(set.extent(), 2 * entt::page_size);
+    ASSERT_TRUE(set.contains(entt::entity{entt::page_size-1u}));
+    ASSERT_TRUE(set.contains(entt::entity{entt::page_size}));
+    ASSERT_FALSE(set.contains(entt::entity{entt::page_size+1u}));
 
-    set.erase(entt::entity{page_size-1});
+    set.erase(entt::entity{entt::page_size-1u});
 
-    ASSERT_EQ(set.extent(), 2 * page_size);
-    ASSERT_FALSE(set.contains(entt::entity{page_size-1}));
-    ASSERT_TRUE(set.contains(entt::entity{page_size}));
+    ASSERT_EQ(set.extent(), 2 * entt::page_size);
+    ASSERT_FALSE(set.contains(entt::entity{entt::page_size-1u}));
+    ASSERT_TRUE(set.contains(entt::entity{entt::page_size}));
 
     set.shrink_to_fit();
-    set.erase(entt::entity{page_size});
+    set.erase(entt::entity{entt::page_size});
 
-    ASSERT_EQ(set.extent(), 2 * page_size);
-    ASSERT_FALSE(set.contains(entt::entity{page_size-1}));
-    ASSERT_FALSE(set.contains(entt::entity{page_size}));
+    ASSERT_EQ(set.extent(), 2 * entt::page_size);
+    ASSERT_FALSE(set.contains(entt::entity{entt::page_size-1u}));
+    ASSERT_FALSE(set.contains(entt::entity{entt::page_size}));
 
     set.shrink_to_fit();
 

+ 13 - 13
test/entt/entity/storage.cpp

@@ -40,7 +40,7 @@ TEST(Storage, Functionalities) {
 
     pool.reserve(42);
 
-    ASSERT_EQ(pool.capacity(), ENTT_PAGE_SIZE);
+    ASSERT_EQ(pool.capacity(), entt::page_size);
     ASSERT_TRUE(pool.empty());
     ASSERT_EQ(pool.size(), 0u);
     ASSERT_EQ(std::as_const(pool).begin(), std::as_const(pool).end());
@@ -80,7 +80,7 @@ TEST(Storage, Functionalities) {
     ASSERT_FALSE(pool.contains(entt::entity{0}));
     ASSERT_FALSE(pool.contains(entt::entity{41}));
 
-    ASSERT_EQ(pool.capacity(), ENTT_PAGE_SIZE);
+    ASSERT_EQ(pool.capacity(), entt::page_size);
 
     pool.shrink_to_fit();
 
@@ -211,24 +211,24 @@ TEST(Storage, Remove) {
 TEST(Storage, ShrinkToFit) {
     entt::storage<int> pool;
 
-    for(std::size_t next{}; next < ENTT_PAGE_SIZE; ++next) {
+    for(std::size_t next{}; next < entt::page_size; ++next) {
         pool.emplace(entt::entity(next));
     }
 
-    pool.emplace(entt::entity{ENTT_PAGE_SIZE});
-    pool.erase(entt::entity{ENTT_PAGE_SIZE});
+    pool.emplace(entt::entity{entt::page_size});
+    pool.erase(entt::entity{entt::page_size});
 
-    ASSERT_EQ(pool.capacity(), 2 * ENTT_PAGE_SIZE);
-    ASSERT_EQ(pool.size(), ENTT_PAGE_SIZE);
+    ASSERT_EQ(pool.capacity(), 2 * entt::page_size);
+    ASSERT_EQ(pool.size(), entt::page_size);
 
     pool.shrink_to_fit();
 
-    ASSERT_EQ(pool.capacity(), ENTT_PAGE_SIZE);
-    ASSERT_EQ(pool.size(), ENTT_PAGE_SIZE);
+    ASSERT_EQ(pool.capacity(), entt::page_size);
+    ASSERT_EQ(pool.size(), entt::page_size);
 
     pool.clear();
 
-    ASSERT_EQ(pool.capacity(), ENTT_PAGE_SIZE);
+    ASSERT_EQ(pool.capacity(), entt::page_size);
     ASSERT_EQ(pool.size(), 0u);
 
     pool.shrink_to_fit();
@@ -679,12 +679,12 @@ TEST(Storage, CanModifyDuringIteration) {
     entt::storage<int> pool;
     pool.emplace(entt::entity{0}, 42);
 
-    ASSERT_EQ(pool.capacity(), ENTT_PAGE_SIZE);
+    ASSERT_EQ(pool.capacity(), entt::page_size);
 
     const auto it = pool.cbegin();
-    pool.reserve(ENTT_PAGE_SIZE + 1u);
+    pool.reserve(entt::page_size + 1u);
 
-    ASSERT_EQ(pool.capacity(), 2 * ENTT_PAGE_SIZE);
+    ASSERT_EQ(pool.capacity(), 2 * entt::page_size);
 
     // this should crash with asan enabled if we break the constraint
     const auto entity = *it;