Elide MapBlock::contents_cached

This commit is contained in:
sfan5 2023-12-12 16:08:41 +01:00
parent f5b35a074f
commit 777dca7043
3 changed files with 19 additions and 19 deletions

@ -106,7 +106,8 @@ static u32 workOnBoth(const MBContainer &vec)
int foo = 0;
for (MapBlock *block : vec) {
block->contents.clear();
block->contents_cached = false;
bool want_contents_cached = block->contents.empty() && !block->do_not_cache_contents;
v3s16 p0;
for(p0.X=0; p0.X<MAP_BLOCKSIZE; p0.X++)
@ -116,18 +117,18 @@ static u32 workOnBoth(const MBContainer &vec)
MapNode n = block->getNodeNoCheck(p0);
content_t c = n.getContent();
if (!block->contents_cached && !block->do_not_cache_contents) {
if (!CONTAINS(block->contents, c))
block->contents.push_back(c);
if (block->contents.size() > 10) {
// Too many different nodes... don't try to cache
if (want_contents_cached && !CONTAINS(block->contents, c)) {
if (block->contents.size() >= 10) {
want_contents_cached = false;
block->do_not_cache_contents = true;
block->contents.clear();
block->contents.shrink_to_fit();
} else {
block->contents.push_back(c);
}
}
}
block->contents_cached = !block->do_not_cache_contents;
foo += block->contents.size();
}
return foo;

@ -114,7 +114,7 @@ public:
m_modified_reason |= reason;
}
if (mod == MOD_STATE_WRITE_NEEDED)
contents_cached = false;
contents.clear();
}
inline u32 getModified()
@ -463,9 +463,8 @@ public:
// Cache of content types
// This is actually a set but for the small sizes we have a vector should be
// more efficient.
// Can be empty, in which case nothing was cached yet.
std::vector<content_t> contents;
// True if content types are cached
bool contents_cached = false;
// True if we never want to cache content types for this block
bool do_not_cache_contents = false;
// marks the sides which are opaque: 00+Z-Z+Y-Y+X-X

@ -894,7 +894,8 @@ public:
// Check the content type cache first
// to see whether there are any ABMs
// to be run at all for this block.
if (block->contents_cached) {
if (!block->contents.empty()) {
assert(!block->do_not_cache_contents); // invariant
blocks_cached++;
bool run_abms = false;
for (content_t c : block->contents) {
@ -905,9 +906,6 @@ public:
}
if (!run_abms)
return;
} else {
// Clear any caching
block->contents.clear();
}
blocks_scanned++;
@ -917,6 +915,8 @@ public:
u32 active_object_count = this->countObjects(block, map, active_object_count_wider);
m_env->m_added_objects = 0;
bool want_contents_cached = block->contents.empty() && !block->do_not_cache_contents;
v3s16 p0;
for(p0.X=0; p0.X<MAP_BLOCKSIZE; p0.X++)
for(p0.Y=0; p0.Y<MAP_BLOCKSIZE; p0.Y++)
@ -926,14 +926,15 @@ public:
content_t c = n.getContent();
// Cache content types as we go
if (!block->contents_cached && !block->do_not_cache_contents) {
if (!CONTAINS(block->contents, c))
block->contents.push_back(c);
if (block->contents.size() > CONTENT_TYPE_CACHE_MAX) {
if (want_contents_cached && !CONTAINS(block->contents, c)) {
if (block->contents.size() >= CONTENT_TYPE_CACHE_MAX) {
// Too many different nodes... don't try to cache
want_contents_cached = false;
block->do_not_cache_contents = true;
block->contents.clear();
block->contents.shrink_to_fit();
} else {
block->contents.push_back(c);
}
}
@ -997,7 +998,6 @@ public:
break;
}
}
block->contents_cached = !block->do_not_cache_contents;
}
};