From 1b89d4d54190f1d4bccd1da879d2267ce4dfa47c Mon Sep 17 00:00:00 2001 From: sfan5 Date: Thu, 11 Apr 2024 17:23:20 +0200 Subject: [PATCH] Simplify getBlockNodeIdMapping since commit 0f9c78c3ebf920fac65030e66367b9940055075f nodedef->get() will never return an entry with empty name, so we can drop the related parts. --- src/mapblock.cpp | 21 +++++++-------------- src/nameidmapping.h | 9 ++++----- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/mapblock.cpp b/src/mapblock.cpp index e2204f083..3985324bb 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -215,21 +215,22 @@ void MapBlock::expireIsAirCache() // List relevant id-name pairs for ids in the block using nodedef // Renumbers the content IDs (starting at 0 and incrementing) +// Note that there's no technical reason why we *have to* renumber the IDs, +// but we do it anyway as it also helps compressability. static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes, const NodeDefManager *nodedef) { - // The static memory requires about 65535 * sizeof(int) RAM in order to be + // The static memory requires about 65535 * 2 bytes RAM in order to be // sure we can handle all content ids. But it's absolutely worth it as it's // a speedup of 4 for one of the major time consuming functions on storing // mapblocks. thread_local std::unique_ptr mapping; static_assert(sizeof(content_t) == 2, "content_t must be 16-bit"); if (!mapping) - mapping = std::make_unique(USHRT_MAX + 1); + mapping = std::make_unique(CONTENT_MAX + 1); - memset(mapping.get(), 0xFF, (USHRT_MAX + 1) * sizeof(content_t)); + memset(mapping.get(), 0xFF, (CONTENT_MAX + 1) * sizeof(content_t)); - std::unordered_set unknown_contents; content_t id_counter = 0; for (u32 i = 0; i < MapBlock::nodecount; i++) { content_t global_id = nodes[i].getContent(); @@ -243,21 +244,13 @@ static void getBlockNodeIdMapping(NameIdMapping *nimap, MapNode *nodes, id = id_counter++; mapping[global_id] = id; - const ContentFeatures &f = nodedef->get(global_id); - const std::string &name = f.name; - if (name.empty()) - unknown_contents.insert(global_id); - else - nimap->set(id, name); + const auto &name = nodedef->get(global_id).name; + nimap->set(id, name); } // Update the MapNode nodes[i].setContent(id); } - for (u16 unknown_content : unknown_contents) { - errorstream << "getBlockNodeIdMapping(): IGNORING ERROR: " - << "Name for node id " << unknown_content << " not known" << std::endl; - } } // Correct ids in the block to match nodedef based on names. diff --git a/src/nameidmapping.h b/src/nameidmapping.h index 3acb43ef1..47f424cbc 100644 --- a/src/nameidmapping.h +++ b/src/nameidmapping.h @@ -21,8 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include -#include #include +#include #include "irrlichttypes_bloated.h" typedef std::unordered_map IdToNameMap; @@ -42,6 +42,7 @@ public: void set(u16 id, const std::string &name) { + assert(!name.empty()); m_id_to_name[id] = name; m_name_to_id[name] = id; } @@ -67,8 +68,7 @@ public: } bool getName(u16 id, std::string &result) const { - IdToNameMap::const_iterator i; - i = m_id_to_name.find(id); + auto i = m_id_to_name.find(id); if (i == m_id_to_name.end()) return false; result = i->second; @@ -76,8 +76,7 @@ public: } bool getId(const std::string &name, u16 &result) const { - NameToIdMap::const_iterator i; - i = m_name_to_id.find(name); + auto i = m_name_to_id.find(name); if (i == m_name_to_id.end()) return false; result = i->second;