blewm 0.1.0

Blewm: A Bloom Filter that Bloo(m) my Mind
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#![forbid(unsafe_code)]
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]

use std::hash::{Hash, BuildHasher, RandomState};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};


const USIZE_BITS: usize = usize::BITS as usize;
const INDEX_1_SHIFT: u32 = usize::ilog2(USIZE_BITS);
const INDEX_2_MASK: usize = (1 << INDEX_1_SHIFT) - 1;

/// A fast, concurrent Bloom filter.
#[derive(Debug)]
pub struct Filter<H = RandomState> {
    bits: Box<[AtomicUsize]>,
    num_hashes: usize,
    hasher: H,
}

impl<H> Filter<H> {
    /// Creates a new concurrent Bloom filter that does not exceed the specified
    /// false positive rate when at most `capacity` elements are inserted.
    ///
    /// ## Panics
    ///
    /// Panics if `false_positive_rate` is not in the range `(0, 1)`.
    pub fn with_hasher(capacity: usize, false_positive_rate: f64, hasher: H) -> Self {
        assert!(0.0 < false_positive_rate && false_positive_rate < 1.0);

        // compute optimal number of hashes and bits
        let log2_eps = f64::log2(false_positive_rate);
        let num_hashes = 1usize.max(f64::round(-log2_eps) as usize);
        let min_num_bits = f64::round(capacity as f64 * -log2_eps / std::f64::consts::LN_2) as usize;
        let num_bits = min_num_bits.next_power_of_two();
        let num_slots = 1usize.max(num_bits / USIZE_BITS);
        let bits: Box<[_]> = (0..num_slots).map(|_| AtomicUsize::new(0)).collect();

        Filter { bits, num_hashes, hasher }
    }

    #[inline]
    pub const fn as_bits(&self) -> &[AtomicUsize] {
        &self.bits
    }

    #[inline]
    pub const fn as_mut_bits(&mut self) -> &mut [AtomicUsize] {
        &mut self.bits
    }

    #[inline]
    pub fn into_bits(self) -> Box<[AtomicUsize]> {
        self.bits
    }

    #[inline]
    pub const fn num_slots(&self) -> usize {
        self.bits.len()
    }

    #[inline]
    pub const fn num_bits(&self) -> usize {
        self.bits.len() * USIZE_BITS
    }

    #[inline]
    pub const fn num_hashes(&self) -> usize {
        self.num_hashes
    }

    #[inline]
    const fn slot_mask(&self) -> usize {
        self.num_slots() - 1
    }

    /// Inserts an element based on its hash into the Bloom filter.
    /// Returns `true` if the element already existed (subject to
    /// the usual caveats regarding false positives).
    pub fn insert_hash(&self, mut hash: u64) -> bool {
        let slot_mask = self.slot_mask();
        let aux = aux_hash(hash);
        let mut exists = true;

        for _ in 0..self.num_hashes {
            let h = hash as usize;
            let slot_idx = (h >> INDEX_1_SHIFT) & slot_mask;
            let bit_idx = h & INDEX_2_MASK;
            let bit_mask = 1 << bit_idx;
            let slot_bits = self.bits[slot_idx].fetch_or(bit_mask, Relaxed);

            hash = next_hash(hash, aux);
            exists &= (slot_bits & bit_mask) != 0;
        }

        exists
    }

    /// Returns `false` if the hash of an element is not in the set,
    /// and `true` if it _may_ be in the set with high probability.
    pub fn contains_hash(&self, mut hash: u64) -> bool {
        let slot_mask = self.slot_mask();
        let aux = aux_hash(hash);

        (0..self.num_hashes).all(|_| {
            let h = hash as usize;
            let slot_idx = (h >> INDEX_1_SHIFT) & slot_mask;
            let bit_idx = h & INDEX_2_MASK;
            let bit_mask = 1 << bit_idx;
            let slot_bits = self.bits[slot_idx].load(Relaxed);

            hash = next_hash(hash, aux);
            slot_bits & bit_mask != 0
        })
    }

    /// Removes all elements.
    ///
    /// ## Examples
    ///
    /// ```
    /// # use blewm::Filter;
    /// let filter = Filter::new(1000, 0.0001);
    ///
    /// for x in 1000..2000 {
    ///     assert_eq!(filter.insert(x), false);
    /// }
    ///
    /// for x in 1000..2000 {
    ///     assert!(filter.contains(x));
    /// }
    ///
    /// filter.clear();
    ///
    /// for x in 0000..3000 {
    ///     assert_eq!(filter.contains(x), false);
    /// }
    /// ```
    pub fn clear(&self) {
        for slot in &self.bits {
            slot.store(0, Relaxed);
        }
    }
}

