|
@@ -181,11 +181,11 @@ public:
|
|
}
|
|
}
|
|
|
|
|
|
void loadingFinished() override{
|
|
void loadingFinished() override{
|
|
|
|
+ size_t max_key = 0;
|
|
// Cache all known values
|
|
// Cache all known values
|
|
for (auto &pair : *this) {
|
|
for (auto &pair : *this) {
|
|
// Calculate the key that should be used
|
|
// Calculate the key that should be used
|
|
size_t key = this->calculateCacheKey(pair.first);
|
|
size_t key = this->calculateCacheKey(pair.first);
|
|
-
|
|
|
|
// Check if the key fits into the current cache size
|
|
// Check if the key fits into the current cache size
|
|
if (this->cache.capacity() <= key) {
|
|
if (this->cache.capacity() <= key) {
|
|
// Some keys compute to 0, so we allocate a minimum of 500 (250*2) entries
|
|
// 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
|
|
// Insert the value into the cache
|
|
this->cache[key] = pair.second;
|
|
this->cache[key] = pair.second;
|
|
- }
|
|
|
|
|
|
|
|
- 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;
|
|
|
|
- }
|
|
|
|
|
|
+ // keep track of highest known key for easy resize
|
|
|
|
+ max_key = std::max(max_key, key);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 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;
|
|
this->loaded = true;
|
|
}
|
|
}
|
|
|
|
|