2013-07-24 07:41:39 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-03-01 02:40:47 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 07:41:39 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-03-01 02:40:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A counting Bloom filter implementation. This allows consumers to
|
|
|
|
* do fast probabilistic "is item X in set Y?" testing which will
|
|
|
|
* never answer "no" when the correct answer is "yes" (but might
|
|
|
|
* incorrectly answer "yes" when the correct answer is "no").
|
|
|
|
*/
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#ifndef mozilla_BloomFilter_h
|
|
|
|
#define mozilla_BloomFilter_h
|
2012-03-01 02:40:47 +00:00
|
|
|
|
2012-10-04 00:09:59 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-03-01 02:40:47 +00:00
|
|
|
#include "mozilla/Likely.h"
|
|
|
|
|
2013-07-30 14:25:31 +00:00
|
|
|
#include <stdint.h>
|
2012-03-01 02:40:47 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/*
|
2021-02-16 22:07:05 +00:00
|
|
|
* This class implements a classic Bloom filter as described at
|
|
|
|
* <http://en.wikipedia.org/wiki/Bloom_filter>. This allows quick
|
|
|
|
* probabilistic answers to the question "is object X in set Y?" where the
|
|
|
|
* contents of Y might not be time-invariant. The probabilistic nature of the
|
|
|
|
* test means that sometimes the answer will be "yes" when it should be "no".
|
|
|
|
* If the answer is "no", then X is guaranteed not to be in Y.
|
2012-03-01 02:40:47 +00:00
|
|
|
*
|
|
|
|
* The filter is parametrized on KeySize, which is the size of the key
|
|
|
|
* generated by each of hash functions used by the filter, in bits,
|
|
|
|
* and the type of object T being added and removed. T must implement
|
|
|
|
* a |uint32_t hash() const| method which returns a uint32_t hash key
|
|
|
|
* that will be used to generate the two separate hash functions for
|
|
|
|
* the Bloom filter. This hash key MUST be well-distributed for good
|
|
|
|
* results! KeySize is not allowed to be larger than 16.
|
|
|
|
*
|
2021-02-16 22:07:05 +00:00
|
|
|
* The filter uses exactly 2**KeySize bit (2**(KeySize-3) bytes) of memory.
|
|
|
|
* From now on we will refer to the memory used by the filter as M.
|
2012-03-01 02:40:47 +00:00
|
|
|
*
|
|
|
|
* The expected rate of incorrect "yes" answers depends on M and on
|
|
|
|
* the number N of objects in set Y. As long as N is small compared
|
|
|
|
* to M, the rate of such answers is expected to be approximately
|
|
|
|
* 4*(N/M)**2 for this filter. In practice, if Y has a few hundred
|
|
|
|
* elements then using a KeySize of 12 gives a reasonably low
|
|
|
|
* incorrect answer rate. A KeySize of 12 has the additional benefit
|
|
|
|
* of using exactly one page for the filter in typical hardware
|
|
|
|
* configurations.
|
|
|
|
*/
|
|
|
|
template <unsigned KeySize, class T>
|
2021-02-16 22:07:05 +00:00
|
|
|
class BitBloomFilter {
|
2014-05-30 05:40:33 +00:00
|
|
|
/*
|
|
|
|
* A counting Bloom filter with 8-bit counters. For now we assume
|
|
|
|
* that having two hash functions is enough, but we may revisit that
|
|
|
|
* decision later.
|
|
|
|
*
|
|
|
|
* The filter uses an array with 2**KeySize entries.
|
|
|
|
*
|
|
|
|
* Assuming a well-distributed hash function, a Bloom filter with
|
|
|
|
* array size M containing N elements and
|
|
|
|
* using k hash function has expected false positive rate exactly
|
|
|
|
*
|
|
|
|
* $ (1 - (1 - 1/M)^{kN})^k $
|
|
|
|
*
|
|
|
|
* because each array slot has a
|
|
|
|
*
|
|
|
|
* $ (1 - 1/M)^{kN} $
|
|
|
|
*
|
|
|
|
* chance of being 0, and the expected false positive rate is the
|
|
|
|
* probability that all of the k hash functions will hit a nonzero
|
|
|
|
* slot.
|
|
|
|
*
|
|
|
|
* For reasonable assumptions (M large, kN large, which should both
|
|
|
|
* hold if we're worried about false positives) about M and kN this
|
|
|
|
* becomes approximately
|
|
|
|
*
|
|
|
|
* $$ (1 - \exp(-kN/M))^k $$
|
|
|
|
*
|
|
|
|
* For our special case of k == 2, that's $(1 - \exp(-2N/M))^2$,
|
|
|
|
* or in other words
|
|
|
|
*
|
|
|
|
* $$ N/M = -0.5 * \ln(1 - \sqrt(r)) $$
|
|
|
|
*
|
|
|
|
* where r is the false positive rate. This can be used to compute
|
|
|
|
* the desired KeySize for a given load N and false positive rate r.
|
|
|
|
*
|
|
|
|
* If N/M is assumed small, then the false positive rate can
|
|
|
|
* further be approximated as 4*N^2/M^2. So increasing KeySize by
|
|
|
|
* 1, which doubles M, reduces the false positive rate by about a
|
|
|
|
* factor of 4, and a false positive rate of 1% corresponds to
|
|
|
|
* about M/N == 20.
|
|
|
|
*
|
|
|
|
* What this means in practice is that for a few hundred keys using a
|
|
|
|
* KeySize of 12 gives false positive rates on the order of 0.25-4%.
|
|
|
|
*
|
|
|
|
* Similarly, using a KeySize of 10 would lead to a 4% false
|
|
|
|
* positive rate for N == 100 and to quite bad false positive
|
|
|
|
* rates for larger N.
|
|
|
|
*/
|
|
|
|
public:
|
2021-02-16 22:07:05 +00:00
|
|
|
BitBloomFilter() {
|
|
|
|
static_assert(KeySize >= 3, "KeySize too small");
|
2014-05-30 05:40:33 +00:00
|
|
|
static_assert(KeySize <= kKeyShift, "KeySize too big");
|
|
|
|
|
2021-02-16 22:07:05 +00:00
|
|
|
// XXX: Should we have a custom operator new using calloc instead and
|
2014-05-30 05:40:33 +00:00
|
|
|
// require that we're allocated via the operator?
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:07:05 +00:00
|
|
|
/*
|
|
|
|
* Clear the filter. This should be done before reusing it.
|
|
|
|
*/
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an item to the filter.
|
|
|
|
*/
|
|
|
|
void add(const T* aValue);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether the filter might contain an item. This can
|
|
|
|
* sometimes return true even if the item is not in the filter,
|
|
|
|
* but will never return false for items that are actually in the
|
|
|
|
* filter.
|
|
|
|
*/
|
|
|
|
bool mightContain(const T* aValue) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Methods for add/contain when we already have a hash computed
|
|
|
|
*/
|
|
|
|
void add(uint32_t aHash);
|
|
|
|
bool mightContain(uint32_t aHash) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const size_t kArraySize = (1 << (KeySize - 3));
|
2023-04-19 05:25:08 +00:00
|
|
|
static const uint32_t kKeyMask = (1 << KeySize) - 1;
|
2021-02-16 22:07:05 +00:00
|
|
|
static const uint32_t kKeyShift = 16;
|
|
|
|
|
|
|
|
static uint32_t hash1(uint32_t aHash) { return aHash & kKeyMask; }
|
|
|
|
static uint32_t hash2(uint32_t aHash) {
|
|
|
|
return (aHash >> kKeyShift) & kKeyMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getSlot(uint32_t aHash) const {
|
|
|
|
uint32_t index = aHash / 8;
|
|
|
|
uint8_t shift = aHash % 8;
|
|
|
|
uint8_t mask = 1 << shift;
|
2023-04-19 05:25:09 +00:00
|
|
|
return !!(mBits[index] & mask);
|
2021-02-16 22:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setSlot(uint32_t aHash) {
|
|
|
|
uint32_t index = aHash / 8;
|
|
|
|
uint8_t shift = aHash % 8;
|
|
|
|
uint8_t bit = 1 << shift;
|
2023-04-19 05:25:09 +00:00
|
|
|
mBits[index] |= bit;
|
2021-02-16 22:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool getFirstSlot(uint32_t aHash) const { return getSlot(hash1(aHash)); }
|
|
|
|
bool getSecondSlot(uint32_t aHash) const { return getSlot(hash2(aHash)); }
|
|
|
|
|
|
|
|
void setFirstSlot(uint32_t aHash) { setSlot(hash1(aHash)); }
|
|
|
|
void setSecondSlot(uint32_t aHash) { setSlot(hash2(aHash)); }
|
|
|
|
|
2023-04-19 05:25:09 +00:00
|
|
|
uint8_t mBits[kArraySize];
|
2021-02-16 22:07:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
|
|
|
inline void BitBloomFilter<KeySize, T>::clear() {
|
2023-04-19 05:25:09 +00:00
|
|
|
memset(mBits, 0, kArraySize);
|
2021-02-16 22:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
|
|
|
inline void BitBloomFilter<KeySize, T>::add(uint32_t aHash) {
|
|
|
|
setFirstSlot(aHash);
|
|
|
|
setSecondSlot(aHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
|
|
|
MOZ_ALWAYS_INLINE void BitBloomFilter<KeySize, T>::add(const T* aValue) {
|
|
|
|
uint32_t hash = aValue->hash();
|
|
|
|
return add(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
|
|
|
MOZ_ALWAYS_INLINE bool BitBloomFilter<KeySize, T>::mightContain(
|
|
|
|
uint32_t aHash) const {
|
|
|
|
// Check that all the slots for this hash contain something
|
|
|
|
return getFirstSlot(aHash) && getSecondSlot(aHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
|
|
|
MOZ_ALWAYS_INLINE bool BitBloomFilter<KeySize, T>::mightContain(
|
|
|
|
const T* aValue) const {
|
|
|
|
uint32_t hash = aValue->hash();
|
|
|
|
return mightContain(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This class implements a counting Bloom filter as described at
|
|
|
|
* <http://en.wikipedia.org/wiki/Bloom_filter#Counting_filters>, with
|
|
|
|
* 8-bit counters.
|
|
|
|
*
|
|
|
|
* Compared to `BitBloomFilter`, this class supports 'remove' operation.
|
|
|
|
*
|
|
|
|
* The filter uses exactly 2**KeySize bytes of memory.
|
|
|
|
*
|
|
|
|
* Other characteristics are the same as BitBloomFilter.
|
|
|
|
*/
|
|
|
|
template <unsigned KeySize, class T>
|
|
|
|
class CountingBloomFilter {
|
|
|
|
public:
|
|
|
|
CountingBloomFilter() {
|
|
|
|
static_assert(KeySize <= kKeyShift, "KeySize too big");
|
|
|
|
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2014-05-30 05:40:33 +00:00
|
|
|
/*
|
|
|
|
* Clear the filter. This should be done before reusing it, because
|
|
|
|
* just removing all items doesn't clear counters that hit the upper
|
|
|
|
* bound.
|
|
|
|
*/
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add an item to the filter.
|
|
|
|
*/
|
|
|
|
void add(const T* aValue);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove an item from the filter.
|
|
|
|
*/
|
|
|
|
void remove(const T* aValue);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether the filter might contain an item. This can
|
|
|
|
* sometimes return true even if the item is not in the filter,
|
|
|
|
* but will never return false for items that are actually in the
|
|
|
|
* filter.
|
|
|
|
*/
|
|
|
|
bool mightContain(const T* aValue) const;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Methods for add/remove/contain when we already have a hash computed
|
|
|
|
*/
|
|
|
|
void add(uint32_t aHash);
|
|
|
|
void remove(uint32_t aHash);
|
|
|
|
bool mightContain(uint32_t aHash) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const size_t kArraySize = (1 << KeySize);
|
|
|
|
static const uint32_t kKeyMask = (1 << KeySize) - 1;
|
|
|
|
static const uint32_t kKeyShift = 16;
|
|
|
|
|
|
|
|
static uint32_t hash1(uint32_t aHash) { return aHash & kKeyMask; }
|
|
|
|
static uint32_t hash2(uint32_t aHash) {
|
|
|
|
return (aHash >> kKeyShift) & kKeyMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t& firstSlot(uint32_t aHash) { return mCounters[hash1(aHash)]; }
|
|
|
|
uint8_t& secondSlot(uint32_t aHash) { return mCounters[hash2(aHash)]; }
|
|
|
|
|
|
|
|
const uint8_t& firstSlot(uint32_t aHash) const {
|
|
|
|
return mCounters[hash1(aHash)];
|
|
|
|
}
|
|
|
|
const uint8_t& secondSlot(uint32_t aHash) const {
|
|
|
|
return mCounters[hash2(aHash)];
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool full(const uint8_t& aSlot) { return aSlot == UINT8_MAX; }
|
|
|
|
|
|
|
|
uint8_t mCounters[kArraySize];
|
2012-03-01 02:40:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
2021-02-16 22:07:05 +00:00
|
|
|
inline void CountingBloomFilter<KeySize, T>::clear() {
|
2014-05-30 05:40:33 +00:00
|
|
|
memset(mCounters, 0, kArraySize);
|
2012-03-01 02:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
2021-02-16 22:07:05 +00:00
|
|
|
inline void CountingBloomFilter<KeySize, T>::add(uint32_t aHash) {
|
2014-05-30 05:40:33 +00:00
|
|
|
uint8_t& slot1 = firstSlot(aHash);
|
|
|
|
if (MOZ_LIKELY(!full(slot1))) {
|
2012-06-04 03:36:43 +00:00
|
|
|
++slot1;
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2014-06-13 06:34:08 +00:00
|
|
|
uint8_t& slot2 = secondSlot(aHash);
|
|
|
|
if (MOZ_LIKELY(!full(slot2))) {
|
2012-06-04 03:36:43 +00:00
|
|
|
++slot2;
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-03-01 02:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
2021-02-16 22:07:05 +00:00
|
|
|
MOZ_ALWAYS_INLINE void CountingBloomFilter<KeySize, T>::add(const T* aValue) {
|
2014-05-30 05:40:33 +00:00
|
|
|
uint32_t hash = aValue->hash();
|
2012-06-04 03:36:43 +00:00
|
|
|
return add(hash);
|
2012-03-01 02:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
2021-02-16 22:07:05 +00:00
|
|
|
inline void CountingBloomFilter<KeySize, T>::remove(uint32_t aHash) {
|
2012-06-04 03:36:43 +00:00
|
|
|
// If the slots are full, we don't know whether we bumped them to be
|
|
|
|
// there when we added or not, so just leave them full.
|
2014-05-30 05:40:33 +00:00
|
|
|
uint8_t& slot1 = firstSlot(aHash);
|
|
|
|
if (MOZ_LIKELY(!full(slot1))) {
|
2012-06-04 03:36:43 +00:00
|
|
|
--slot1;
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
|
|
|
uint8_t& slot2 = secondSlot(aHash);
|
|
|
|
if (MOZ_LIKELY(!full(slot2))) {
|
2012-06-04 03:36:43 +00:00
|
|
|
--slot2;
|
2014-05-30 05:40:33 +00:00
|
|
|
}
|
2012-03-01 02:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
2021-02-16 22:07:05 +00:00
|
|
|
MOZ_ALWAYS_INLINE void CountingBloomFilter<KeySize, T>::remove(
|
|
|
|
const T* aValue) {
|
2014-05-30 05:40:33 +00:00
|
|
|
uint32_t hash = aValue->hash();
|
2012-06-04 03:36:43 +00:00
|
|
|
remove(hash);
|
2012-03-01 02:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
2021-02-16 22:07:05 +00:00
|
|
|
MOZ_ALWAYS_INLINE bool CountingBloomFilter<KeySize, T>::mightContain(
|
2014-05-30 05:40:33 +00:00
|
|
|
uint32_t aHash) const {
|
2012-06-04 03:36:43 +00:00
|
|
|
// Check that all the slots for this hash contain something
|
2014-05-30 05:40:33 +00:00
|
|
|
return firstSlot(aHash) && secondSlot(aHash);
|
2012-03-01 02:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <unsigned KeySize, class T>
|
2021-02-16 22:07:05 +00:00
|
|
|
MOZ_ALWAYS_INLINE bool CountingBloomFilter<KeySize, T>::mightContain(
|
2014-05-30 05:40:33 +00:00
|
|
|
const T* aValue) const {
|
|
|
|
uint32_t hash = aValue->hash();
|
2012-06-04 03:36:43 +00:00
|
|
|
return mightContain(hash);
|
2012-03-01 02:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#endif /* mozilla_BloomFilter_h */
|