aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2016-05-09 08:31:14 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2016-05-09 08:52:31 +0200
commitf17032f703288d43a76cffe8fa89b87ade9e3074 (patch)
treec6b574f4b1adc85bfae534c46bbe9a3e4e740613 /src
parentfbd84788e676c9cbd126f6236ec46bb2f342bd00 (diff)
parent1953c40aa9589a03035fd294f3ba3549374a4826 (diff)
downloadbitcoin-f17032f703288d43a76cffe8fa89b87ade9e3074.tar.xz
Merge #7934: Improve rolling bloom filter performance and benchmark
1953c40 More efficient bitsliced rolling Bloom filter (Pieter Wuille) aa62b68 Benchmark rolling bloom filter (Pieter Wuille)
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.bench.include3
-rw-r--r--src/bench/rollingbloom.cpp43
-rw-r--r--src/bloom.cpp40
-rw-r--r--src/bloom.h13
-rw-r--r--src/test/bloom_tests.cpp5
5 files changed, 77 insertions, 27 deletions
diff --git a/src/Makefile.bench.include b/src/Makefile.bench.include
index 8e7b59b461..24e2b3e0c8 100644
--- a/src/Makefile.bench.include
+++ b/src/Makefile.bench.include
@@ -7,7 +7,8 @@ bench_bench_bitcoin_SOURCES = \
bench/bench_bitcoin.cpp \
bench/bench.cpp \
bench/bench.h \
- bench/Examples.cpp
+ bench/Examples.cpp \
+ bench/rollingbloom.cpp
bench_bench_bitcoin_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(EVENT_CLFAGS) $(EVENT_PTHREADS_CFLAGS) -I$(builddir)/bench/
bench_bench_bitcoin_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
diff --git a/src/bench/rollingbloom.cpp b/src/bench/rollingbloom.cpp
new file mode 100644
index 0000000000..73c02cf718
--- /dev/null
+++ b/src/bench/rollingbloom.cpp
@@ -0,0 +1,43 @@
+// Copyright (c) 2016 The Bitcoin Core developers
+// Distributed under the MIT software license, see the accompanying
+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+#include <iostream>
+
+#include "bench.h"
+#include "bloom.h"
+#include "utiltime.h"
+
+static void RollingBloom(benchmark::State& state)
+{
+ CRollingBloomFilter filter(120000, 0.000001);
+ std::vector<unsigned char> data(32);
+ uint32_t count = 0;
+ uint32_t nEntriesPerGeneration = (120000 + 1) / 2;
+ uint32_t countnow = 0;
+ uint64_t match = 0;
+ while (state.KeepRunning()) {
+ count++;
+ data[0] = count;
+ data[1] = count >> 8;
+ data[2] = count >> 16;
+ data[3] = count >> 24;
+ if (countnow == nEntriesPerGeneration) {
+ int64_t b = GetTimeMicros();
+ filter.insert(data);
+ int64_t e = GetTimeMicros();
+ std::cout << "RollingBloom-refresh,1," << (e-b)*0.000001 << "," << (e-b)*0.000001 << "," << (e-b)*0.000001 << "\n";
+ countnow = 0;
+ } else {
+ filter.insert(data);
+ }
+ countnow++;
+ data[0] = count >> 24;
+ data[1] = count >> 16;
+ data[2] = count >> 8;
+ data[3] = count;
+ match += filter.contains(data);
+ }
+}
+
+BENCHMARK(RollingBloom);
diff --git a/src/bloom.cpp b/src/bloom.cpp
index 4e6f0e5d2d..fd328e8e96 100644
--- a/src/bloom.cpp
+++ b/src/bloom.cpp
@@ -234,14 +234,18 @@ CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate)
*/
uint32_t nFilterBits = (uint32_t)ceil(-1.0 * nHashFuncs * nMaxElements / log(1.0 - exp(logFpRate / nHashFuncs)));
data.clear();
- /* We store up to 16 'bits' per data element. */
- data.resize((nFilterBits + 15) / 16);
+ /* For each data element we need to store 2 bits. If both bits are 0, the
+ * bit is treated as unset. If the bits are (01), (10), or (11), the bit is
+ * treated as set in generation 1, 2, or 3 respectively.
+ * These bits are stored in separate integers: position P corresponds to bit
+ * (P & 63) of the integers data[(P >> 6) * 2] and data[(P >> 6) * 2 + 1]. */
+ data.resize(((nFilterBits + 63) / 64) << 1);
reset();
}
/* Similar to CBloomFilter::Hash */
-inline unsigned int CRollingBloomFilter::Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const {
- return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (data.size() * 16);
+static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak, const std::vector<unsigned char>& vDataToHash) {
+ return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash);
}
void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
@@ -252,18 +256,25 @@ void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
if (nGeneration == 4) {
nGeneration = 1;
}
+ uint64_t nGenerationMask1 = -(uint64_t)(nGeneration & 1);
+ uint64_t nGenerationMask2 = -(uint64_t)(nGeneration >> 1);
/* Wipe old entries that used this generation number. */
- for (uint32_t p = 0; p < data.size() * 16; p++) {
- if (get(p) == nGeneration) {
- put(p, 0);
- }
+ for (uint32_t p = 0; p < data.size(); p += 2) {
+ uint64_t p1 = data[p], p2 = data[p + 1];
+ uint64_t mask = (p1 ^ nGenerationMask1) | (p2 ^ nGenerationMask2);
+ data[p] = p1 & mask;
+ data[p + 1] = p2 & mask;
}
}
nEntriesThisGeneration++;
for (int n = 0; n < nHashFuncs; n++) {
- uint32_t h = Hash(n, vKey);
- put(h, nGeneration);
+ uint32_t h = RollingBloomHash(n, nTweak, vKey);
+ int bit = h & 0x3F;
+ uint32_t pos = (h >> 6) % data.size();
+ /* The lowest bit of pos is ignored, and set to zero for the first bit, and to one for the second. */
+ data[pos & ~1] = (data[pos & ~1] & ~(((uint64_t)1) << bit)) | ((uint64_t)(nGeneration & 1)) << bit;
+ data[pos | 1] = (data[pos | 1] & ~(((uint64_t)1) << bit)) | ((uint64_t)(nGeneration >> 1)) << bit;
}
}
@@ -276,8 +287,11 @@ void CRollingBloomFilter::insert(const uint256& hash)
bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
{
for (int n = 0; n < nHashFuncs; n++) {
- uint32_t h = Hash(n, vKey);
- if (get(h) == 0) {
+ uint32_t h = RollingBloomHash(n, nTweak, vKey);
+ int bit = h & 0x3F;
+ uint32_t pos = (h >> 6) % data.size();
+ /* If the relevant bit is not set in either data[pos & ~1] or data[pos | 1], the filter does not contain vKey */
+ if (!(((data[pos & ~1] | data[pos | 1]) >> bit) & 1)) {
return false;
}
}
@@ -295,7 +309,7 @@ void CRollingBloomFilter::reset()
nTweak = GetRand(std::numeric_limits<unsigned int>::max());
nEntriesThisGeneration = 0;
nGeneration = 1;
- for (std::vector<uint32_t>::iterator it = data.begin(); it != data.end(); it++) {
+ for (std::vector<uint64_t>::iterator it = data.begin(); it != data.end(); it++) {
*it = 0;
}
}
diff --git a/src/bloom.h b/src/bloom.h
index b0ad8b875d..ad6de625d8 100644
--- a/src/bloom.h
+++ b/src/bloom.h
@@ -135,20 +135,9 @@ private:
int nEntriesPerGeneration;
int nEntriesThisGeneration;
int nGeneration;
- std::vector<uint32_t> data;
+ std::vector<uint64_t> data;
unsigned int nTweak;
int nHashFuncs;
-
- unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
-
- inline int get(uint32_t position) const {
- return (data[(position >> 4) % data.size()] >> (2 * (position & 0xF))) & 0x3;
- }
-
- inline void put(uint32_t position, uint32_t val) {
- uint32_t& cell = data[(position >> 4) % data.size()];
- cell = (cell & ~(((uint32_t)3) << (2 * (position & 0xF)))) | (val << (2 * (position & 0xF)));
- }
};
#endif // BITCOIN_BLOOM_H
diff --git a/src/test/bloom_tests.cpp b/src/test/bloom_tests.cpp
index 9557000ddc..042fad42da 100644
--- a/src/test/bloom_tests.cpp
+++ b/src/test/bloom_tests.cpp
@@ -514,11 +514,14 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
if (i >= 100)
BOOST_CHECK(rb1.contains(data[i-100]));
rb1.insert(data[i]);
+ BOOST_CHECK(rb1.contains(data[i]));
}
// Insert 999 more random entries:
for (int i = 0; i < 999; i++) {
- rb1.insert(RandomData());
+ std::vector<unsigned char> d = RandomData();
+ rb1.insert(d);
+ BOOST_CHECK(rb1.contains(d));
}
// Sanity check to make sure the filter isn't just filling up:
nHits = 0;