Skip to content

Commit e064487

Browse files
committed
addrman, refactor: improve stochastic test in AddSingle
This commit changes this algo to be O(1) instead of O(n) by using `<<`.
1 parent c9f2882 commit e064487

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/addrman.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,10 @@ bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, std::c
585585
return false;
586586

587587
// stochastic test: previous nRefCount == N: 2^N times harder to increase it
588-
int nFactor = 1;
589-
for (int n = 0; n < pinfo->nRefCount; n++)
590-
nFactor *= 2;
591-
if (nFactor > 1 && (insecure_rand.randrange(nFactor) != 0))
592-
return false;
588+
if (pinfo->nRefCount > 0) {
589+
const int nFactor{1 << pinfo->nRefCount};
590+
if (insecure_rand.randrange(nFactor) != 0) return false;
591+
}
593592
} else {
594593
pinfo = Create(addr, source, &nId);
595594
pinfo->nTime = std::max(NodeSeconds{0s}, pinfo->nTime - time_penalty);

0 commit comments

Comments
 (0)