impl<H: BuildHasher> Filter<H> {
    /// Inserts an element into the Bloom filter.
    /// Returns `true` if the element already existed (subject to
    /// the usual caveats regarding false positives).
    ///
    /// ## Examples
    ///
    /// ```
    /// # use blewm::Filter;
    /// let filter = Filter::new(1000, 0.0001);
    ///
    /// filter.insert(1);
    /// filter.insert(2);
    /// filter.insert(3);
    /// filter.insert(42);
    ///
    /// assert!(filter.contains(1));
    /// assert!(filter.contains(2));
    /// assert!(filter.contains(3));
    ///
    /// for x in 4..1000 {
    ///     assert_eq!(filter.contains(x), x == 42);
    /// }
    /// ```
    pub fn insert<T: Hash>(&self, element: T) -> bool {
        let hash = self.hasher.hash_one(element);
        self.insert_hash(hash)
    }

    pub fn contains<T: Hash>(&self, element: T) -> bool {
        let hash = self.hasher.hash_one(element);
        self.contains_hash(hash)
    }

    /// Returns the hash of the element, based on which it
    /// can be inserted to or looked up in the Bloom filter.
    #[inline]
    pub fn hash_item<T: Hash>(&self, element: T) -> u64 {
        self.hasher.hash_one(element)
    }
}

impl<H: Default> Filter<H> {
    /// Creates a new concurrent Bloom filter that does not exceed the specified
    /// false positive rate when at most `capacity` elements are inserted.
    ///
    /// See the documentation of [`Filter::with_hasher`] for details.
    #[inline]
    pub fn with_default_hasher(capacity: usize, false_positive_rate: f64) -> Self {
        Self::with_hasher(capacity, false_positive_rate, H::default())
    }
}

impl Filter<RandomState> {
    /// Creates a new concurrent Bloom filter that does not exceed the specified
    /// false positive rate when at most `capacity` elements are inserted, using
    /// the default hasher.
    ///
    /// See the documentation of [`Filter::with_hasher`] for details.
    #[inline]
    pub fn new(capacity: usize, false_positive_rate: f64) -> Self {
        Self::with_default_hasher(capacity, false_positive_rate)
    }
}

impl<H: Clone> Clone for Filter<H> {
    fn clone(&self) -> Self {
        Filter {
            bits: self.bits.iter().map(|slot| AtomicUsize::new(slot.load(Relaxed))).collect(),
            num_hashes: self.num_hashes,
            hasher: self.hasher.clone(),
        }
    }
}

impl<H: PartialEq> PartialEq for Filter<H> {
    fn eq(&self, other: &Self) -> bool {
        self.num_hashes == other.num_hashes // necessary because different # of hashers produce distinct patterns
            && self.hasher == other.hasher // necessary because different hashers may produce different patterns
            && self.bits.len() == other.bits.len()
            && self.bits.iter().map(|x| x.load(Relaxed)).eq(other.bits.iter().map(|y| y.load(Relaxed)))
    }
}

impl<H: Eq> Eq for Filter<H> {}

impl<H> From<Filter<H>> for Box<[AtomicUsize]> {
    fn from(filter: Filter<H>) -> Self {
        filter.into_bits()
    }
}

impl<H> AsRef<[AtomicUsize]> for Filter<H> {
    fn as_ref(&self) -> &[AtomicUsize] {
        self.as_bits()
    }
}

impl<H> AsMut<[AtomicUsize]> for Filter<H> {
    fn as_mut(&mut self) -> &mut [AtomicUsize] {
        self.as_mut_bits()
    }
}

/// Explicit impl for the immutable reference type,
/// so that we don't have to have mutable references
/// (which `Extend` would normally require).
impl<H, T> Extend<T> for &Filter<H>
where
    H: BuildHasher,
    T: Hash,
{
    /// ```
    /// # use blewm::Filter;
    /// let filter = Filter::new(1000, 1e-4);
    /// let mut filter = &filter;
    ///
    /// let range = 7386..8386; // chosen by fair dice roll
    /// for x in range.clone() {
    ///     assert_eq!(filter.contains(x), false);
    /// }
    ///
    /// let new_elems = [7922, 7685, 8313, 7426, 8118, 7394];
    /// filter.extend(new_elems);
    ///
    /// for x in range {
    ///     assert_eq!(filter.contains(x), new_elems.contains(&x));
    /// }
    ///
    /// for x in (0..1000).chain(9000..10_000) {
    ///     assert_eq!(filter.contains(x), false);
    /// }
    /// ```
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = T>
    {
        for item in iter {
            self.insert(item);
        }
    }
}

