-void MeshUpdateQueue::cleanupCache()
-{
- const int mapblock_kB = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE *
- sizeof(MapNode) / 1000;
- g_profiler->avg("MeshUpdateQueue MapBlock cache size kB",
- mapblock_kB * m_cache.size());
-
- // Iterating the entire cache can get pretty expensive so don't do it too often
- {
- constexpr int cleanup_interval = 250;
- const u64 now = porting::getTimeMs();
- if (m_next_cache_cleanup > now)
- return;
- m_next_cache_cleanup = now + cleanup_interval;
- }
-
- // The cache size is kept roughly below cache_soft_max_size, not letting
- // anything get older than cache_seconds_max or deleted before 2 seconds.
- const int cache_seconds_max = 10;
- const int cache_soft_max_size = m_meshgen_block_cache_size * 1000 / mapblock_kB;
- int cache_seconds = MYMAX(2, cache_seconds_max -
- m_cache.size() / (cache_soft_max_size / cache_seconds_max));
-
- int t_now = time(0);
-
- for (auto it = m_cache.begin(); it != m_cache.end(); ) {
- CachedMapBlockData *cached_block = it->second;
- if (cached_block->refcount_from_queue == 0 &&
- cached_block->last_used_timestamp < t_now - cache_seconds) {
- it = m_cache.erase(it);
- delete cached_block;
- } else {
- ++it;
- }
- }
-}
-