Speed up loading CachedYamlDBs (#7599)

Instead of iterating from the end to find the last item in the cache, just store the highest key we've found.
Then, resize the cache to that
This commit is contained in:
Vincent Stumpf
2023-02-12 00:20:54 -08:00
committed by GitHub
parent ce8f053359
commit 7629ff4032

View File

@@ -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;
}