aboutsummaryrefslogtreecommitdiff
path: root/src/addrman.h
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.h
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.h')
-rw-r--r--src/addrman.h22
1 files changed, 11 insertions, 11 deletions
diff --git a/src/addrman.h b/src/addrman.h
index 41994288db..4929fd2ecf 100644
--- a/src/addrman.h
+++ b/src/addrman.h
@@ -8,22 +8,22 @@
#include <clientversion.h>
#include <config/bitcoin-config.h>
+#include <fs.h>
+#include <hash.h>
#include <netaddress.h>
#include <protocol.h>
#include <random.h>
+#include <streams.h>
#include <sync.h>
#include <timedata.h>
#include <tinyformat.h>
#include <util/system.h>
-#include <fs.h>
-#include <hash.h>
#include <iostream>
-#include <map>
#include <optional>
#include <set>
#include <stdint.h>
-#include <streams.h>
+#include <unordered_map>
#include <vector>
/**
@@ -251,7 +251,7 @@ public:
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
s << nUBuckets;
- std::map<int, int> mapUnkIds;
+ std::unordered_map<int, int> mapUnkIds;
int nIds = 0;
for (const auto& entry : mapInfo) {
mapUnkIds[entry.first] = nIds;
@@ -435,13 +435,13 @@ public:
// Prune new entries with refcount 0 (as a result of collisions).
int nLostUnk = 0;
- for (std::map<int, CAddrInfo>::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<int, CAddrInfo>::const_iterator itCopy = it++;
+ const auto itCopy = it++;
Delete(itCopy->first);
- nLostUnk++;
+ ++nLostUnk;
} else {
- it++;
+ ++it;
}
}
if (nLost + nLostUnk > 0) {
@@ -662,10 +662,10 @@ private:
int nIdCount GUARDED_BY(cs);
//! table with information about all nIds
- std::map<int, CAddrInfo> mapInfo GUARDED_BY(cs);
+ std::unordered_map<int, CAddrInfo> mapInfo GUARDED_BY(cs);
//! find an nId based on its network address
- std::map<CNetAddr, int> mapAddr GUARDED_BY(cs);
+ std::unordered_map<CNetAddr, int, CNetAddrHash> mapAddr GUARDED_BY(cs);
//! randomly-ordered vector of all nIds
std::vector<int> vRandom GUARDED_BY(cs);