/// The non-reference impl is done in terms of the impl for `&Filter<H>`.
impl<H, T> Extend<T> for Filter<H>
where
    H: BuildHasher,
    T: Hash,
{
    /// ```
    /// # use blewm::Filter;
    /// let mut filter = Filter::new(1000, 1e-4);
    ///
    /// let range = 7386..8386; // chosen by fair dice roll
    /// for x in range.clone() {
    ///     assert_eq!(filter.contains(x), false);
    /// }
    ///
    /// let new_elems = [7922, 7685, 8313, 7426, 8118, 7394];
    /// filter.extend(new_elems);
    ///
    /// for x in range {
    ///     assert_eq!(filter.contains(x), new_elems.contains(&x));
    /// }
    ///
    /// for x in (0..1000).chain(9000..10_000) {
    ///     assert_eq!(filter.contains(x), false);
    /// }
    /// ```
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = T>
    {
        let mut this: &Self = &*self;
        this.extend(iter);
    }
}

/// Adapted from the `fastbloom` crate
#[inline]
fn aux_hash(hash: u64) -> u64 {
    hash.wrapping_shr(32)
        .wrapping_mul(0x517c_c1b7_2722_0a95) // 0xffff_ffff_ffff_ffff / 0x517c_c1b7_2722_0a95 = π
}

/// Adapted from the `fastbloom` crate
#[inline]
fn next_hash(hash: u64, aux: u64) -> u64 {
    hash.wrapping_add(aux).rotate_left(5)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread::{self, JoinHandle};
    use ahash::RandomState as AHashRandomState;

    fn generic_hasher<H>(desired_fpr: f64) -> JoinHandle<()>
    where
        H: Default + BuildHasher
    {
        thread::spawn(move || generic_hasher_impl::<H>(desired_fpr))
    }

    fn generic_hasher_impl<H>(desired_fpr: f64)
    where
        H: Default + BuildHasher
    {
        for log_cap in [0, 4, 8, 12, 16, 20, 24, 26] {
            let cap = 1 << log_cap;
            let len = cap;
            let filter = Filter::<H>::with_default_hasher(cap, desired_fpr);
            let mut fp = 0;

            dbg!(log_cap, cap, len, filter.num_slots(), filter.num_bits(), filter.num_hashes());

            for i in 0..len {
                if i % 2 == 0 {
                    fp += u64::from(filter.insert(i));
                }
            }

            for i in 0..len {
                if i % 2 == 0 {
                    // there should be no false negatives, ever
                    assert!(filter.contains(i));
                } else {
                    // count false positives
                    fp += u64::from(filter.contains(i));
                }
            }

            // The observed false positive ratio should be within a reasonable margin
            // of the desired FPR.
            let actual_fpr = fp as f64 / (len as f64 / 2.0); // divide by 2 because we omitted odd elements

            dbg!(std::any::type_name::<H>(), actual_fpr, desired_fpr);
            assert!(actual_fpr <= 3.0 * desired_fpr);
        }
    }

    #[test]
    fn works_with_default_hasher() {
        let handles: Vec<_> = (1..=9)
            .map(|neg_log_fpr| {
                generic_hasher::<RandomState>(f64::powi(10.0, -neg_log_fpr))
            })
            .collect();

        // explicitly join handles so we catch panics
        for h in handles {
            h.join().unwrap();
        }
    }

    #[test]
    fn works_with_3rd_party_hasher() {
        let handles: Vec<_> = (1..=9)
            .map(|neg_log_fpr| {
                generic_hasher::<AHashRandomState>(f64::powi(10.0, -neg_log_fpr))
            })
            .collect();

        // explicitly join handles so we catch panics
        for h in handles {
            h.join().unwrap();
        }
    }

    #[test]
    fn multi_threaded() {
        let num_threads = thread::available_parallelism().unwrap().get();
        let items_per_thread = 1_000_000;
        let desired_fpr = 1.0e-6;
        let filter = Filter::<AHashRandomState>::with_default_hasher(num_threads * items_per_thread, desired_fpr);

        thread::scope(|scope| {
            let mut handles = Vec::with_capacity(num_threads);

            for i in 0..num_threads {
                let filter = &filter;
                let h = scope.spawn(move || {
                    let start_idx = i * items_per_thread;
                    let end_idx = start_idx + items_per_thread;
                    let mut fp = 0;

                    for j in start_idx..end_idx {
                        if j % 3 == 1 {
                            fp += usize::from(filter.insert(j));
                            assert!(filter.contains(j));
                        }
                    }

                    for j in start_idx..end_idx {
                        if j % 3 == 1 {
                            assert!(filter.contains(j));
                        } else {
                            fp += usize::from(filter.contains(j));
                        }
                    }

                    // multiply by 2/3 because we inserted every 3rd item
                    let actual_fpr = fp as f64 / (items_per_thread as f64 * 2.0 / 3.0);

                    dbg!(i, actual_fpr);
                    assert!(actual_fpr < 3.0 * desired_fpr);
                });
                handles.push(h);
            }

            // explicitly join threads to forward panics
            for h in handles {
                h.join().unwrap();
            }
        });
    }
}