From c6cf90f67b51730cd833462135df197746f09b8c Mon Sep 17 00:00:00 2001 From: sfan5 Date: Tue, 12 Dec 2023 15:30:26 +0100 Subject: [PATCH] Change MapBlock content cache to a vector --- src/benchmark/benchmark_mapblock.cpp | 4 +++- src/mapblock.h | 6 ++++-- src/serverenvironment.cpp | 9 +++++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/benchmark/benchmark_mapblock.cpp b/src/benchmark/benchmark_mapblock.cpp index c273764dc..7756233af 100644 --- a/src/benchmark/benchmark_mapblock.cpp +++ b/src/benchmark/benchmark_mapblock.cpp @@ -118,11 +118,13 @@ static u32 workOnBoth(const MBContainer &vec) content_t c = n.getContent(); if (!block->contents_cached && !block->do_not_cache_contents) { - block->contents.insert(c); + if (!CONTAINS(block->contents, c)) + block->contents.push_back(c); if (block->contents.size() > 10) { // Too many different nodes... don't try to cache block->do_not_cache_contents = true; block->contents.clear(); + block->contents.shrink_to_fit(); } } } diff --git a/src/mapblock.h b/src/mapblock.h index 0c6fec7a0..54b118e64 100644 --- a/src/mapblock.h +++ b/src/mapblock.h @@ -19,7 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once -#include +#include #include "irr_v3d.h" #include "mapnode.h" #include "exceptions.h" @@ -476,7 +476,9 @@ public: //// ABM optimizations //// // Cache of content types - std::unordered_set contents; + // This is actually a set but for the small sizes we have a vector should be + // more efficient. + std::vector contents; // True if content types are cached bool contents_cached = false; // True if we never want to cache content types for this block diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 3cd849103..c4f8d1b13 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -782,6 +782,8 @@ struct ActiveABM s16 max_y; }; +#define CONTENT_TYPE_CACHE_MAX 64 + class ABMHandler { private: @@ -922,13 +924,16 @@ public: { MapNode n = block->getNodeNoCheck(p0); content_t c = n.getContent(); + // Cache content types as we go if (!block->contents_cached && !block->do_not_cache_contents) { - block->contents.insert(c); - if (block->contents.size() > 64) { + if (!CONTAINS(block->contents, c)) + block->contents.push_back(c); + if (block->contents.size() > CONTENT_TYPE_CACHE_MAX) { // Too many different nodes... don't try to cache block->do_not_cache_contents = true; block->contents.clear(); + block->contents.shrink_to_fit(); } }