Simplify getBlockNodeIdMapping

since commit 0f9c78c3ebf920fac65030e66367b9940055075f nodedef->get()
will never return an entry with empty name, so we can drop the related parts.
This commit is contained in:
sfan5 2024-04-11 17:23:20 +02:00
parent d8190e1c5f
commit 1b89d4d541
2 changed files with 11 additions and 19 deletions

@ -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<content_t[]> mapping;
static_assert(sizeof(content_t) == 2, "content_t must be 16-bit");
if (!mapping)
mapping = std::make_unique<content_t[]>(USHRT_MAX + 1);
mapping = std::make_unique<content_t[]>(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<content_t> 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
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.

@ -21,8 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
#include <iostream>
#include <set>
#include <unordered_map>
#include <cassert>
#include "irrlichttypes_bloated.h"
typedef std::unordered_map<u16, std::string> 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;