diff options
author | William Casarin <jb55@jb55.com> | 2020-05-01 20:03:43 -0700 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2021-09-29 09:40:10 +0800 |
commit | 2ba4ddf31d27bebc144b3729479967b40bbe0b6a (patch) | |
tree | 6be2168585922040490ea0b56f982de1bf40cf6b /src/bloom.h | |
parent | 3c776fdcec176ffaa2056633fa2b4e737cda29ce (diff) |
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 <jb55@jb55.com>
Diffstat (limited to 'src/bloom.h')
-rw-r--r-- | src/bloom.h | 15 |
1 files changed, 5 insertions, 10 deletions
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<unsigned char>& vDataToHash) const; + unsigned int Hash(unsigned int nHashNum, Span<const unsigned char> 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<unsigned char>& vKey); + void insert(Span<const unsigned char> vKey); void insert(const COutPoint& outpoint); - void insert(const uint256& hash); - bool contains(const std::vector<unsigned char>& vKey) const; + bool contains(Span<const unsigned char> 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<unsigned char>& vKey); - void insert(const uint256& hash); - bool contains(const std::vector<unsigned char>& vKey) const; - bool contains(const uint256& hash) const; + void insert(Span<const unsigned char> vKey); + bool contains(Span<const unsigned char> vKey) const; void reset(); |