-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize hasher and add hasher benchmark
- Loading branch information
Showing
5 changed files
with
123 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,8 @@ harness = false | |
|
||
[[bench]] | ||
name = "ilp" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "hashset" | ||
harness = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use fnv::FnvHashSet; | ||
use gxhash::*; | ||
use twox_hash::xxh3; | ||
use std::collections::HashSet; | ||
use std::hash::BuildHasherDefault; | ||
|
||
fn hashmap_insertion(c: &mut Criterion) { | ||
|
||
// Long keys | ||
benchmark_for_string(c, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); | ||
|
||
// Medium keys | ||
benchmark_for_string(c, "https://github.com/ogxd/gxhash"); | ||
|
||
// Short keys | ||
benchmark_for_string(c, "gxhash"); | ||
} | ||
|
||
fn benchmark_for_string(c: &mut Criterion, string: &str) { | ||
let mut group = c.benchmark_group(format!("HashSet<&str[{}]>", string.len())); | ||
|
||
let mut set = HashSet::new(); | ||
group.bench_function("Default Hasher", |b| { | ||
b.iter(|| set.insert(string)) | ||
}); | ||
|
||
let mut set = GxHashSet::default(); | ||
group.bench_function("GxHash", |b| { | ||
b.iter(|| set.insert(string)) | ||
}); | ||
|
||
let mut set = HashSet::<&str, BuildHasherDefault<xxh3::Hash64>>::default(); | ||
group.bench_function("XxHash", |b| { | ||
b.iter(|| set.insert(string)) | ||
}); | ||
|
||
let mut set = FnvHashSet::default(); | ||
group.bench_function("FNV-1a", |b| { | ||
b.iter(|| set.insert(string)) | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, hashmap_insertion); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::hash::{Hasher, BuildHasherDefault}; | ||
use std::collections::{HashMap, HashSet}; | ||
use crate::gxhash::*; | ||
use crate::gxhash::platform::*; | ||
|
||
pub struct GxHasher(State); | ||
|
||
impl Default for GxHasher { | ||
#[inline] | ||
fn default() -> GxHasher { | ||
GxHasher(unsafe { create_empty() }) | ||
} | ||
} | ||
|
||
impl GxHasher { | ||
#[inline] | ||
pub fn with_seed(seed: i32) -> GxHasher { | ||
// Use gxhash64 to generate an initial state from a seed | ||
GxHasher(unsafe { gxhash(&[], create_seed(seed)) }) | ||
} | ||
} | ||
|
||
impl Hasher for GxHasher { | ||
#[inline] | ||
fn finish(&self) -> u64 { | ||
unsafe { | ||
let p = &self.0 as *const State as *const u64; | ||
*p | ||
} | ||
} | ||
|
||
#[inline] | ||
fn write(&mut self, bytes: &[u8]) { | ||
self.0 = unsafe { gxhash(bytes, self.0) }; | ||
} | ||
} | ||
|
||
/// A builder for default FNV hashers. | ||
pub type GxBuildHasher = BuildHasherDefault<GxHasher>; | ||
|
||
/// A `HashMap` using a default GxHash hasher. | ||
//#[cfg(feature = "std")] | ||
pub type GxHashMap<K, V> = HashMap<K, V, GxBuildHasher>; | ||
|
||
/// A `HashSet` using a default GxHash hasher. | ||
//#[cfg(feature = "std")] | ||
pub type GxHashSet<T> = HashSet<T, GxBuildHasher>; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn hasher_works() { | ||
let mut hashset = GxHashSet::default(); | ||
assert!(hashset.insert(1234)); | ||
assert!(!hashset.insert(1234)); | ||
assert!(hashset.insert(42)); | ||
|
||
let mut hashset = GxHashSet::default(); | ||
assert!(hashset.insert("hello")); | ||
assert!(hashset.insert("world")); | ||
assert!(!hashset.insert("hello")); | ||
assert!(hashset.insert("bye")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,7 @@ | |
#![feature(stdsimd)] | ||
|
||
mod gxhash; | ||
mod hasher; | ||
|
||
pub use gxhash::*; | ||
pub use gxhash::*; | ||
pub use hasher::*; |