aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Leitner-Ankerl <martin.ankerl@gmail.com>2022-06-11 11:00:53 +0200
committerMartin Leitner-Ankerl <martin.ankerl@gmail.com>2023-03-23 19:38:38 +0100
commit5e4ac5abf54f8e6d6330df0c73119aa0cca4c103 (patch)
tree5a5310f5fdf6302a89ee35127e6cf4c9b02b1b8a
parent1afca6b663bb54022afff193fd9d83856606b189 (diff)
downloadbitcoin-5e4ac5abf54f8e6d6330df0c73119aa0cca4c103.tar.xz
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().
-rw-r--r--src/coins.cpp9
-rw-r--r--src/test/validation_flush_tests.cpp5
-rw-r--r--src/validation.cpp1
3 files changed, 8 insertions, 7 deletions
diff --git a/src/coins.cpp b/src/coins.cpp
index 5a6ae525a7..f55932f302 100644
--- a/src/coins.cpp
+++ b/src/coins.cpp
@@ -253,9 +253,12 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
bool CCoinsViewCache::Flush() {
bool fOk = base->BatchWrite(cacheCoins, hashBlock, /*erase=*/true);
- if (fOk && !cacheCoins.empty()) {
- /* BatchWrite must erase all cacheCoins elements when erase=true. */
- throw std::logic_error("Not all cached coins were erased");
+ if (fOk) {
+ if (!cacheCoins.empty()) {
+ /* BatchWrite must erase all cacheCoins elements when erase=true. */
+ throw std::logic_error("Not all cached coins were erased");
+ }
+ ReallocateCache();
}
cachedCoinsUsage = 0;
return fOk;
diff --git a/src/test/validation_flush_tests.cpp b/src/test/validation_flush_tests.cpp
index 26c48eb0e0..205164b94c 100644
--- a/src/test/validation_flush_tests.cpp
+++ b/src/test/validation_flush_tests.cpp
@@ -131,8 +131,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
CoinsCacheSizeState::OK);
}
- // Flushing the view doesn't take us back to OK because cacheCoins has
- // preallocated memory that doesn't get reclaimed even after flush.
+ // Flushing the view does take us back to OK because ReallocateCache() is called
BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
@@ -144,7 +143,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
BOOST_CHECK_EQUAL(
chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0),
- CoinsCacheSizeState::CRITICAL);
+ CoinsCacheSizeState::OK);
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/src/validation.cpp b/src/validation.cpp
index e82fead89e..cf7688ea9f 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -4930,7 +4930,6 @@ bool Chainstate::ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
} else {
// Otherwise, flush state to disk and deallocate the in-memory coins map.
ret = FlushStateToDisk(state, FlushStateMode::ALWAYS);
- CoinsTip().ReallocateCache();
}
return ret;
}