Skip to content

Commit

Permalink
Migrate to a Rust xxHash implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Sep 26, 2020
1 parent 4271cc6 commit 4743e7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -19,3 +19,4 @@ clap = "2.33"
fnv = "1.0"
scalable_bloom_filter = "0.1"
xxhash2 = "0.1"
twox-hash = "1.5.0"
27 changes: 19 additions & 8 deletions src/filters.rs
Expand Up @@ -8,8 +8,10 @@
use clap::*;
use fnv::FnvHashSet;
use scalable_bloom_filter::ScalableBloomFilter;
use twox_hash::XxHash64;

use std::collections::HashSet;
use xxhash2;
use std::hash::Hasher;

// Enumerable filters for clap-rs.
arg_enum! {
Expand Down Expand Up @@ -108,11 +110,8 @@ impl Filter for DigestFilter {
/// Detects a unique value.
#[inline]
fn detect(&mut self, input: &[u8]) -> bool {
// hash to u64 always, for collisions
let digest = xxhash2::hash64(input, 0);

// insert the new digest
self.inner.insert(digest)
// insert as a hashed digest
self.inner.insert(hash(input))
}
}

Expand Down Expand Up @@ -180,8 +179,8 @@ impl Filter for BloomFilter {
/// Detects a unique value.
#[inline]
fn detect(&mut self, input: &[u8]) -> bool {
// // create a digest from the input
let digest = xxhash2::hash64(input, 0);
// create a digest from the input
let digest = hash(input);

// short circuit if duplicated
if self.inner.contains(&digest) {
Expand All @@ -194,6 +193,18 @@ impl Filter for BloomFilter {
}
}

/// Small hash binding around `Hasher`.
fn hash(input: &[u8]) -> u64 {
// create a new default hasher
let mut hasher = XxHash64::default();

// write the bytes to the hasher
hasher.write(input);

// finish the hash
hasher.finish()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 4743e7a

Please sign in to comment.