Skip to content

Commit

Permalink
Strengthen fastpath for u64 hashes
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Kaitchuck <[email protected]>
  • Loading branch information
tkaitchuck committed Feb 27, 2024
1 parent 9f052f8 commit cd41f90
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fxhash = "0.2.1"
hex = "0.4.2"
rand = "0.8.5"
serde_json = "1.0.59"
hashbrown = "0.12.3"
hashbrown = "0.14.3"

[package.metadata.docs.rs]
rustc-args = ["-C", "target-feature=+aes"]
Expand Down
2 changes: 1 addition & 1 deletion src/fallback_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ impl Hasher for AHasherU64 {
#[inline]
fn write_u64(&mut self, i: u64) {
self.buffer = folded_multiply(i ^ self.buffer, MULTIPLE);
self.pad = self.pad.wrapping_add(i);
}

#[inline]
Expand Down Expand Up @@ -341,7 +342,6 @@ impl Hasher for AHasherStr {

#[cfg(test)]
mod tests {
use crate::convert::Convert;
use crate::fallback_hash::*;

#[test]
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ mod test {
use crate::specialize::CallHasher;
use crate::*;
use std::collections::HashMap;
use std::hash::Hash;

#[test]
fn test_ahash_alias_map_construction() {
Expand Down
1 change: 0 additions & 1 deletion src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ pub(crate) fn add_in_length(enc: &mut u128, len: u64) {
#[cfg(test)]
mod test {
use super::*;
use crate::convert::Convert;

// This is code to search for the shuffle constant
//
Expand Down
40 changes: 40 additions & 0 deletions tests/map_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,46 @@ fn test_key_ref() {
assert!(m.contains(&b"hello"[..]));
}

#[cfg(feature = "std")]
#[test]
fn test_byte_dist() {
use rand::{rngs::StdRng, SeedableRng, Rng};

let mut r = StdRng::seed_from_u64(0xe786_c22b_119c_1479);
for _round in 0..100 {
let mut table: [bool; 256 * 8] = [false; 256 * 8];
let hasher = RandomState::with_seeds(r.gen(), r.gen(), r.gen(), r.gen());
for i in 0..128 {
let mut keys: [u8; 8] = hasher.hash_one(i as u64).to_ne_bytes();
for idx in 0..8 {
while table[idx * 256 + keys[idx] as usize] {
keys[idx] = keys[idx].wrapping_add(1);
}
table[idx * 256 + keys[idx] as usize] = true;
}
}

for idx in 0..8 {
let mut len = 0;
let mut total_len = 0;
let mut num_seq = 0;
for i in 0..256 {
if table[idx * 256 + i] {
len += 1;
} else if len != 0 {
num_seq += 1;
total_len += len;
len = 0;
}
}
let mean = total_len as f32 / num_seq as f32;
println!("Mean sequence length = {}", mean);
assert!(mean > 1.70);
assert!(mean < 3.375);
}
}
}


fn ahash_vec<H: Hash>(b: &Vec<H>) -> u64 {
let mut total: u64 = 0;
Expand Down

0 comments on commit cd41f90

Please sign in to comment.