From 334b5d7bb8812548575cf654d1a8c9654f4d20be Mon Sep 17 00:00:00 2001 From: NukeBird Date: Mon, 17 Feb 2025 22:50:53 +0300 Subject: [PATCH] std::vector for reusable ids should give more cache benefits --- tests/zecsy.cpp | 5 +++++ zecsy.hpp | 11 ++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/zecsy.cpp b/tests/zecsy.cpp index eb8da86..2eddd4c 100644 --- a/tests/zecsy.cpp +++ b/tests/zecsy.cpp @@ -136,6 +136,11 @@ TEST_CASE("Addresses of removed components should be reused") } else { + /* + * Gotta reverse it because now we reuse ids in LIFO order + */ + std::reverse(addr.begin(), addr.end()); + for(int j = 0; j < N; ++j) { REQUIRE(&w.get(entities[j]) == addr[j]); diff --git a/zecsy.hpp b/zecsy.hpp index a798e7e..e0717f0 100644 --- a/zecsy.hpp +++ b/zecsy.hpp @@ -57,7 +57,7 @@ namespace zecsy struct component_pool { std::vector data; - std::queue free_list; // Reusable indices + std::vector free_list; // Reusable indices std::unordered_map entity_to_index; std::unordered_map index_to_entity; }; @@ -111,8 +111,8 @@ namespace zecsy size_t index; if(!pool.free_list.empty()) { - index = pool.free_list.front(); - pool.free_list.pop(); + index = pool.free_list.back(); + pool.free_list.pop_back(); } else { @@ -142,7 +142,7 @@ namespace zecsy auto& pool = pools[id]; auto index = pool.entity_to_index[e]; - pool.free_list.push(index); + pool.free_list.push_back(index); pool.entity_to_index.erase(e); pool.index_to_entity.erase(index); } @@ -163,7 +163,8 @@ namespace zecsy template inline void component_storage::set(entity_id e, const First& comp0, - const Second& comp1, const Rest&... rest_comps) + const Second& comp1, + const Rest&... rest_comps) { set(e, comp0); set(e, comp1);