Debian Codegraph @

README.md

raw symbols SCIP
1 # fastbloom
2 [![Github](https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github)](https://github.com/tomtomwombat/fastbloom)
3 [![Crates.io](https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust)](https://crates.io/crates/fastbloom)
4 [![docs.rs](https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs)](https://docs.rs/fastbloom)
5 ![Downloads](https://img.shields.io/crates/d/fastbloom?style=for-the-badge)
6
7 The fastest Bloom filter in Rust. No accuracy compromises. Full concurrency support and compatible with any hasher.
8
9 ## Overview
10
11 fastbloom is a fast, flexible, and accurate Bloom filter implemented in Rust. fastbloom's default hasher is SipHash-1-3 using randomized keys but can be seeded or configured to use any hasher. fastbloom is 2-20 times faster and magnitudes more accurate than existing Bloom filter implementations. fastbloom's `AtomicBloomFilter` is a concurrent Bloom filter that avoids lock contention.
12
13 ## Usage
14
15 Due to a different (improved!) algorithm in 0.17.x, Bloomfilters have incompatible serialization/deserialization with prior versions.
16
17 ```toml
18 # Cargo.toml
19 [dependencies]
20 fastbloom = "0.17.0"
21 ```
22 Basic usage:
23 ```rust
24 use fastbloom::BloomFilter;
25
26 let mut filter = BloomFilter::with_num_bits(1024).expected_items(2);
27 filter.insert("42");
28 filter.insert("🦀");
29 ```
30 Instantiate with a target false positive rate:
31 ```rust
32 use fastbloom::BloomFilter;
33
34 let filter = BloomFilter::with_false_pos(0.001).items(["42", "🦀"].iter());
35 assert!(filter.contains("42"));
36 assert!(filter.contains("🦀"));
37 ```
38 Use any hasher:
39 ```rust
40 use fastbloom::BloomFilter;
41 use foldhash::fast::RandomState;
42
43 let filter = BloomFilter::with_num_bits(1024)
44 .hasher(RandomState::default())
45 .items(["42", "🦀"].iter());
46 ```
47 Full concurrency support. `AtomicBloomFilter` is a drop-in replacement for `RwLock<OtherBloomFilter>` because all methods take `&self`:
48 ```rust
49 use fastbloom::AtomicBloomFilter;
50
51 let filter = AtomicBloomFilter::with_num_bits(1024).expected_items(2);
52 filter.insert("42");
53 filter.insert("🦀");
54 ```
55
56 ## Background
57 Bloom filters are space-efficient approximate membership set data structures supported by an underlying bit array to track item membership. To insert/check membership, a number of bits are set/checked at positions based on the item's hash. False positives from a membership check are possible, but false negatives are not. Once constructed, neither the Bloom filter's underlying memory usage nor number of bits per item change. [See more.](https://en.wikipedia.org/wiki/Bloom_filter)
58
59 ```text
60 hash(4) ──────┬─────┬───────────────┐
61 ↓ ↓ ↓
62 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0
63 ↑ ↑ ↑
64 └───────────┴───────────┴──── hash(3) (not in the set)
65 ```
66
67 ## Implementation
68
69 fastbloom is blazingly fast because it efficiently derives many index bits from **only one real hash per item** and leverages other research findings on Bloom filters. fastbloom employs "hash composition" on two 32-bit halves of an original 64-bit hash. Each subsequent hash is derived by combining the original hash value with a different constant using modular arithmetic and bitwise operations. This results in a set of hash functions that are effectively independent and uniformly distributed, even though they are derived from the same original hash function. Computing the composition of two original hashes is faster than re-computing the hash with a different seed. This technique is [explained in depth in this paper.](https://www.eecs.harvard.edu/~michaelm/postscripts/rsa2008.pdf)
70
71 ## Speed
72 - AMD Ryzen 9 5900X 12-Core Processor (3.70 GHz)
73 - 64-bit operating system, x64-based processor
74
75 ![perf-non-member](https://github.com/user-attachments/assets/a825d2f7-4cd7-4b6b-a447-fd7f3dab356b)
76 ![perf-member](https://github.com/user-attachments/assets/998f9470-b91f-460c-ad2d-1f197160c210)
77 > Hashers used:
78 > - xxhash: sbbf
79 > - Sip1-3: bloom, bloomfilter, probabilistic-collections
80 > - foldhash: fastbloom
81 >
82 > [Benchmark source](https://github.com/tomtomwombat/bench-bloom-filters)
83
84 ## Accuracy
85
86 fastbloom does not compromise accuracy. Below is a comparison of false positive rates with other Bloom filter crates:
87
88 ![fp](https://github.com/user-attachments/assets/17ddaab7-c63f-456a-9e2e-b22c7f994386)
89
90 [Benchmark source](https://github.com/tomtomwombat/bench-bloom-filters)
91
92 ## Available Features
93
94 - **`rand`** - Enabled by default, this has the `DefaultHasher` source its random state using `thread_rng()` instead of hardware sources. Getting entropy from a user-space source is considerably faster, but requires additional dependencies to achieve this. Disabling this feature by using `default-features = false` makes `DefaultHasher` source its entropy using `foldhash`, which will have a much simpler code footprint at the expense of speed.
95 - **`serde`** - `BloomFilter`s implement `Serialize` and `Deserialize` when possible.
96 - **`loom`** - `AtomicBloomFilter`s use [loom](https://github.com/tokio-rs/loom) atomics, making it compatible with loom testing.
97
98 ## References
99 - [Bloom filter - Wikipedia](https://en.wikipedia.org/wiki/Bloom_filter)
100 - [Bloom filters debunked: Dispelling 30 Years of bad math with Coq!](https://gopiandcode.uk/logs/log-bloomfilters-debunked.html)
101 - [Bloom Filter Interactive Demonstration](https://www.jasondavies.com/bloomfilter/)
102 - [Cache-, Hash- and Space-Efficient Bloom Filters](https://web.archive.org/web/20070623102632/http://algo2.iti.uni-karlsruhe.de/singler/publications/cacheefficientbloomfilters-wea2007.pdf)
103 - [Less hashing, same performance: Building a better Bloom filter](https://www.eecs.harvard.edu/~michaelm/postscripts/rsa2008.pdf)
104 - [A fast alternative to the modulo reduction](https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/)
105
106 ## License
107
108 Licensed under either of
109
110 * Apache License, Version 2.0
111 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
112 * MIT license
113 ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
114
115 at your option.
116
117 ## Contribution
118
119 Unless you explicitly state otherwise, any contribution intentionally submitted
120 for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
121 dual licensed as above, without any additional terms or conditions.