Skip to content

Commit 4d2fa97

Browse files
committed
[addrman] Clean up ctor
Use default initialization and initializer lists, and use range-based for loops for resetting the buckets.
1 parent 7e6e659 commit 4d2fa97

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

src/addrman.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,19 @@ double CAddrInfo::GetChance(int64_t nNow) const
7979

8080
CAddrMan::CAddrMan(bool deterministic, int32_t consistency_check_ratio)
8181
: insecure_rand{deterministic}
82+
, nKey{deterministic ? uint256{1} : insecure_rand.rand256()}
8283
, m_consistency_check_ratio{consistency_check_ratio}
8384
{
84-
std::vector<int>().swap(vRandom);
85-
nKey = insecure_rand.rand256();
86-
for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
87-
for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
88-
vvNew[bucket][entry] = -1;
85+
for (auto& bucket : vvNew) {
86+
for (auto& entry : bucket) {
87+
entry = -1;
8988
}
9089
}
91-
for (size_t bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; bucket++) {
92-
for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
93-
vvTried[bucket][entry] = -1;
90+
for (auto& bucket : vvTried) {
91+
for (auto& entry : bucket) {
92+
entry = -1;
9493
}
9594
}
96-
97-
nIdCount = 0;
98-
nTried = 0;
99-
nNew = 0;
100-
nLastGood = 1; //Initially at 1 so that "never" is strictly worse.
101-
mapInfo.clear();
102-
mapAddr.clear();
103-
if (deterministic) nKey = uint256{1};
10495
}
10596

10697
CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)

src/addrman.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ class CAddrMan
626626
static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
627627

628628
//! last used nId
629-
int nIdCount GUARDED_BY(cs);
629+
int nIdCount GUARDED_BY(cs){0};
630630

631631
//! table with information about all nIds
632632
std::unordered_map<int, CAddrInfo> mapInfo GUARDED_BY(cs);
@@ -640,19 +640,19 @@ class CAddrMan
640640
mutable std::vector<int> vRandom GUARDED_BY(cs);
641641

642642
// number of "tried" entries
643-
int nTried GUARDED_BY(cs);
643+
int nTried GUARDED_BY(cs){0};
644644

645645
//! list of "tried" buckets
646646
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
647647

648648
//! number of (unique) "new" entries
649-
int nNew GUARDED_BY(cs);
649+
int nNew GUARDED_BY(cs){0};
650650

651651
//! list of "new" buckets
652652
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs);
653653

654-
//! last time Good was called (memory only)
655-
int64_t nLastGood GUARDED_BY(cs);
654+
//! last time Good was called (memory only). Initially set to 1 so that "never" is strictly worse.
655+
int64_t nLastGood GUARDED_BY(cs){1};
656656

657657
//! Holds addrs inserted into tried table that collide with existing entries. Test-before-evict discipline used to resolve these collisions.
658658
std::set<int> m_tried_collisions;

0 commit comments

Comments
 (0)