Przeglądaj źródła

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
Vincent Stumpf 2 lat temu
rodzic
commit
7629ff4032
1 zmienionych plików z 7 dodań i 11 usunięć
  1. 7 11
      src/common/database.hpp

+ 7 - 11
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;
-		}
 
-		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;
 	}