A fast and memory-efficient Bloom Filter implementation in Python with support for memory mapping, compression, saving/loading, merging, and entropy analysis.
- Bloom filter creation with configurable size, hash functions, and slice parameters
- Memory-mapped bit array for large filters (avoids loading entire filter into RAM)
- Add, query, and update operations
- Save/load filters with bz2 compression using pickle
- Merge two conforming filters
- Statistics: bit usage, hit ratio, entropy, hash ID
- Multiple hash functions: blake2b512, sha3_256, sha256
- Fast mode (single hash) and accurate mode (multiple slices)
- CLI entry point via
python -m fastbloomfilter
- Counting bloom filters (element removal)
- Scalable bloom filters
- Distributed/clustered filters
- Redis or other external storage backends
class BloomFilter:
def __init__(
self,
array_size: int = ((1024 ** 2) * 128),
slices: int = 10,
slice_bits: int = 256,
do_hashing: bool = True,
filename: str | None = None,
fast: bool = False,
data_is_hex: bool = False,
use_mmap: bool = False,
mmap_file: str | None = None,
memory_threshold: int = (1024 ** 2) * 64
) -> None: ...Parameters:
array_size: Size of filter in bytes (default: 128MB)slices: Number of hash functions to useslice_bits: Bits per hash slicedo_hashing: Whether to hash input valuesfilename: Path to load/save filterfast: Use single hash mode (faster, less accurate)data_is_hex: Input data is hexadecimaluse_mmap: Force memory mappingmmap_file: Path for memory-mapped filememory_threshold: Auto-enable mmap above this size
Methods:
add(value: str) -> None: Add a value to the filterquery(value: str) -> bool: Check if value might be in filterupdate(value: str) -> bool: Query and add if not present; returns True if already existedsave(filename: str | None = None) -> bool: Save filter to compressed pickleload(filename: str | None = None) -> bool: Load filter from filestat() -> None: Print usage statisticsinfo() -> None: Print full filter infocalc_capacity(error_rate: float, capacity: int) -> int: Calculate required bit countcalc_entropy() -> float: Calculate and print Shannon entropycalc_hashid() -> str: Calculate filter hash IDclose() -> None: Release resources
Magic Methods:
__getitem__(value: str) -> bool: Alias for query__add__(other: BloomFilter) -> BloomFilter: Merge two filters
def blake2b512(s: str) -> hashlib.HASH: ...
def sha3(s: str) -> hashlib.HASH: ...
def sha256(s: str) -> hashlib.HASH: ...
def shannon_entropy(data: bytes, iterator: Iterable | None = None) -> float: ...- Filter Storage: bz2-compressed pickle (.bz2)
- Input Values: UTF-8 encoded strings
- Hash Output: Hexadecimal digest strings
- Empty filter creation (array_size=0) - should work but with warning
- Querying non-existent element - returns False (no false negatives)
- Adding duplicate values - silently succeeds, bits already set
- Loading corrupted file - returns False, prints error to stderr
- Memory-mapped file on read-only filesystem - raises exception
- Merging non-conforming filters (different sizes) - prints error, no merge
- Very large filters (GB+) - uses memory mapping automatically
- Fast mode vs accurate mode trade-offs
- Target: Python 3.11+
- Memory: Auto-switches to memory mapping above 64MB threshold
- Hash functions: blake2b512 preferred, falls back to sha3_256
- Dependencies: bitarray, tqdm (for merge progress)