Call ReallocateCache() on each Flush()

This frees up all associated memory with the map, not only the nodes.
This is necessary in preparation for using the PoolAllocator for
CCoinsMap, which does not actually free any memory on clear().
This commit is contained in:
Martin Leitner-Ankerl 2022-06-11 11:00:53 +02:00
parent 1afca6b663
commit 5e4ac5abf5
3 changed files with 8 additions and 7 deletions

View file

@ -253,10 +253,13 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
bool CCoinsViewCache::Flush() { bool CCoinsViewCache::Flush() {
bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/true); bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/true);
if (fOk && !cacheCoins.empty()) { if (fOk) {
if (!cacheCoins.empty()) {
/* BatchWrite must erase all cacheCoins elements when erase=true. */ /* BatchWrite must erase all cacheCoins elements when erase=true. */
throw std::logic_error("Not all cached coins were erased"); throw std::logic_error("Not all cached coins were erased");
} }
ReallocateCache();
}
cachedCoinsUsage = 0; cachedCoinsUsage = 0;
return fOk; return fOk;
} }

View file

@ -131,8 +131,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
CoinsCacheSizeState::OK); CoinsCacheSizeState::OK);
} }
// Flushing the view doesn't take us back to OK because cacheCoins has // Flushing the view does take us back to OK because ReallocateCache() is called
// preallocated memory that doesn't get reclaimed even after flush.
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
@ -144,7 +143,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
BOOST_CHECK_EQUAL( BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0), chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
CoinsCacheSizeState::CRITICAL); CoinsCacheSizeState::OK);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View file

@ -4930,7 +4930,6 @@ bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
} else { } else {
// Otherwise, flush state to disk and deallocate the in-memory coins map. // Otherwise, flush state to disk and deallocate the in-memory coins map.
ret = FlushStateToDisk(state, FlushStateMode::ALWAYS); ret = FlushStateToDisk(state, FlushStateMode::ALWAYS);
CoinsTip().ReallocateCache();
} }
return ret; return ret;
} }