Skip to content

bitvec+bench #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[project]
[package]
name = "bloomfilter"
version = "1.0.9"
authors = ["Frank Denis <[email protected]>"]
Expand All @@ -9,11 +9,12 @@ repository = "https://github.com/jedisct1/rust-bloom-filter"
edition = "2018"

[dependencies]
bit-vec = "0.6.3"
getrandom = { version = "0.2.3", optional = true }
siphasher = "0.3.7"
bitvec = "1.0.1"
getrandom = { version = "0.2.8", optional = true }
siphasher = "0.3.10"

[features]
default = ["random"]
random = ["getrandom"]
serde = ["siphasher/serde_std", "bit-vec/serde"]
serde = ["siphasher/serde_std", "bitvec/serde"]
bench = []
28 changes: 28 additions & 0 deletions src/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
extern crate test;
use super::Bloom;
use test::Bencher;

// launch with `cargo bench --features=bench`
// only works with nightly, still marked unstable

#[bench]
fn bench_bloom_set_check_10_80(b: &mut Bencher) {
let mut bloom = Bloom::new(10, 80);
let mut i: usize = 0;
b.iter(|| {
bloom.set(&i);
_ = bloom.check(&i);
i += 1;
})
}

#[bench]
fn bench_bloom_set_check_fp_rate_0_1(b: &mut Bencher) {
let mut bloom = Bloom::new_for_fp_rate(1_000_000_000, 0.01);
let mut i: usize = 0;
b.iter(|| {
bloom.set(&i);
_ = bloom.check(&i);
i += 1;
})
}
28 changes: 17 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

#![warn(non_camel_case_types, non_upper_case_globals, unused_qualifications)]
#![allow(clippy::unreadable_literal, clippy::bool_comparison)]
#![feature(test)]

use bit_vec::BitVec;
use bitvec::prelude::BitVec;
#[cfg(feature = "random")]
use getrandom::getrandom;
use siphasher::sip::SipHasher13;
Expand All @@ -18,24 +19,28 @@ use std::f64;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;

#[cfg(feature = "bench")]
#[cfg(test)]
mod bench;

#[cfg(feature = "serde")]
use siphasher::reexports::serde;

pub mod reexports {
pub use bitvec;
#[cfg(feature = "random")]
pub use ::getrandom;
pub use bit_vec;
pub use getrandom;
pub use siphasher;
#[cfg(feature = "serde")]
pub use siphasher::reexports::serde;
pub use siphasher;
}

/// Bloom filter structure
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(crate = "serde"))]
#[derive(Clone, Debug)]
pub struct Bloom<T: ?Sized> {
bit_vec: BitVec,
bit_vec: BitVec<u8>,
bitmap_bits: u64,
k_num: u32,
sips: [SipHasher13; 2],
Expand All @@ -52,7 +57,8 @@ impl<T: ?Sized> Bloom<T> {
assert!(bitmap_size > 0 && items_count > 0);
let bitmap_bits = (bitmap_size as u64) * 8u64;
let k_num = Self::optimal_k_num(bitmap_bits, items_count);
let bitmap = BitVec::from_elem(bitmap_bits as usize, false);
let mut bitmap = BitVec::new();
bitmap.resize(bitmap_bits as usize, false);
let mut k1 = [0u8; 16];
let mut k2 = [0u8; 16];
k1.copy_from_slice(&seed[0..16]);
Expand Down Expand Up @@ -97,7 +103,7 @@ impl<T: ?Sized> Bloom<T> {
/// Create a bloom filter structure from a previous state given as a `ByteVec` structure.
/// The state is assumed to be retrieved from an existing bloom filter.
pub fn from_bit_vec(
bit_vec: BitVec,
bit_vec: BitVec<u8>,
bitmap_bits: u64,
k_num: u32,
sip_keys: [(u64, u64); 2],
Expand All @@ -123,7 +129,7 @@ impl<T: ?Sized> Bloom<T> {
k_num: u32,
sip_keys: [(u64, u64); 2],
) -> Self {
Self::from_bit_vec(BitVec::from_bytes(bytes), bitmap_bits, k_num, sip_keys)
Self::from_bit_vec(BitVec::from_slice(bytes), bitmap_bits, k_num, sip_keys)
}

/// Compute a recommended bitmap size for items_count items
Expand Down Expand Up @@ -185,11 +191,11 @@ impl<T: ?Sized> Bloom<T> {

/// Return the bitmap as a vector of bytes
pub fn bitmap(&self) -> Vec<u8> {
self.bit_vec.to_bytes()
self.bit_vec.clone().into_vec()
}

/// Return the bitmap as a "BitVec" structure
pub fn bit_vec(&self) -> &BitVec {
pub fn bit_vec(&self) -> &BitVec<u8> {
&self.bit_vec
}

Expand Down Expand Up @@ -234,7 +240,7 @@ impl<T: ?Sized> Bloom<T> {

/// Clear all of the bits in the filter, removing all keys from the set
pub fn clear(&mut self) {
self.bit_vec.clear()
self.bit_vec.fill(false)
}

#[inline]
Expand Down