bench: add on-disk HaveInputs benchmark

`CCoinsViewCache::HaveInputs()` is used by `Consensus::CheckTxInputs()` and currently exercises the `HaveCoin()` path in the UTXO cache.
Performance could be affected due to `Coin` copying and different cache warming behavior.

Add a benchmark to track `HaveInputs()` throughput before and after the `HaveCoin()` changes in this series to make sure the performance is retained.
The benchmark populates an on-disk `CCoinsViewDB` with dummy unspent coins for all block inputs and then calls `HaveInputs()` for each non-coinbase transaction using a fresh `CCoinsViewCache` (to avoid cross-iteration caching).
It calls `HaveInputs()` twice to exercise the cache-hit path as well.

Benchmark baseline:
|               ns/tx |                tx/s |    err% |     total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
|            1,004.58 |          995,437.07 |    0.7% |      1.10 | `HaveInputsOnDisk`
This commit is contained in:
Lőrinc
2026-01-25 15:00:49 +01:00
parent b65ff0e5a1
commit 0ca4295f2e
2 changed files with 49 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ add_executable(bench_bitcoin
checkblockindex.cpp
checkqueue.cpp
cluster_linearize.cpp
coins_haveinputs.cpp
connectblock.cpp
crypto_hash.cpp
descriptors.cpp

View File

@@ -0,0 +1,48 @@
// Copyright (c) 2026-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://opensource.org/license/mit/.
#include <bench/bench.h>
#include <bench/data/block413567.raw.h>
#include <coins.h>
#include <consensus/amount.h>
#include <primitives/block.h>
#include <script/script.h>
#include <streams.h>
#include <test/util/setup_common.h>
#include <txdb.h>
#include <util/fs.h>
#include <cassert>
#include <ranges>
static void HaveInputsOnDisk(benchmark::Bench& bench)
{
const auto testing_setup{MakeNoLogFileContext<const BasicTestingSetup>(ChainType::REGTEST)};
const auto path{testing_setup->m_path_root / fs::PathFromString(bench.name())};
CCoinsViewDB db{{.path = path, .cache_bytes = 1_MiB, .memory_only = false, .wipe_data = true}, {}};
CBlock block;
DataStream{benchmark::data::block413567} >> TX_WITH_WITNESS(block);
const uint256 best_block{block.GetHash()};
CCoinsViewCache cache{&db};
cache.SetBestBlock(best_block);
for (auto& tx : block.vtx | std::views::drop(1)) {
for (auto& txin : tx->vin) {
cache.AddCoin(txin.prevout, {{COIN, CScript()}, /*nHeightIn=*/1, /*fCoinBaseIn=*/false}, /*possible_overwrite=*/false);
}
}
cache.Flush();
bench.batch(block.vtx.size() - 1).unit("tx").run([&] {
CCoinsViewCache view{&db}; // Recreate to avoid caching
view.SetBestBlock(best_block);
for (auto& tx : block.vtx | std::views::drop(1)) {
assert(view.HaveInputs(*tx));
assert(view.HaveInputs(*tx)); // Exercise the cache-hit path as well.
}
});
}
BENCHMARK(HaveInputsOnDisk);