mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-08 14:47:31 +02:00
Merge bitcoin/bitcoin#34164: validation: add reusable coins view for ConnectBlock
3e0fd0e4ddrefactor: rename will_reuse_cache to reallocate_cache (Andrew Toth)44b4ee194dvalidation: reuse same CCoinsViewCache for every ConnectBlock call (Andrew Toth)8fb6043231coins: introduce CCoinsViewCache::ResetGuard (Andrew Toth)041758f5edcoins: use hashBlock setter internally for CCoinsViewCache methods (Andrew Toth)8dd9200fc9coins: add Reset on CCoinsViewCache (Andrew Toth) Pull request description: This is the first commit of #31132, which can be merged as an independent change. It has a small benefit on its own, but will help in moving the parent PR forward. Add a `Reset()` method to `CCoinsViewCache` that clears `cacheCoins`, `cachedCoinsUsage`, and `hashBlock` without flushing to the `base` view. This allows efficiently reusing a cache instance across multiple blocks. Add `CCoinsViewCache::CreateResetGuard` method to return a `CCoinsViewCache::ResetGuard`. The `ResetGuard` automatically calls `Reset()` on destruction. This RAII pattern ensures the cache is always properly reset between blocks. Add `m_connect_block_view` as a persistent `CCoinsViewCache` for `ConnectBlock`, avoiding repeated memory allocations. ACKs for top commit: l0rinc: ACK3e0fd0e4ddachow101: ACK3e0fd0e4ddsedited: ACK3e0fd0e4ddTree-SHA512: a95feaa062a9eb7cf7514425a7e7adffd347cd1f7b32b4c1fefcde30002141757c184174702b3104a029dcd33194f8bd734159deebb2e668716089305b42cb00
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <consensus/consensus.h>
|
||||
#include <logging.h>
|
||||
#include <random.h>
|
||||
#include <uint256.h>
|
||||
#include <util/trace.h>
|
||||
|
||||
TRACEPOINT_SEMAPHORE(utxocache, add);
|
||||
@@ -247,15 +248,15 @@ void CCoinsViewCache::BatchWrite(CoinsViewCacheCursor& cursor, const uint256& ha
|
||||
}
|
||||
}
|
||||
}
|
||||
hashBlock = hashBlockIn;
|
||||
SetBestBlock(hashBlockIn);
|
||||
}
|
||||
|
||||
void CCoinsViewCache::Flush(bool will_reuse_cache)
|
||||
void CCoinsViewCache::Flush(bool reallocate_cache)
|
||||
{
|
||||
auto cursor{CoinsViewCacheCursor(m_sentinel, cacheCoins, /*will_erase=*/true)};
|
||||
base->BatchWrite(cursor, hashBlock);
|
||||
cacheCoins.clear();
|
||||
if (will_reuse_cache) {
|
||||
if (reallocate_cache) {
|
||||
ReallocateCache();
|
||||
}
|
||||
cachedCoinsUsage = 0;
|
||||
@@ -271,6 +272,13 @@ void CCoinsViewCache::Sync()
|
||||
}
|
||||
}
|
||||
|
||||
void CCoinsViewCache::Reset() noexcept
|
||||
{
|
||||
cacheCoins.clear();
|
||||
cachedCoinsUsage = 0;
|
||||
SetBestBlock(uint256::ZERO);
|
||||
}
|
||||
|
||||
void CCoinsViewCache::Uncache(const COutPoint& hash)
|
||||
{
|
||||
CCoinsMap::iterator it = cacheCoins.find(hash);
|
||||
|
||||
Reference in New Issue
Block a user