diff --git a/src/common/database.hpp b/src/common/database.hpp index 44632189f0..567e4568cc 100644 --- a/src/common/database.hpp +++ b/src/common/database.hpp @@ -181,11 +181,11 @@ public: } void loadingFinished() override{ + size_t max_key = 0; // Cache all known values for (auto &pair : *this) { // Calculate the key that should be used size_t key = this->calculateCacheKey(pair.first); - // Check if the key fits into the current cache size if (this->cache.capacity() <= key) { // Some keys compute to 0, so we allocate a minimum of 500 (250*2) entries @@ -199,19 +199,15 @@ public: // Insert the value into the cache this->cache[key] = pair.second; + + // keep track of highest known key for easy resize + max_key = std::max(max_key, key); } - for( auto it = this->cache.rbegin(); it != this->cache.rend(); it++ ){ - if( *it != nullptr ){ - // Resize to only fit all existing non null entries - this->cache.resize( this->cache.rend() - it ); - - // Free the memory that was allocated too much - this->cache.shrink_to_fit(); - break; - } - } - + // Resize to only fit all existing non null entries + this->cache.resize(max_key); + // Free the memory that was allocated too much + this->cache.shrink_to_fit(); this->loaded = true; }