aboutsummaryrefslogtreecommitdiff
path: root/src/addrman.cpp
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2020-04-20 17:11:08 +0200
committerVasil Dimov <vd@FreeBSD.org>2021-05-28 16:40:15 +0200
commita92485b2c250fd18f55d22aa32722bf52ab32bfe (patch)
tree152c562ce3b0ac2e95f4b84061a3b05cf298506b /src/addrman.cpp
parent8115c2ad7dc87cc37662421875b728ffc29aaffd (diff)
downloadbitcoin-a92485b2c250fd18f55d22aa32722bf52ab32bfe.tar.xz
addrman: use unordered_map instead of map
`CAddrMan` uses `std::map` internally even though it does not require that the map's elements are sorted. `std::map`'s access time is `O(log(map size))`. `std::unordered_map` is more suitable as it has a `O(1)` access time. This patch lowers the execution times of `CAddrMan`'s methods as follows (as per `src/bench/addrman.cpp`): ``` AddrMan::Add(): -3.5% AddrMan::GetAddr(): -76% AddrMan::Good(): -0.38% AddrMan::Select(): -45% ```
Diffstat (limited to 'src/addrman.cpp')
-rw-r--r--src/addrman.cpp10
1 files changed, 6 insertions, 4 deletions
diff --git a/src/addrman.cpp b/src/addrman.cpp
index ceab1689d7..bc5d87e5f1 100644
--- a/src/addrman.cpp
+++ b/src/addrman.cpp
@@ -12,6 +12,8 @@
#include <cmath>
#include <optional>
+#include <unordered_map>
+#include <unordered_set>
int CAddrInfo::GetTriedBucket(const uint256& nKey, const std::vector<bool> &asmap) const
{
@@ -77,12 +79,12 @@ double CAddrInfo::GetChance(int64_t nNow) const
CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)
{
- std::map<CNetAddr, int>::iterator it = mapAddr.find(addr);
+ const auto it = mapAddr.find(addr);
if (it == mapAddr.end())
return nullptr;
if (pnId)
*pnId = (*it).second;
- std::map<int, CAddrInfo>::iterator it2 = mapInfo.find((*it).second);
+ const auto it2 = mapInfo.find((*it).second);
if (it2 != mapInfo.end())
return &(*it2).second;
return nullptr;
@@ -408,8 +410,8 @@ CAddrInfo CAddrMan::Select_(bool newOnly)
#ifdef DEBUG_ADDRMAN
int CAddrMan::Check_()
{
- std::set<int> setTried;
- std::map<int, int> mapNew;
+ std::unordered_set<int> setTried;
+ std::unordered_map<int, int> mapNew;
if (vRandom.size() != (size_t)(nTried + nNew))
return -7;