Skip to content

Commit eb2e113

Browse files
mzumsandejnewbery
andcommitted
addrman: Improve performance of Good
This is done by removing an unnecessary loop in Good_() and looping through the new tables in MakeTried() more efficiently, choosing a starting value that allow us to stop early in typical cases. Co-authored-by: John Newbery <[email protected]>
1 parent 2161a05 commit eb2e113

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
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);

0 commit comments

Comments
 (0)