Merge bitcoin/bitcoin#34164: validation: add reusable coins view for ConnectBlock

3e0fd0e4dd refactor: rename will_reuse_cache to reallocate_cache (Andrew Toth)
44b4ee194d validation: reuse same CCoinsViewCache for every ConnectBlock call (Andrew Toth)
8fb6043231 coins: introduce CCoinsViewCache::ResetGuard (Andrew Toth)
041758f5ed coins: use hashBlock setter internally for CCoinsViewCache methods (Andrew Toth)
8dd9200fc9 coins: 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:
    ACK 3e0fd0e4dd
  achow101:
    ACK 3e0fd0e4dd
  sedited:
    ACK 3e0fd0e4dd

Tree-SHA512: a95feaa062a9eb7cf7514425a7e7adffd347cd1f7b32b4c1fefcde30002141757c184174702b3104a029dcd33194f8bd734159deebb2e668716089305b42cb00
This commit is contained in:
Ava Chow
2026-01-29 14:59:36 -08:00
7 changed files with 116 additions and 11 deletions

View File

@@ -1856,6 +1856,7 @@ void CoinsViews::InitCache()
{
AssertLockHeld(::cs_main);
m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
m_connect_block_view = std::make_unique<CCoinsViewCache>(&*m_cacheview);
}
Chainstate::Chainstate(
@@ -2956,7 +2957,7 @@ bool Chainstate::DisconnectTip(BlockValidationState& state, DisconnectedBlockTra
LogError("DisconnectTip(): DisconnectBlock %s failed\n", pindexDelete->GetBlockHash().ToString());
return false;
}
view.Flush(/*will_reuse_cache=*/false); // local CCoinsViewCache goes out of scope
view.Flush(/*reallocate_cache=*/false); // local CCoinsViewCache goes out of scope
}
LogDebug(BCLog::BENCH, "- Disconnect block: %.2fms\n",
Ticks<MillisecondsDouble>(SteadyClock::now() - time_start));
@@ -3073,7 +3074,8 @@ bool Chainstate::ConnectTip(
LogDebug(BCLog::BENCH, " - Load block from disk: %.2fms\n",
Ticks<MillisecondsDouble>(time_2 - time_1));
{
CCoinsViewCache view(&CoinsTip());
CCoinsViewCache& view{*m_coins_views->m_connect_block_view};
const auto reset_guard{view.CreateResetGuard()};
bool rv = ConnectBlock(*block_to_connect, state, pindexNew, view);
if (m_chainman.m_options.signals) {
m_chainman.m_options.signals->BlockChecked(block_to_connect, state);
@@ -3091,7 +3093,7 @@ bool Chainstate::ConnectTip(
Ticks<MillisecondsDouble>(time_3 - time_2),
Ticks<SecondsDouble>(m_chainman.time_connect_total),
Ticks<MillisecondsDouble>(m_chainman.time_connect_total) / m_chainman.num_blocks_total);
view.Flush(/*will_reuse_cache=*/false); // local CCoinsViewCache goes out of scope
view.Flush(/*reallocate_cache=*/false); // No need to reallocate since it only has capacity for 1 block
}
const auto time_4{SteadyClock::now()};
m_chainman.time_flush += time_4 - time_3;
@@ -4904,7 +4906,7 @@ bool Chainstate::ReplayBlocks()
}
cache.SetBestBlock(pindexNew->GetBlockHash());
cache.Flush(/*will_reuse_cache=*/false); // local CCoinsViewCache goes out of scope
cache.Flush(/*reallocate_cache=*/false); // local CCoinsViewCache goes out of scope
m_chainman.GetNotifications().progress(bilingual_str{}, 100, false);
return true;
}