Some fast bloom filter implemented by Rust for Python and Rust! 10x faster than pybloom!
Project description
fastbloom
A fast bloom filter | counting bloom filter implemented by Rust for Rust and Python!
Language: 简体中文
setup
Python
requirements
Python >= 3.7
install
Install the latest fastbloom version with:
pip install fastbloom-rs
Rust
fastbloom-rs = "{latest}"
Examples
BloomFilter
A Bloom filter is a space-efficient probabilistic data structure, conceived by Burton Howard Bloom in 1970, that is used to test whether an element is a member of a set. False positive matches are possible, but false negatives are not.
Reference: Bloom, B. H. (1970). Space/time trade-offs in hash coding with allowable errors. Communications of the ACM, 13(7), 422-426. Full text article
Python
basic usage
from fastbloom_rs import BloomFilter
bloom = BloomFilter(100_000_000, 0.01)
bloom.add_str('hello')
bloom.add_bytes(b'world')
bloom.add_int(9527)
assert bloom.contains('hello')
assert bloom.contains(b'world')
assert bloom.contains(9527)
assert not bloom.contains('hello world')
build bloom filter from bytes or list
from fastbloom_rs import BloomFilter
bloom = BloomFilter(100_000_000, 0.01)
bloom.add_str('hello')
assert bloom.contains('hello')
bloom2 = BloomFilter.from_bytes(bloom.get_bytes(), bloom.hashes())
assert bloom2.contains('hello')
bloom3 = BloomFilter.from_int_array(bloom.get_int_array(), bloom.hashes())
assert bloom3.contains('hello')
more examples at py_tests.
Rust
use fastbloom_rs::{BloomFilter, FilterBuilder};
let mut bloom = FilterBuilder::new(100_000_000, 0.01).build_bloom_filter();
bloom.add(b"helloworld");
assert_eq!(bloom.contains(b"helloworld"), true);
assert_eq!(bloom.contains(b"helloworld!"), false);
more examples at docs.rs
CountingBloomFilter
A Counting Bloom filter works in a similar manner as a regular Bloom filter; however, it is able to keep track of insertions and deletions. In a counting Bloom filter, each entry in the Bloom filter is a small counter associated with a basic Bloom filter bit.
Reference: F. Bonomi, M. Mitzenmacher, R. Panigrahy, S. Singh, and G. Varghese, “An Improved Construction for Counting Bloom Filters,” in 14th Annual European Symposium on Algorithms, LNCS 4168, 2006
Python
from fastbloom_rs import CountingBloomFilter
cbf = CountingBloomFilter(1000_000, 0.01)
cbf.add('hello')
cbf.add('hello')
assert 'hello' in cbf
cbf.remove('hello')
assert 'hello' in cbf # because 'hello' added twice.
# If add same element larger than 15 times, then remove 15 times the filter will not contain the element.
cbf.remove('hello')
assert 'hello' not in cbf
A CountingBloomFilter has a four bits counter to save hash index, so when insert an
element repeatedly, the counter will spill over quickly. So, you can set
enable_repeat_insert
to False
to check whether the element has added.
if it has added, it will not add again. enable_repeat_insert
default set to True
.
from fastbloom_rs import CountingBloomFilter
cbf = CountingBloomFilter(1000_000, 0.01, False)
cbf.add('hello')
cbf.add('hello') # because enable_repeat_insert=False, this addition will not take effect.
assert 'hello' in cbf
cbf.remove('hello')
assert 'hello' not in cbf
more examples at py_tests.
Rust
use fastbloom_rs::{CountingBloomFilter, FilterBuilder};
let mut builder = FilterBuilder::new(100_000, 0.01);
let mut cbf = builder.build_counting_bloom_filter();
cbf.add(b"helloworld");
assert_eq!(bloom.contains(b"helloworld"), true);
benchmark
computer info
CPU | Memory | OS |
---|---|---|
AMD Ryzen 7 5800U with Radeon Graphics | 16G | Windows 10 |
add one str to bloom filter
Benchmark insert one str to bloom filter:
bloom_add_test time: [41.168 ns 41.199 ns 41.233 ns]
change: [-0.4891% -0.0259% +0.3417%] (p = 0.91 > 0.05)
No change in performance detected.
Found 13 outliers among 100 measurements (13.00%)
1 (1.00%) high mild
12 (12.00%) high severe
add one million to bloom filter
Benchmark loop insert (1..1_000_000).map(|n| { n.to_string() })
to bloom filter:
bloom_add_all_test time: [236.24 ms 236.86 ms 237.55 ms]
change: [-3.4346% -2.9050% -2.3524%] (p = 0.00 < 0.05)
Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) high mild
1 (1.00%) high severe
check one contains in bloom filter
bloom_contains_test time: [42.065 ns 42.102 ns 42.156 ns]
change: [-0.7830% -0.5901% -0.4029%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 15 outliers among 100 measurements (15.00%)
1 (1.00%) low mild
5 (5.00%) high mild
9 (9.00%) high severe
check one not contains in bloom filter
bloom_not_contains_test time: [22.695 ns 22.727 ns 22.773 ns]
change: [-3.1948% -2.9695% -2.7268%] (p = 0.00 < 0.05)
Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
4 (4.00%) high mild
8 (8.00%) high severe
add one str to counting bloom filter
counting_bloom_add_test time: [60.822 ns 60.861 ns 60.912 ns]
change: [+0.2427% +0.3772% +0.5579%] (p = 0.00 < 0.05)
Change within noise threshold.
Found 10 outliers among 100 measurements (10.00%)
1 (1.00%) low severe
4 (4.00%) low mild
1 (1.00%) high mild
4 (4.00%) high severe
add one million to counting bloom filter
Benchmark loop insert (1..1_000_000).map(|n| { n.to_string() })
to counting bloom filter:
counting_bloom_add_million_test
time: [272.48 ms 272.58 ms 272.68 ms]
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) low mild
1 (1.00%) high mild
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for fastbloom_rs-0.5.0-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b7b41f23b273d6e5922460c8c07b18824ee6198916a0aa8a670c08bd8f24837 |
|
MD5 | 8c89a1fcfdc68ae5104ee68fc87081d5 |
|
BLAKE2b-256 | 7cfba2e3c7a705fbe04802b8e79d67a542d9c996dd24cf1702b44d4e85bd43cf |
Hashes for fastbloom_rs-0.5.0-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d32636059de80a272e4a65c3acd070cc5f593848ecaee5ee138b1e8322bce7e |
|
MD5 | 697c996106e48f7323e33a32ac598d3b |
|
BLAKE2b-256 | e9eb725b93908c9d6236d73d68da63d73b88cd6f7e2b08b6e7aede4a5aab1ef8 |
Hashes for fastbloom_rs-0.5.0-cp37-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c2d7e344a6d2cfe61d0ee36f7f2bc198e4cd256188eb8eea0ab3516cf99b7f7 |
|
MD5 | 7a213ff302c08d781958ac4206018e3d |
|
BLAKE2b-256 | 11bac1cebd073c458d0b1f4e9deb0169351129bb2c18b4584df94f18f5f60c1c |
Hashes for fastbloom_rs-0.5.0-cp37-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 705829b86bb97f8017cce4d1cd58368465268c5a94d49b74af5603a06e63e441 |
|
MD5 | 7cb89f1b7b3a65881cf232218f1fea40 |
|
BLAKE2b-256 | e4c3d0af48401fe745e82df640a713009383694f98b10a6f99fe932f803cd8c7 |
Hashes for fastbloom_rs-0.5.0-cp37-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3983477f0c41cd248d22ad1e3d80a0bba11f35f032f779a4c3168a2812657a0a |
|
MD5 | f567574a603d38a354ce7aa362e9cb4f |
|
BLAKE2b-256 | 5633d23a27227e629e20c330a518c449e9738cb41fe89410f5d818a7b93819ef |
Hashes for fastbloom_rs-0.5.0-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 71c97f996559cfc7acfe96d4bed31b6f6e2761197ec8588482835e515cff9da7 |
|
MD5 | b15a539545cded451e6a5379a580ef79 |
|
BLAKE2b-256 | 8794777d90f7818f4163f01897f2c33709a91d8bff3a060b0c770f929588857b |
Hashes for fastbloom_rs-0.5.0-cp37-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 78e0f8db408b44d9130b766e8e131d083ac4d190469bd9f5449c4fa55a631eb8 |
|
MD5 | 742e12c21efefb3846a863cb0f356cd9 |
|
BLAKE2b-256 | b7ca031bc1068ee4a74810fca156a9eec1276e2fe9e822d149f1abc0b82371bc |
Hashes for fastbloom_rs-0.5.0-cp37-abi3-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c1e22ef4a90331a038a23f4dfc6a74506386b5c8b5dd5254237d9f79cf9d539 |
|
MD5 | f1d4794cbe3af18fa3ceb209358190cf |
|
BLAKE2b-256 | 45a9458e15dd23e7ca8e2dcbad2264c46dc51384f3f0b19eb4e436406474817e |