Switch MeshUpdateQueue to better data structure

This commit is contained in:
sfan5 2022-07-31 23:16:40 +02:00
parent 4c1ef1b72b
commit f22d40975e
2 changed files with 8 additions and 8 deletions

@ -158,8 +158,7 @@ CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mo
size_t *cache_hit_counter) size_t *cache_hit_counter)
{ {
CachedMapBlockData *cached_block = nullptr; CachedMapBlockData *cached_block = nullptr;
std::map<v3s16, CachedMapBlockData*>::iterator it = auto it = m_cache.find(p);
m_cache.find(p);
if (it != m_cache.end()) { if (it != m_cache.end()) {
cached_block = it->second; cached_block = it->second;
@ -193,7 +192,7 @@ CachedMapBlockData* MeshUpdateQueue::cacheBlock(Map *map, v3s16 p, UpdateMode mo
CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p) CachedMapBlockData* MeshUpdateQueue::getCachedBlock(const v3s16 &p)
{ {
std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.find(p); auto it = m_cache.find(p);
if (it != m_cache.end()) { if (it != m_cache.end()) {
return it->second; return it->second;
} }
@ -250,12 +249,11 @@ void MeshUpdateQueue::cleanupCache()
int t_now = time(0); int t_now = time(0);
for (std::map<v3s16, CachedMapBlockData*>::iterator it = m_cache.begin(); for (auto it = m_cache.begin(); it != m_cache.end(); ) {
it != m_cache.end(); ) {
CachedMapBlockData *cached_block = it->second; CachedMapBlockData *cached_block = it->second;
if (cached_block->refcount_from_queue == 0 && if (cached_block->refcount_from_queue == 0 &&
cached_block->last_used_timestamp < t_now - cache_seconds) { cached_block->last_used_timestamp < t_now - cache_seconds) {
m_cache.erase(it++); it = m_cache.erase(it);
delete cached_block; delete cached_block;
} else { } else {
++it; ++it;

@ -21,6 +21,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <ctime> #include <ctime>
#include <mutex> #include <mutex>
#include <unordered_map>
#include <unordered_set>
#include "mapblock_mesh.h" #include "mapblock_mesh.h"
#include "threading/mutex_auto_lock.h" #include "threading/mutex_auto_lock.h"
#include "util/thread.h" #include "util/thread.h"
@ -81,8 +83,8 @@ public:
private: private:
Client *m_client; Client *m_client;
std::vector<QueuedMeshUpdate *> m_queue; std::vector<QueuedMeshUpdate *> m_queue;
std::set<v3s16> m_urgents; std::unordered_set<v3s16> m_urgents;
std::map<v3s16, CachedMapBlockData *> m_cache; std::unordered_map<v3s16, CachedMapBlockData *> m_cache;
u64 m_next_cache_cleanup; // milliseconds u64 m_next_cache_cleanup; // milliseconds
std::mutex m_mutex; std::mutex m_mutex;