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.:
0diff --git a/src/bench/rpc_blockchain.cpp b/src/bench/rpc_blockchain.cpp
1--- a/src/bench/rpc_blockchain.cpp (revision bf5c569898d0297de010102a623bf52009607ed8)
2+++ b/src/bench/rpc_blockchain.cpp (date 1735836623994)
3@@ -24,7 +24,6 @@
4 namespace {
5
6 struct TestBlockAndIndex {
7- const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
8 CBlock block{};
9 uint256 blockHash{};
10 CBlockIndex blockindex{};
11@@ -47,9 +46,10 @@
12
13 static void BlockToJson(benchmark::Bench& bench, TxVerbosity verbosity)
14 {
15- TestBlockAndIndex data;
16+ const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
17 bench.run([&] {
18- auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, verbosity);
19+ TestBlockAndIndex data{};
20+ auto univalue = blockToJSON(testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, verbosity);
21 ankerl::nanobench::doNotOptimizeAway(univalue);
22 });
23 }
24@@ -75,8 +75,9 @@
25
26 static void BlockToJsonVerboseWrite(benchmark::Bench& bench)
27 {
28+ const std::unique_ptr<const TestingSetup> testing_setup{MakeNoLogFileContext<const TestingSetup>(ChainType::MAIN)};
29 TestBlockAndIndex data;
30- auto univalue = blockToJSON(data.testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
31+ auto univalue = blockToJSON(testing_setup->m_node.chainman->m_blockman, data.block, data.blockindex, data.blockindex, TxVerbosity::SHOW_DETAILS_AND_PREVOUT);
32 bench.run([&] {
33 auto str = univalue.write();
34 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.