From 95e61c1cf2a91d041c8025306ba36f0ea2806894 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Tue, 17 Sep 2019 17:02:56 -0400 Subject: [PATCH 1/3] Move Hashers to util/hasher.{cpp/h} Move the hashers that we use for hash tables to a common place. Moved hashers: - SaltedTxidHasher - SaltedOutpointHasher - FilterHeaderHasher - SignatureCacheHasher - BlockHasher --- src/Makefile.am | 2 + src/coins.cpp | 2 - src/coins.h | 29 +----------- src/index/blockfilterindex.h | 6 +-- src/script/sigcache.h | 22 +-------- src/txmempool.cpp | 2 - src/txmempool.h | 16 +------ src/util/hasher.cpp | 12 +++++ src/util/hasher.h | 87 ++++++++++++++++++++++++++++++++++++ src/validation.h | 9 +--- 10 files changed, 106 insertions(+), 81 deletions(-) create mode 100644 src/util/hasher.cpp create mode 100644 src/util/hasher.h diff --git a/src/Makefile.am b/src/Makefile.am index 67fd402603..a0ba756d99 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -224,6 +224,7 @@ BITCOIN_CORE_H = \ util/error.h \ util/fees.h \ util/golombrice.h \ + util/hasher.h \ util/macros.h \ util/memory.h \ util/message.h \ @@ -539,6 +540,7 @@ libbitcoin_util_a_SOURCES = \ util/bytevectorhash.cpp \ util/error.cpp \ util/fees.cpp \ + util/hasher.cpp \ util/system.cpp \ util/message.cpp \ util/moneystr.cpp \ diff --git a/src/coins.cpp b/src/coins.cpp index 5de2ed7810..abba61d3d2 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -31,8 +31,6 @@ bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); } size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); } -SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits::max())), k1(GetRand(std::numeric_limits::max())) {} - CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), cachedCoinsUsage(0) {} size_t CCoinsViewCache::DynamicMemoryUsage() const { diff --git a/src/coins.h b/src/coins.h index a3e241ac90..d2eb42d8cf 100644 --- a/src/coins.h +++ b/src/coins.h @@ -8,11 +8,11 @@ #include #include -#include #include #include #include #include +#include #include #include @@ -82,33 +82,6 @@ public: } }; -class SaltedOutpointHasher -{ -private: - /** Salt */ - const uint64_t k0, k1; - -public: - SaltedOutpointHasher(); - - /** - * This *must* return size_t. With Boost 1.46 on 32-bit systems the - * unordered_map will behave unpredictably if the custom hasher returns a - * uint64_t, resulting in failures when syncing the chain (#4634). - * - * Having the hash noexcept allows libstdc++'s unordered_map to recalculate - * the hash during rehash, so it does not have to cache the value. This - * reduces node's memory by sizeof(size_t). The required recalculation has - * a slight performance penalty (around 1.6%), but this is compensated by - * memory savings of about 9% which allow for a larger dbcache setting. - * - * @see https://gcc.gnu.org/onlinedocs/gcc-9.2.0/libstdc++/manual/manual/unordered_associative.html - */ - size_t operator()(const COutPoint& id) const noexcept { - return SipHashUint256Extra(k0, k1, id.hash, id.n); - } -}; - /** * A Coin in one level of the coins database caching hierarchy. * diff --git a/src/index/blockfilterindex.h b/src/index/blockfilterindex.h index 317f8c0e40..bb75eee960 100644 --- a/src/index/blockfilterindex.h +++ b/src/index/blockfilterindex.h @@ -9,15 +9,11 @@ #include #include #include +#include /** Interval between compact filter checkpoints. See BIP 157. */ static constexpr int CFCHECKPT_INTERVAL = 1000; -struct FilterHeaderHasher -{ - size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); } -}; - /** * BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of * blocks by height. An index is constructed for each supported filter type with its own database diff --git a/src/script/sigcache.h b/src/script/sigcache.h index 00534f9758..a945df0cc0 100644 --- a/src/script/sigcache.h +++ b/src/script/sigcache.h @@ -8,6 +8,7 @@ #include