diff --git a/src/addrman.h b/src/addrman.h --- a/src/addrman.h +++ b/src/addrman.h @@ -22,9 +22,9 @@ #include #include -#include #include #include +#include #include /** @@ -227,10 +227,10 @@ int nIdCount GUARDED_BY(cs); //! table with information about all nIds - std::map mapInfo GUARDED_BY(cs); + std::unordered_map mapInfo GUARDED_BY(cs); //! find an nId based on its network address - std::map mapAddr GUARDED_BY(cs); + std::unordered_map mapAddr GUARDED_BY(cs); //! randomly-ordered vector of all nIds std::vector vRandom GUARDED_BY(cs); @@ -430,7 +430,7 @@ int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30); s << nUBuckets; - std::map mapUnkIds; + std::unordered_map mapUnkIds; int nIds = 0; for (const auto &entry : mapInfo) { mapUnkIds[entry.first] = nIds; @@ -611,14 +611,13 @@ // Prune new entries with refcount 0 (as a result of collisions). int nLostUnk = 0; - for (std::map::const_iterator it = mapInfo.begin(); - it != mapInfo.end();) { + for (auto it = mapInfo.cbegin(); it != mapInfo.cend();) { if (it->second.fInTried == false && it->second.nRefCount == 0) { - std::map::const_iterator itCopy = it++; + const auto itCopy = it++; Delete(itCopy->first); - nLostUnk++; + ++nLostUnk; } else { - it++; + ++it; } } if (nLost + nLostUnk > 0) { diff --git a/src/addrman.cpp b/src/addrman.cpp --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -13,6 +13,8 @@ #include #include +#include +#include int CAddrInfo::GetTriedBucket(const uint256 &nKey, const std::vector &asmap) const { @@ -101,14 +103,14 @@ } CAddrInfo *CAddrMan::Find(const CNetAddr &addr, int *pnId) { - std::map::iterator it = mapAddr.find(addr); + const auto it = mapAddr.find(addr); if (it == mapAddr.end()) { return nullptr; } if (pnId) { *pnId = (*it).second; } - std::map::iterator it2 = mapInfo.find((*it).second); + const auto it2 = mapInfo.find((*it).second); if (it2 != mapInfo.end()) { return &(*it2).second; } @@ -464,8 +466,8 @@ #ifdef DEBUG_ADDRMAN int CAddrMan::Check_() { - std::set setTried; - std::map mapNew; + std::unordered_set setTried; + std::unordered_map mapNew; if (vRandom.size() != size_t(nTried + nNew)) { return -7; diff --git a/src/netaddress.h b/src/netaddress.h --- a/src/netaddress.h +++ b/src/netaddress.h @@ -11,7 +11,9 @@ #include #include +#include #include +#include #include #include #include @@ -273,6 +275,7 @@ } } + friend class CNetAddrHash; friend class CSubNet; private: @@ -492,6 +495,20 @@ } }; +class CNetAddrHash { +public: + size_t operator()(const CNetAddr &a) const noexcept { + CSipHasher hasher(m_salt_k0, m_salt_k1); + hasher.Write(a.m_net); + hasher.Write(a.m_addr.data(), a.m_addr.size()); + return static_cast(hasher.Finalize()); + } + +private: + const uint64_t m_salt_k0 = GetRand(std::numeric_limits::max()); + const uint64_t m_salt_k1 = GetRand(std::numeric_limits::max()); +}; + class CSubNet { protected: /// Network (base) address