aboutsummaryrefslogtreecommitdiff
path: root/src/netaddress.h
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2021-06-12 11:40:34 +0800
committerfanquake <fanquake@gmail.com>2021-06-12 11:41:27 +0800
commit1a369f006fd0bec373b95001ed84b480e852f191 (patch)
treefb7a7e831bac9cac859e1b26b783c78596a2cc5b /src/netaddress.h
parent9795e8ec8c279e0df34aee7241bc3b5720a5b380 (diff)
parenta92485b2c250fd18f55d22aa32722bf52ab32bfe (diff)
downloadbitcoin-1a369f006fd0bec373b95001ed84b480e852f191.tar.xz
Merge bitcoin/bitcoin#18722: addrman: improve performance by using more suitable containers
a92485b2c250fd18f55d22aa32722bf52ab32bfe addrman: use unordered_map instead of map (Vasil Dimov) Pull request description: `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% ``` ACKs for top commit: jonatack: ACK a92485b2c250fd18f55d22aa32722bf52ab32bfe achow101: ACK a92485b2c250fd18f55d22aa32722bf52ab32bfe hebasto: re-ACK a92485b2c250fd18f55d22aa32722bf52ab32bfe, only suggested changes and rebased since my [previous](https://github.com/bitcoin/bitcoin/pull/18722#pullrequestreview-666663681) review. Tree-SHA512: d82959a00e6bd68a6c4c5a265dd08849e6602ac3231293b7a3a3b7bf82ab1d3ba77f8ca682919c15c5d601b13e468b8836fcf19595248116635f7a50d02ed603
Diffstat (limited to 'src/netaddress.h')
-rw-r--r--src/netaddress.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/netaddress.h b/src/netaddress.h
index 0d04ab88fb..dd47ab5749 100644
--- a/src/netaddress.h
+++ b/src/netaddress.h
@@ -11,7 +11,9 @@
#include <attributes.h>
#include <compat.h>
+#include <crypto/siphash.h>
#include <prevector.h>
+#include <random.h>
#include <serialize.h>
#include <tinyformat.h>
#include <util/strencodings.h>
@@ -251,6 +253,7 @@ class CNetAddr
}
}
+ friend class CNetAddrHash;
friend class CSubNet;
private:
@@ -464,6 +467,22 @@ class CNetAddr
}
};
+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<size_t>(hasher.Finalize());
+ }
+
+private:
+ const uint64_t m_salt_k0 = GetRand(std::numeric_limits<uint64_t>::max());
+ const uint64_t m_salt_k1 = GetRand(std::numeric_limits<uint64_t>::max());
+};
+
class CSubNet
{
protected: