Skip to content

Commit

Permalink
Add xxhash32
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Sep 12, 2023
1 parent a17bd8d commit a85413e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion experimental/hashcmp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ all-features = true
t1ha = "0.1.0"
wyhash = "0.5.0"
twox-hash = "1.6.3"
xxhash-rust = { version = "0.8.7", features = ["xxh64"] }
xxhash-rust = { version = "0.8.7", features = ["xxh64", "xxh32"] }

[dev-dependencies]
55 changes: 55 additions & 0 deletions experimental/hashcmp/src/bin/twox_hash_32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#![no_main] // https://github.com/unicode-org/icu4x/issues/395

use std::hash::Hasher;
use std::collections::BTreeSet;

const ALPHANUMS: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";

#[no_mangle]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
let seed = std::hint::black_box(2023u32);
let mut hashes = BTreeSet::new();
let mut hashes_32bit = BTreeSet::new();
let mut record = |hashed| {
hashes.insert(hashed);
hashes_32bit.insert(hashed as u32);
};
let mut cases = 0;
for i in 0u8..255 {
let mut hasher = twox_hash::XxHash32::with_seed(seed);
hasher.write_u8(i);
record(hasher.finish());
cases += 1;
}
for a in ALPHANUMS.iter() {
for b in ALPHANUMS.iter() {
for c in ALPHANUMS.iter() {
let mut hasher = twox_hash::XxHash32::with_seed(seed);
hasher.write_u8(*a);
hasher.write_u8(*b);
hasher.write_u8(*c);
record(hasher.finish());
cases += 1;
}
}
}
for start in 0..63 {
for stride in 1..=63 {
for count in 4..=63 {
let mut hasher = twox_hash::XxHash32::with_seed(seed);
for i in 0..count {
let j = (start + i*stride) % 63;
hasher.write_u8(ALPHANUMS[j]);
}
record(hasher.finish());
cases += 1;
}
}
}
println!("unique hashes: {} / {} (32-bit: {})", hashes.len(), cases, hashes_32bit.len());
0
}
52 changes: 52 additions & 0 deletions experimental/hashcmp/src/bin/xxhash_rust_32.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#![no_main] // https://github.com/unicode-org/icu4x/issues/395

use std::collections::BTreeSet;

const ALPHANUMS: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_";

#[no_mangle]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
let seed = std::hint::black_box(2023u32);
let mut hashes = BTreeSet::new();
let mut hashes_32bit = BTreeSet::new();
let mut record = |hashed| {
hashes.insert(hashed);
hashes_32bit.insert(hashed as u32);
};
let mut cases = 0;
for i in 0u8..255 {
let mut hasher = xxhash_rust::xxh32::Xxh32::new(seed);
hasher.update(&[i]);
record(hasher.digest());
cases += 1;
}
for a in ALPHANUMS.iter() {
for b in ALPHANUMS.iter() {
for c in ALPHANUMS.iter() {
let mut hasher = xxhash_rust::xxh32::Xxh32::new(seed);
hasher.update(&[*a, *b, *c]);
record(hasher.digest());
cases += 1;
}
}
}
for start in 0..63 {
for stride in 1..=63 {
for count in 4..=63 {
let mut hasher = xxhash_rust::xxh32::Xxh32::new(seed);
for i in 0..count {
let j = (start + i*stride) % 63;
hasher.update(&ALPHANUMS[j..=j]);
}
record(hasher.digest());
cases += 1;
}
}
}
println!("unique hashes: {} / {} (32-bit: {})", hashes.len(), cases, hashes_32bit.len());
0
}

0 comments on commit a85413e

Please sign in to comment.