Skip to content

Commit 1698336

Browse files
committed
merge bitcoin#22974: Improve performance of Good
1 parent 29f4482 commit 1698336

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>
@@ -503,11 +504,14 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId)
503504
AssertLockHeld(cs);
504505

505506
// remove the entry from all new buckets
506-
for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
507-
int pos = info.GetBucketPosition(nKey, true, bucket);
507+
const int start_bucket{info.GetNewBucket(nKey, m_asmap)};
508+
for (int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; ++n) {
509+
const int bucket{(start_bucket + n) % ADDRMAN_NEW_BUCKET_COUNT};
510+
const int pos{info.GetBucketPosition(nKey, true, bucket)};
508511
if (vvNew[bucket][pos] == nId) {
509512
vvNew[bucket][pos] = -1;
510513
info.nRefCount--;
514+
if (info.nRefCount == 0) break;
511515
}
512516
}
513517
nNew--;
@@ -579,22 +583,10 @@ void CAddrMan::Good_(const CService& addr, bool test_before_evict, int64_t nTime
579583
if (info.fInTried)
580584
return;
581585

582-
// find a bucket it is in now
583-
int nRnd = insecure_rand.randrange(ADDRMAN_NEW_BUCKET_COUNT);
584-
int nUBucket = -1;
585-
for (unsigned int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; n++) {
586-
int nB = (n + nRnd) % ADDRMAN_NEW_BUCKET_COUNT;
587-
int nBpos = info.GetBucketPosition(nKey, true, nB);
588-
if (vvNew[nB][nBpos] == nId) {
589-
nUBucket = nB;
590-
break;
591-
}
592-
}
593-
594-
// if no bucket is found, something bad happened;
595-
// TODO: maybe re-add the node, but for now, just bail out
596-
if (nUBucket == -1)
586+
// if it is not in new, something bad happened
587+
if (!Assume(info.nRefCount > 0)) {
597588
return;
589+
}
598590

599591
// which tried bucket to move the entry to
600592
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)