1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Add single block cache

This commit is contained in:
Lars 2025-06-15 15:00:01 -07:00
parent f00e53d398
commit a072629978
2 changed files with 23 additions and 2 deletions

View file

@ -78,12 +78,15 @@ void Map::deleteBlockImmediate(MapBlock *block)
std::unique_ptr<MapBlock> Map::detachBlock(MapBlock *block) std::unique_ptr<MapBlock> Map::detachBlock(MapBlock *block)
{ {
v3s16 p = block->getPos();
// Remove from container // Remove from container
auto it = m_blocks.find(block->getPos()); auto it = m_blocks.find(p);
assert(it != m_blocks.end()); assert(it != m_blocks.end());
std::unique_ptr<MapBlock> ret = std::move(it->second); std::unique_ptr<MapBlock> ret = std::move(it->second);
assert(ret.get() == block); assert(ret.get() == block);
m_blocks.erase(it); m_blocks.erase(it);
if (m_block_cache_p == p)
m_block_cache = nullptr;
// Mark as removed // Mark as removed
block->makeOrphan(); block->makeOrphan();
@ -106,8 +109,19 @@ void Map::insertBlock(std::unique_ptr<MapBlock> block)
MapBlock *Map::getBlockNoCreateNoEx(v3s16 p3d) MapBlock *Map::getBlockNoCreateNoEx(v3s16 p3d)
{ {
if(m_block_cache && p3d == m_block_cache_p){
return m_block_cache;
}
auto it = m_blocks.find(p3d); auto it = m_blocks.find(p3d);
return it != m_blocks.end() ? it->second.get() : nullptr; if (it == m_blocks.end())
return nullptr;
MapBlock *block = it->second.get();
m_block_cache = block;
m_block_cache_p = p3d;
return block;
} }
MapBlock *Map::getBlockNoCreate(v3s16 p3d) MapBlock *Map::getBlockNoCreate(v3s16 p3d)
@ -329,6 +343,8 @@ void Map::timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
// Delete directly from container // Delete directly from container
it = m_blocks.erase(it); it = m_blocks.erase(it);
if (m_block_cache_p == p)
m_block_cache = nullptr;
if (unloaded_blocks) if (unloaded_blocks)
unloaded_blocks->push_back(p); unloaded_blocks->push_back(p);

View file

@ -276,6 +276,11 @@ protected:
std::unordered_map<v3s16, std::unique_ptr<MapBlock>> m_blocks; std::unordered_map<v3s16, std::unique_ptr<MapBlock>> m_blocks;
// Last-used block is cached here for quicker access.
// Be sure to set this to nullptr when the cached block is deleted
MapBlock *m_block_cache = nullptr;
v3s16 m_block_cache_p;
// This stores the properties of the nodes on the map. // This stores the properties of the nodes on the map.
const NodeDefManager *m_nodedef; const NodeDefManager *m_nodedef;