diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2020-04-20 17:11:08 +0200 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2021-05-28 16:40:15 +0200 |
commit | a92485b2c250fd18f55d22aa32722bf52ab32bfe (patch) | |
tree | 152c562ce3b0ac2e95f4b84061a3b05cf298506b /src/netaddress.h | |
parent | 8115c2ad7dc87cc37662421875b728ffc29aaffd (diff) |
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/netaddress.h')
-rw-r--r-- | src/netaddress.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/netaddress.h b/src/netaddress.h index 897ce46cda..5c2d68150b 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> @@ -254,6 +256,7 @@ class CNetAddr } } + friend class CNetAddrHash; friend class CSubNet; private: @@ -477,6 +480,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: |