Change MapBlock content cache to a vector

This commit is contained in:
sfan5 2023-12-12 15:30:26 +01:00
parent cb6e3ac6e1
commit c6cf90f67b
3 changed files with 14 additions and 5 deletions

@ -118,11 +118,13 @@ static u32 workOnBoth(const MBContainer &vec)
content_t c = n.getContent(); content_t c = n.getContent();
if (!block->contents_cached && !block->do_not_cache_contents) { 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) { if (block->contents.size() > 10) {
// Too many different nodes... don't try to cache // Too many different nodes... don't try to cache
block->do_not_cache_contents = true; block->do_not_cache_contents = true;
block->contents.clear(); block->contents.clear();
block->contents.shrink_to_fit();
} }
} }
} }

@ -19,7 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once #pragma once
#include <set> #include <vector>
#include "irr_v3d.h" #include "irr_v3d.h"
#include "mapnode.h" #include "mapnode.h"
#include "exceptions.h" #include "exceptions.h"
@ -476,7 +476,9 @@ public:
//// ABM optimizations //// //// ABM optimizations ////
// Cache of content types // Cache of content types
std::unordered_set<content_t> contents; // This is actually a set but for the small sizes we have a vector should be
// more efficient.
std::vector<content_t> contents;
// True if content types are cached // True if content types are cached
bool contents_cached = false; bool contents_cached = false;
// True if we never want to cache content types for this block // True if we never want to cache content types for this block

@ -782,6 +782,8 @@ struct ActiveABM
s16 max_y; s16 max_y;
}; };
#define CONTENT_TYPE_CACHE_MAX 64
class ABMHandler class ABMHandler
{ {
private: private:
@ -922,13 +924,16 @@ public:
{ {
MapNode n = block->getNodeNoCheck(p0); MapNode n = block->getNodeNoCheck(p0);
content_t c = n.getContent(); content_t c = n.getContent();
// Cache content types as we go // Cache content types as we go
if (!block->contents_cached && !block->do_not_cache_contents) { if (!block->contents_cached && !block->do_not_cache_contents) {
block->contents.insert(c); if (!CONTAINS(block->contents, c))
if (block->contents.size() > 64) { block->contents.push_back(c);
if (block->contents.size() > CONTENT_TYPE_CACHE_MAX) {
// Too many different nodes... don't try to cache // Too many different nodes... don't try to cache
block->do_not_cache_contents = true; block->do_not_cache_contents = true;
block->contents.clear(); block->contents.clear();
block->contents.shrink_to_fit();
} }
} }