Skip to content

Commit 153e686

Browse files
committed
bench: change AddrManGood to AddrManAddThenGood
Moves some of the setup into the benchmark loop so it is possible to run the loop for an arbitrary number of times. Due to recent optimizations in #22974 the benchmark now runs much faster, so the inner loop now calls Good() 32 times as often to get better numbers. Renamed the benchmark to AddrManAddThenGood because that's now what is actually tested. To get the the number of just Good(), one needs to subtract the benchmark result of AddrManAdd.
1 parent 468b232 commit 153e686

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

src/bench/addrman.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,41 +103,33 @@ static void AddrManGetAddr(benchmark::Bench& bench)
103103
});
104104
}
105105

106-
static void AddrManGood(benchmark::Bench& bench)
106+
static void AddrManAddThenGood(benchmark::Bench& bench)
107107
{
108-
/* Create many CAddrMan objects - one to be modified at each loop iteration.
109-
* This is necessary because the CAddrMan::Good() method modifies the
110-
* object, affecting the timing of subsequent calls to the same method and
111-
* we want to do the same amount of work in every loop iteration. */
112-
113-
bench.epochs(5).epochIterations(1);
114-
const uint64_t addrman_count{bench.epochs() * bench.epochIterations()};
115-
Assert(addrman_count == 5U);
116-
117-
std::vector<std::unique_ptr<CAddrMan>> addrmans(addrman_count);
118-
for (size_t i{0}; i < addrman_count; ++i) {
119-
addrmans[i] = std::make_unique<CAddrMan>(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
120-
FillAddrMan(*addrmans[i]);
121-
}
122-
123108
auto markSomeAsGood = [](CAddrMan& addrman) {
124109
for (size_t source_i = 0; source_i < NUM_SOURCES; ++source_i) {
125110
for (size_t addr_i = 0; addr_i < NUM_ADDRESSES_PER_SOURCE; ++addr_i) {
126-
if (addr_i % 32 == 0) {
127-
addrman.Good(g_addresses[source_i][addr_i]);
128-
}
111+
addrman.Good(g_addresses[source_i][addr_i]);
129112
}
130113
}
131114
};
132115

133-
uint64_t i = 0;
116+
CreateAddresses();
117+
134118
bench.run([&] {
135-
markSomeAsGood(*addrmans.at(i));
136-
++i;
119+
// To make the benchmark independent of the number of evaluations, we always prepare a new addrman.
120+
// This is necessary because CAddrMan::Good() method modifies the object, affecting the timing of subsequent calls
121+
// to the same method and we want to do the same amount of work in every loop iteration.
122+
//
123+
// This has some overhead (exactly the result of AddrManAdd benchmark), but that overhead is constant so improvements in
124+
// CAddrMan::Good() will still be noticeable.
125+
CAddrMan addrman(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
126+
AddAddressesToAddrMan(addrman);
127+
128+
markSomeAsGood(addrman);
137129
});
138130
}
139131

140132
BENCHMARK(AddrManAdd);
141133
BENCHMARK(AddrManSelect);
142134
BENCHMARK(AddrManGetAddr);
143-
BENCHMARK(AddrManGood);
135+
BENCHMARK(AddrManAddThenGood);

0 commit comments

Comments
 (0)