Skip to content

Commit 7f7bd31

Browse files
committed
Merge bitcoin/bitcoin#22974: addrman: Improve performance of Good
57ce203 fuzz: allow lower number of sources (Martin Zumsande) acf656d fuzz: Use public interface to fill addrman tried tables (Martin Zumsande) eb2e113 addrman: Improve performance of Good (Martin Zumsande) Pull request description: Currently, `CAddrman::Good()` is rather slow because the process of moving an addr from new to tried involves looping over the new tables twice: 1) In `Good_()`, there is a loop searching for a new bucket the addr is currently in, but this information is never used except for aborting if it is not found anywhere (since [this commit](bitcoin/bitcoin@e6b343d#diff-49d1faa58beca1ee1509a247e0331bb91f8604e30a483a7b2dea813e6cea02e2R263) it is no longer passed to `MakeTried`) This is unnecessary because in a non-corrupted addrman, an address that is not in New must be either in Tried or not at all in addrman, both cases in which we'd return early in `Good_()` and never get to this point. I removed this loop (and left a check for `nRefCount` as a belt-and-suspenders check). 2) In `MakeTried()`, which is called from `Good_()`, another loop removes all instances of this address from new. This can be spedup by stopping the search at `nRefCount==0`. Further reductions in `nRefCount` would only lead to an assert anyway. Moreover, the search can be started at the bucket determined by the source of the addr for which `Good` was called, so that if it is present just once in New, no further buckets need to be checked. While calls to `Good()` are not that frequent normally, the performance gain is clearly seen in the fuzz target `addman_serdeser`, where, because of the slowness in creating a decently filled addrman, a shortcut was created that would directly populate the tried tables by reaching into addrman's internals, bypassing `Good()` (#21129). I removed this workaround in the second commit: Using `Good()` is still slower by a factor of 2 (down from a factor of ~60 before), but I think that this compensated by the advantages of not having to reach into the internal structures of addrman (see jnewbery/bitcoin#18 (comment)). [Edit]: For benchmark results see bitcoin/bitcoin#22974 (comment) and bitcoin/bitcoin#22974 (comment) - the benchmark `AddrManGood` shows a significant speedup by a factor >100. ACKs for top commit: naumenkogs: ACK 57ce203 jnewbery: ACK 57ce203 laanwj: Code review ACK 57ce203 theStack: ACK 57ce203 vasild: ACK 57ce203 Tree-SHA512: fb6dfc198f2e28bdbb41cef9709828f22d83b4be0e640a3155ca42e771b6f58466de1468f54d773e794f780a79113f9f7d522032e87fdd75bdc4d99330445198
2 parents 488e745 + 57ce203 commit 7f7bd31

File tree

2 files changed

+12
-39
lines changed

2 files changed

+12
-39
lines changed

src/addrman.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <netaddress.h>
1212
#include <serialize.h>
1313
#include <streams.h>
14+
#include <util/check.h>
1415

1516
#include <cmath>
1617
#include <optional>
@@ -488,11 +489,14 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId)
488489
AssertLockHeld(cs);
489490

490491
// remove the entry from all new buckets
491-
for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
492-
int pos = info.GetBucketPosition(nKey, true, bucket);
492+
const int start_bucket{info.GetNewBucket(nKey, m_asmap)};
493+
for (int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; ++n) {
494+
const int bucket{(start_bucket + n) % ADDRMAN_NEW_BUCKET_COUNT};
495+
const int pos{info.GetBucketPosition(nKey, true, bucket)};
493496
if (vvNew[bucket][pos] == nId) {
494497
vvNew[bucket][pos] = -1;
495498
info.nRefCount--;
499+
if (info.nRefCount == 0) break;
496500
}
497501
}
498502
nNew--;
@@ -564,22 +568,10 @@ void CAddrMan::Good_(const CService& addr, bool test_before_evict, int64_t nTime
564568
if (info.fInTried)
565569
return;
566570

567-
// find a bucket it is in now
568-
int nRnd = insecure_rand.randrange(ADDRMAN_NEW_BUCKET_COUNT);
569-
int nUBucket = -1;
570-
for (unsigned int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; n++) {
571-
int nB = (n + nRnd) % ADDRMAN_NEW_BUCKET_COUNT;
572-
int nBpos = info.GetBucketPosition(nKey, true, nB);
573-
if (vvNew[nB][nBpos] == nId) {
574-
nUBucket = nB;
575-
break;
576-
}
577-
}
578-
579-
// if no bucket is found, something bad happened;
580-
// TODO: maybe re-add the node, but for now, just bail out
581-
if (nUBucket == -1)
571+
// if it is not in new, something bad happened
572+
if (!Assume(info.nRefCount > 0)) {
582573
return;
574+
}
583575

584576
// which tried bucket to move the entry to
585577
int tried_bucket = info.GetTriedBucket(nKey, m_asmap);

src/test/fuzz/addrman.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class CAddrManDeterministic : public CAddrMan
8585
// 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
8686
const size_t n = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
8787

88-
const size_t num_sources = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(10, 50);
88+
const size_t num_sources = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
8989
CNetAddr prev_source;
9090
// Use insecure_rand inside the loops instead of m_fuzzed_data_provider because when
9191
// the latter is exhausted it just returns 0.
@@ -96,31 +96,12 @@ class CAddrManDeterministic : public CAddrMan
9696
for (size_t j = 0; j < num_addresses; ++j) {
9797
const auto addr = CAddress{CService{RandAddr(), 8333}, NODE_NETWORK};
9898
const auto time_penalty = insecure_rand.randrange(100000001);
99-
#if 1
100-
// 2.83 sec to fill.
101-
if (n > 0 && mapInfo.size() % n == 0 && mapAddr.find(addr) == mapAddr.end()) {
102-
// Add to the "tried" table (if the bucket slot is free).
103-
const CAddrInfo dummy{addr, source};
104-
const int bucket = dummy.GetTriedBucket(nKey, m_asmap);
105-
const int bucket_pos = dummy.GetBucketPosition(nKey, false, bucket);
106-
if (vvTried[bucket][bucket_pos] == -1) {
107-
int id;
108-
CAddrInfo* addr_info = Create(addr, source, &id);
109-
vvTried[bucket][bucket_pos] = id;
110-
addr_info->fInTried = true;
111-
++nTried;
112-
}
113-
} else {
114-
// Add to the "new" table.
115-
Add_(addr, source, time_penalty);
116-
}
117-
#else
118-
// 261.91 sec to fill.
11999
Add_(addr, source, time_penalty);
100+
120101
if (n > 0 && mapInfo.size() % n == 0) {
121102
Good_(addr, false, GetTime());
122103
}
123-
#endif
104+
124105
// Add 10% of the addresses from more than one source.
125106
if (insecure_rand.randrange(10) == 0 && prev_source.IsValid()) {
126107
Add_(addr, prev_source, time_penalty);

0 commit comments

Comments
 (0)