I will leave that as an idea for a follow-up PR.
The problem is that the goal of this PR was to speed up the RPC.
But if we benchmark that instead of just a subset of the task, i.e.:
diff --git a/src/bench/rpc_blockchain.cpp b/src/bench/rpc_blockchain.cpp
--- a/src/bench/rpc_blockchain.cpp (revision bf5c569898d0297de010102a623bf52009607ed8)
+++ b/src/bench/rpc_blockchain.cpp (date 1735836623994)
@@ -24,7 +24,6 @@
namespace {
struct TestBlockAndIndex {
- const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
CBlock block{};
uint256 blockHash{};
CBlockIndex blockindex{};
@@ -47,9 +46,10 @@
static void BlockToJson(benchmark::Bench& bench, TxVerbosity verbosity)
{
- TestBlockAndIndex data;
+ const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
bench.run([&] {
- auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, verbosity);
+ TestBlockAndIndex data{};
+ auto univalue = blockToJSON(testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, verbosity);
ankerl::nanobench::doNotOptimizeAway(univalue);
});
}
@@ -75,8 +75,9 @@
static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
{
+ const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
TestBlockAndIndex data;
- auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
+ auto univalue = blockToJSON(testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
bench.run([&] {
auto str = univalue.write();
ankerl::nanobench::doNotOptimizeAway(str);
via
build/src/bench/bench_bitcoin -filter='BlockToJsonVerbosity.*' -min-time=10000
unfortunately it reveals:
Before:
| ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 1,317,881.88 | 758.79 | 0.3% | 10.99 | BlockToJsonVerbosity1
| 26,534,215.46 | 37.69 | 0.3% | 10.95 | BlockToJsonVerbosity2
| 26,486,601.35 | 37.75 | 0.1% | 10.95 | BlockToJsonVerbosity3
After:
| ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 1,313,779.11 | 761.16 | 0.6% | 11.00 | BlockToJsonVerbosity1
| 26,128,170.14 | 38.27 | 0.2% | 11.02 | BlockToJsonVerbosity2
| 26,143,108.35 | 38.25 | 0.2% | 11.03 | BlockToJsonVerbosity3
i.e. barely any speedup for the RPC.