Skip to content

Commit 9aac9f9

Browse files
committed
replace modulus with FastMod
Replaces the slow modulo operation with a much faster 32bit multiplication & shift. This works because the hash should be uniformly distributed between 0 and 2^32-1. This speeds up the benchmark by a factor of about 1.3: RollingBloom, 5, 1500000, 3.73733, 4.97569e-07, 4.99002e-07, 4.98372e-07 # before RollingBloom, 5, 1500000, 2.86842, 3.81630e-07, 3.83730e-07, 3.82473e-07 # FastMod Be aware that this changes the position of the bits that are toggled, so this should probably not be used for CBloomFilter which is serialized.
1 parent 66cc47b commit 9aac9f9

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/bloom.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak,
245245
return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash);
246246
}
247247

248+
249+
// A replacement for x % n. This assumes that x and n are 32bit integers, and x is a uniformly random distributed 32bit value
250+
// which should be the case for a good hash.
251+
// See https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
252+
static inline uint32_t FastMod(uint32_t x, size_t n) {
253+
return ((uint64_t)x * (uint64_t)n) >> 32;
254+
}
255+
248256
void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
249257
{
250258
if (nEntriesThisGeneration == nEntriesPerGeneration) {
@@ -268,7 +276,8 @@ void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
268276
for (int n = 0; n < nHashFuncs; n++) {
269277
uint32_t h = RollingBloomHash(n, nTweak, vKey);
270278
int bit = h & 0x3F;
271-
uint32_t pos = (h >> 6) % data.size();
279+
/* FastMod works with the upper bits of h, so it is safe to ignore that the lower bits of h are already used for bit. */
280+
uint32_t pos = FastMod(h, data.size());
272281
/* The lowest bit of pos is ignored, and set to zero for the first bit, and to one for the second. */
273282
data[pos & ~1] = (data[pos & ~1] & ~(((uint64_t)1) << bit)) | ((uint64_t)(nGeneration & 1)) << bit;
274283
data[pos | 1] = (data[pos | 1] & ~(((uint64_t)1) << bit)) | ((uint64_t)(nGeneration >> 1)) << bit;
@@ -286,7 +295,7 @@ bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
286295
for (int n = 0; n < nHashFuncs; n++) {
287296
uint32_t h = RollingBloomHash(n, nTweak, vKey);
288297
int bit = h & 0x3F;
289-
uint32_t pos = (h >> 6) % data.size();
298+
uint32_t pos = FastMod(h, data.size());
290299
/* If the relevant bit is not set in either data[pos & ~1] or data[pos | 1], the filter does not contain vKey */
291300
if (!(((data[pos & ~1] | data[pos | 1]) >> bit) & 1)) {
292301
return false;

0 commit comments

Comments
 (0)