Skip to main content

Crate qfilter

Crate qfilter 

Source
Expand description

Approximate Membership Query Filter (AMQ-Filter) based on the Rank Select Quotient Filter (RSQF).

This is a small and flexible general-purpose AMQ-Filter, it not only supports approximate membership testing like a bloom filter but also deletions, merging, resizing and serde serialization.

§Example

let mut f = qfilter::Filter::new(1000000, 0.01).unwrap();
for i in 0..1000 {
    f.insert(i).unwrap();
}
for i in 0..1000 {
    assert!(f.contains(i));
}

§Hasher

Methods accepting T: Hash are provided for convenience using foldhash-portable, which offers high performance and stability across platforms. Note that a fixed seed is used (no DoS resistance) and #[derive(Hash)] output is not guaranteed stable across Rust compiler versions.

§Custom hasher

Filter supports a custom BuildHasher via its S type parameter (similar to HashMap). Use Filter::new_with_hasher() and related constructors. Caveats:

  • The hasher is not serialized. On deserialization it is reconstructed via S::default(). If that doesn’t produce the correct hasher (e.g. random-seeded hashers), use Filter::with_hasher() to restore it.
  • Filters being merged must use the same hasher and seed.
  • The same hasher instance (or equivalent seed) must be used for all operations.

§Filter size

For a given capacity and error probability the RSQF may require significantly less space than the equivalent bloom filter or other AMQ-Filters.

Bits per itemError probability when fullBits per item (cont.)Error (cont.)
3.1250.36219.1256.87e-06
4.1250.20120.1253.43e-06
5.1250.10621.1251.72e-06
6.1250.054722.1258.58e-07
7.1250.027723.1254.29e-07
8.1250.01424.1252.15e-07
9.1250.0070125.1251.07e-07
10.1250.0035126.1255.36e-08
11.1250.0017627.1252.68e-08
12.1250.00087928.1251.34e-08
13.1250.00043929.1256.71e-09
14.1250.0002230.1253.35e-09
15.1250.0001131.1251.68e-09
16.1255.49e-0532.1258.38e-10
17.1252.75e-05....
18.1251.37e-05....

§Performance

  • Lookup/Insert/Delete: O(1) expected case, O(n) worst case with pathological hash collisions
  • Memory overhead: 2.125 bits per slot for metadata (occupieds, runends, offset)
  • Cache efficiency: Block-based layout with 64-slot blocks improves cache locality

Performance degrades gracefully as occupancy increases. The filter automatically limits occupancy to 95% to maintain good performance.

§Legacy x86_64 CPUs support

The implementation assumes popcnt and BMI2 (pdep, tzcnt) instructions are available when compiling for x86_64 targets. These instructions are available on CPUS released after 2015. If they are not available, the Filter constructor will panic.

The legacy_x86_64_support feature enables support for older x86_64 CPUs by using portable fallbacks.

Structs§

Builder
Builder for constructing a Filter from fingerprints in sorted (ascending) order.
Filter
Approximate Membership Query Filter (AMQ-Filter) based on the Rank Select Quotient Filter (RSQF).
FilterSpecs
Specifications for a filter with given parameters.
FingerprintIter
An iterator over the fingerprints stored in a Filter or FilterRef.
StableBuildHasher
A BuildHasher that produces StableHasher instances with a fixed seed.

Enums§

Error
Errors returned by Filter operations.

Functions§

compute_fingerprint_with_hasher
Hashes an item and truncates it to a fingerprint of the given bit size.
filter_specs
Computes filter specifications for a given capacity and false positive rate.
truncate_to_fingerprint
Truncates a hash to a fingerprint of the given bit size.

Type Aliases§

FilterRef
A read-only, borrowed view of a Filter.