Skip to content

Commit be373ee

Browse files
committed
reworks
1 parent 885d8db commit be373ee

8 files changed

Lines changed: 39 additions & 27 deletions

File tree

crates/geo_filters/docs/choosing-a-hash-function.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ to `BuildHasher::hash_one`.
2222
The following is an example of some incorrect code which produces nonsense results:
2323

2424
```rust
25+
use std::hash::RandomState;
26+
2527
// Implement our marker trait for `RandomState`.
2628
// You should _NOT_ do this as `RandomState::default` does not produce
2729
// reproducible hashers.

crates/geo_filters/src/build_hasher.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ pub trait ReproducibleBuildHasher: BuildHasher + Default + Clone {
2222
debug_assert_eq!(
2323
Self::default().build_hasher().finish(),
2424
Self::default().build_hasher().finish(),
25-
"Hashers produced by GeoFilterBuildHasher do not produce the same output with the same input"
25+
"Hashers produced by ReproducibleBuildHasher do not produce the same output with the same input"
2626
);
2727
}
2828
}
2929

30-
// Note that this `BuildHasher` has a consistent implementation of `Default`
31-
// but is NOT stable across releases of Rust. It is therefore dangerous
32-
// to use if you plan on serializing the geofilters and reusing them due
33-
// to the fact that you can serialize a filter made with one version and
34-
// deserialize with another version of the hasher factor.
30+
/// Note that this `BuildHasher` has a consistent implementation of `Default`
31+
/// but is NOT stable across releases of Rust. It is therefore dangerous
32+
/// to use if you plan on serializing the geofilters and reusing them due
33+
/// to the fact that you can serialize a filter made with one version and
34+
/// deserialize with another version of the hasher factor.
3535
pub type DefaultBuildHasher = BuildHasherDefault<DefaultHasher>;
3636

3737
impl ReproducibleBuildHasher for DefaultBuildHasher {}

crates/geo_filters/src/config.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,10 @@ pub(crate) fn take_ref<I: Iterator>(iter: &mut I, n: usize) -> impl Iterator<Ite
355355
pub(crate) mod tests {
356356
use rand::{RngCore, SeedableRng};
357357

358-
use crate::{build_hasher::DefaultBuildHasher, Count, Method};
358+
use crate::{Count, Method};
359359

360360
/// Runs estimation trials and returns the average precision and variance.
361-
pub(crate) fn test_estimate<M: Method, C: Count<M, DefaultBuildHasher>>(
362-
f: impl Fn() -> C,
363-
) -> (f32, f32) {
361+
pub(crate) fn test_estimate<M: Method, C: Count<M>>(f: impl Fn() -> C) -> (f32, f32) {
364362
let mut rnd = rand::rngs::StdRng::from_os_rng();
365363
let cnt = 10000usize;
366364
let mut avg_precision = 0.0;

crates/geo_filters/src/diff_count.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ pub(crate) fn masked<C: GeoConfig<Diff>>(
305305
/// This operation corresponds to computing the symmetric difference of the two
306306
/// sets represented by the GeoDiffCounts.
307307
///
308-
/// SAFETY: The two GeoDiffCounts must have the same configuration and hash builder.
308+
/// # Panics
309+
///
310+
/// Panics if the configuration of the geofilters is not identical.
309311
pub(crate) fn xor<C: GeoConfig<Diff>>(
310312
diff_count: &GeoDiffCount<'_, C>,
311313
other: &GeoDiffCount<'_, C>,
@@ -321,7 +323,9 @@ pub(crate) fn xor<C: GeoConfig<Diff>>(
321323
)
322324
}
323325

324-
impl<C: GeoConfig<Diff>> Count<Diff, C::BuildHasher> for GeoDiffCount<'_, C> {
326+
impl<C: GeoConfig<Diff>> Count<Diff> for GeoDiffCount<'_, C> {
327+
type BuildHasher = C::BuildHasher;
328+
325329
fn push_hash(&mut self, hash: u64) {
326330
self.xor_bit(self.config.hash_to_bucket(hash));
327331
}

crates/geo_filters/src/distinct_count.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ impl<C: GeoConfig<Distinct>> GeoDistinctCount<'_, C> {
128128
}
129129
}
130130

131-
impl<C: GeoConfig<Distinct>> Count<Distinct, C::BuildHasher> for GeoDistinctCount<'_, C> {
131+
impl<C: GeoConfig<Distinct>> Count<Distinct> for GeoDistinctCount<'_, C> {
132+
type BuildHasher = C::BuildHasher;
133+
132134
fn push_hash(&mut self, hash: u64) {
133135
self.set_bit(self.config.hash_to_bucket(hash));
134136
}

crates/geo_filters/src/evaluation/hll.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ impl Hasher for NoopHasher {
9191

9292
#[inline]
9393
fn write(&mut self, _: &[u8]) {
94-
unimplemented!("NoopHasher only supports writing u64 values");
94+
unimplemented!(
95+
"NoopHasher does not support arbitrary byte sequences. Use write_u64 instead"
96+
);
9597
}
9698

9799
#[inline]
@@ -110,7 +112,9 @@ impl BuildHasher for BuildNoopHasher {
110112

111113
impl ReproducibleBuildHasher for BuildNoopHasher {}
112114

113-
impl<C: HllConfig> Count<Distinct, DefaultBuildHasher> for Hll<C> {
115+
impl<C: HllConfig> Count<Distinct> for Hll<C> {
116+
type BuildHasher = DefaultBuildHasher;
117+
114118
fn push_hash(&mut self, hash: u64) {
115119
self.inner.borrow_mut().insert(&hash)
116120
}

crates/geo_filters/src/evaluation/simulation.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,35 @@ pub trait SimulationCount {
2222
}
2323
impl<C: GeoConfig<Diff> + Clone> SimulationCount for GeoDiffCount<'_, C> {
2424
fn push_hash(&mut self, hash: u64) {
25-
<Self as Count<_, _>>::push_hash(self, hash)
25+
<Self as Count<_>>::push_hash(self, hash)
2626
}
2727
fn size(&self) -> f32 {
28-
<Self as Count<_, _>>::size(self)
28+
<Self as Count<_>>::size(self)
2929
}
3030
fn bytes_in_memory(&self) -> usize {
31-
<Self as Count<_, _>>::bytes_in_memory(self)
31+
<Self as Count<_>>::bytes_in_memory(self)
3232
}
3333
}
3434
impl<C: GeoConfig<Distinct>> SimulationCount for GeoDistinctCount<'_, C> {
3535
fn push_hash(&mut self, hash: u64) {
36-
<Self as Count<_, _>>::push_hash(self, hash)
36+
<Self as Count<_>>::push_hash(self, hash)
3737
}
3838
fn size(&self) -> f32 {
39-
<Self as Count<_, _>>::size(self)
39+
<Self as Count<_>>::size(self)
4040
}
4141
fn bytes_in_memory(&self) -> usize {
42-
<Self as Count<_, _>>::bytes_in_memory(self)
42+
<Self as Count<_>>::bytes_in_memory(self)
4343
}
4444
}
4545
impl<C: HllConfig> SimulationCount for Hll<C> {
4646
fn push_hash(&mut self, hash: u64) {
47-
<Self as Count<_, _>>::push_hash(self, hash)
47+
<Self as Count<_>>::push_hash(self, hash)
4848
}
4949
fn size(&self) -> f32 {
50-
<Self as Count<_, _>>::size(self)
50+
<Self as Count<_>>::size(self)
5151
}
5252
fn bytes_in_memory(&self) -> usize {
53-
<Self as Count<_, _>>::bytes_in_memory(self)
53+
<Self as Count<_>>::bytes_in_memory(self)
5454
}
5555
}
5656

crates/geo_filters/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub mod distinct_count;
1414
#[cfg(feature = "evaluation")]
1515
pub mod evaluation;
1616

17-
use std::hash::Hash;
17+
use std::hash::{BuildHasher as _, Hash};
1818

1919
use crate::build_hasher::ReproducibleBuildHasher;
2020

@@ -32,13 +32,15 @@ pub struct Distinct {}
3232
impl Method for Distinct {}
3333

3434
/// Trait for types solving the set cardinality estimation problem.
35-
pub trait Count<M: Method, H: ReproducibleBuildHasher> {
35+
pub trait Count<M: Method> {
36+
type BuildHasher: ReproducibleBuildHasher;
37+
3638
/// Add the given hash to the set.
3739
fn push_hash(&mut self, hash: u64);
3840

3941
/// Add the hash of the given item, computed with the configured hasher, to the set.
4042
fn push<I: Hash>(&mut self, item: I) {
41-
let build_hasher = H::default();
43+
let build_hasher = Self::BuildHasher::default();
4244
self.push_hash(build_hasher.hash_one(item));
4345
}
4446

0 commit comments

Comments
 (0)