]> git.lizzy.rs Git - minetest.git/commitdiff
Ratelimit MeshUpdateQueue::cleanupCache() runs
authorsfan5 <sfan5@live.de>
Sun, 31 Jul 2022 21:10:18 +0000 (23:10 +0200)
committersfan5 <sfan5@live.de>
Tue, 2 Aug 2022 09:58:26 +0000 (11:58 +0200)
src/client/mesh_generator_thread.cpp
src/client/mesh_generator_thread.h

index 5c3f4180bb32d7136955ab8b8b6e2e352ffb701e..9f4d98aac5865fc49862b40fae57ac0850301936 100644 (file)
@@ -50,7 +50,8 @@ QueuedMeshUpdate::~QueuedMeshUpdate()
 */
 
 MeshUpdateQueue::MeshUpdateQueue(Client *client):
-       m_client(client)
+       m_client(client),
+       m_next_cache_cleanup(0)
 {
        m_cache_enable_shaders = g_settings->getBool("enable_shaders");
        m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
@@ -231,6 +232,15 @@ void MeshUpdateQueue::cleanupCache()
        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;
index 1b734bc0614e35c5e3de747c098dd4f26b84c4b1..e48c8334d62f2833a28dde252349666b8104e73e 100644 (file)
@@ -83,6 +83,7 @@ class MeshUpdateQueue
        std::vector<QueuedMeshUpdate *> m_queue;
        std::set<v3s16> m_urgents;
        std::map<v3s16, CachedMapBlockData *> m_cache;
+       u64 m_next_cache_cleanup; // milliseconds
        std::mutex m_mutex;
 
        // TODO: Add callback to update these when g_settings changes