From 2ba4ddf31d27bebc144b3729479967b40bbe0b6a Mon Sep 17 00:00:00 2001 From: William Casarin Date: Fri, 1 May 2020 20:03:43 -0700 Subject: [PATCH 1/3] bloom: use Span instead of std::vector for `insert` and `contains` We can avoid many unnecessary std::vector allocations by changing CBloomFilter to take Spans instead of std::vector's for the `insert` and `contains` operations. CBloomFilter currently converts types such as CDataStream and uint256 to std::vector on `insert` and `contains`. This is unnecessary because CDataStreams and uint256 are already std::vectors internally. We just need a way to point to the right data within those types. Span gives us this ability. Signed-off-by: William Casarin --- src/bloom.cpp | 43 +++++++++------------------------------- src/bloom.h | 15 +++++--------- src/hash.cpp | 1 + src/test/bloom_tests.cpp | 2 +- 4 files changed, 16 insertions(+), 45 deletions(-) diff --git a/src/bloom.cpp b/src/bloom.cpp index d0128a26d7..78ef1897c9 100644 --- a/src/bloom.cpp +++ b/src/bloom.cpp @@ -37,13 +37,13 @@ CBloomFilter::CBloomFilter(const unsigned int nElements, const double nFPRate, c { } -inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, const std::vector& vDataToHash) const +inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, Span vDataToHash) const { // 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values. return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8); } -void CBloomFilter::insert(const std::vector& vKey) +void CBloomFilter::insert(Span vKey) { if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700) return; @@ -59,17 +59,10 @@ void CBloomFilter::insert(const COutPoint& outpoint) { CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << outpoint; - std::vector data(stream.begin(), stream.end()); - insert(data); + insert(stream); } -void CBloomFilter::insert(const uint256& hash) -{ - std::vector data(hash.begin(), hash.end()); - insert(data); -} - -bool CBloomFilter::contains(const std::vector& vKey) const +bool CBloomFilter::contains(Span vKey) const { if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700) return true; @@ -87,14 +80,7 @@ bool CBloomFilter::contains(const COutPoint& outpoint) const { CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << outpoint; - std::vector data(stream.begin(), stream.end()); - return contains(data); -} - -bool CBloomFilter::contains(const uint256& hash) const -{ - std::vector data(hash.begin(), hash.end()); - return contains(data); + return contains(MakeUCharSpan(stream)); } bool CBloomFilter::IsWithinSizeConstraints() const @@ -198,7 +184,8 @@ CRollingBloomFilter::CRollingBloomFilter(const unsigned int nElements, const dou } /* Similar to CBloomFilter::Hash */ -static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak, const std::vector& vDataToHash) { +static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak, Span vDataToHash) +{ return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash); } @@ -210,7 +197,7 @@ static inline uint32_t FastMod(uint32_t x, size_t n) { return ((uint64_t)x * (uint64_t)n) >> 32; } -void CRollingBloomFilter::insert(const std::vector& vKey) +void CRollingBloomFilter::insert(Span vKey) { if (nEntriesThisGeneration == nEntriesPerGeneration) { nEntriesThisGeneration = 0; @@ -241,13 +228,7 @@ void CRollingBloomFilter::insert(const std::vector& vKey) } } -void CRollingBloomFilter::insert(const uint256& hash) -{ - std::vector vData(hash.begin(), hash.end()); - insert(vData); -} - -bool CRollingBloomFilter::contains(const std::vector& vKey) const +bool CRollingBloomFilter::contains(Span vKey) const { for (int n = 0; n < nHashFuncs; n++) { uint32_t h = RollingBloomHash(n, nTweak, vKey); @@ -261,12 +242,6 @@ bool CRollingBloomFilter::contains(const std::vector& vKey) const return true; } -bool CRollingBloomFilter::contains(const uint256& hash) const -{ - std::vector vData(hash.begin(), hash.end()); - return contains(vData); -} - void CRollingBloomFilter::reset() { nTweak = GetRand(std::numeric_limits::max()); diff --git a/src/bloom.h b/src/bloom.h index fdaa8abfb2..391e2cc888 100644 --- a/src/bloom.h +++ b/src/bloom.h @@ -11,7 +11,6 @@ class COutPoint; class CTransaction; -class uint256; //! 20,000 items with fp rate < 0.1% or 10,000 items and <0.0001% static const unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes @@ -49,7 +48,7 @@ private: unsigned int nTweak; unsigned char nFlags; - unsigned int Hash(unsigned int nHashNum, const std::vector& vDataToHash) const; + unsigned int Hash(unsigned int nHashNum, Span vDataToHash) const; public: /** @@ -66,13 +65,11 @@ public: SERIALIZE_METHODS(CBloomFilter, obj) { READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags); } - void insert(const std::vector& vKey); + void insert(Span vKey); void insert(const COutPoint& outpoint); - void insert(const uint256& hash); - bool contains(const std::vector& vKey) const; + bool contains(Span vKey) const; bool contains(const COutPoint& outpoint) const; - bool contains(const uint256& hash) const; //! True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS //! (catch a filter which was just deserialized which was too big) @@ -112,10 +109,8 @@ class CRollingBloomFilter public: CRollingBloomFilter(const unsigned int nElements, const double nFPRate); - void insert(const std::vector& vKey); - void insert(const uint256& hash); - bool contains(const std::vector& vKey) const; - bool contains(const uint256& hash) const; + void insert(Span vKey); + bool contains(Span vKey) const; void reset(); diff --git a/src/hash.cpp b/src/hash.cpp index 3465caa3a9..92c923fbd2 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -3,6 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include #include diff --git a/src/test/bloom_tests.cpp b/src/test/bloom_tests.cpp index 5a98558240..23ef2062ef 100644 --- a/src/test/bloom_tests.cpp +++ b/src/test/bloom_tests.cpp @@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_key) CBloomFilter filter(2, 0.001, 0, BLOOM_UPDATE_ALL); filter.insert(vchPubKey); uint160 hash = pubkey.GetID(); - filter.insert(std::vector(hash.begin(), hash.end())); + filter.insert(hash); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); stream << filter; From f1ed1d3194d4160923f3b02fa1acffd805ab4428 Mon Sep 17 00:00:00 2001 From: fanquake Date: Wed, 29 Sep 2021 09:43:37 +0800 Subject: [PATCH 2/3] bloom: use constexpr where appropriate --- src/bloom.cpp | 4 ++-- src/bloom.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/bloom.cpp b/src/bloom.cpp index 78ef1897c9..20722c8d5c 100644 --- a/src/bloom.cpp +++ b/src/bloom.cpp @@ -16,8 +16,8 @@ #include -#define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455 -#define LN2 0.6931471805599453094172321214581765680755001343602552 +static constexpr double LN2SQUARED = 0.4804530139182014246671025263266649717305529515945455; +static constexpr double LN2 = 0.6931471805599453094172321214581765680755001343602552; CBloomFilter::CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweakIn, unsigned char nFlagsIn) : /** diff --git a/src/bloom.h b/src/bloom.h index 391e2cc888..67177c6daa 100644 --- a/src/bloom.h +++ b/src/bloom.h @@ -13,8 +13,8 @@ class COutPoint; class CTransaction; //! 20,000 items with fp rate < 0.1% or 10,000 items and <0.0001% -static const unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes -static const unsigned int MAX_HASH_FUNCS = 50; +static constexpr unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes +static constexpr unsigned int MAX_HASH_FUNCS = 50; /** * First two bits of nFlags control how much IsRelevantAndUpdate actually updates From a11da7541148b5bb8e293c0ee49b2856a6628099 Mon Sep 17 00:00:00 2001 From: fanquake Date: Wed, 29 Sep 2021 09:48:36 +0800 Subject: [PATCH 3/3] bloom: cleanup includes --- src/bloom.cpp | 12 +++++++----- src/bloom.h | 1 + 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/bloom.cpp b/src/bloom.cpp index 20722c8d5c..15e06389de 100644 --- a/src/bloom.cpp +++ b/src/bloom.cpp @@ -4,17 +4,19 @@ #include -#include #include +#include +#include #include