Skip to content

Commit

Permalink
Merge pull request #18 from samfrench/update-hash-library
Browse files Browse the repository at this point in the history
Use murmur3 for hashing
  • Loading branch information
MarkBiesheuvel authored Dec 9, 2024
2 parents 3909a53 + 4a3679b commit e6d84bf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion optimizely/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
serde_json = "1.0"
thiserror = "1.0"
error-stack = "0.5"
fasthash = "0.4"
murmur3 = "0.5.2"
log = "0.4"

[dependencies.serde]
Expand Down
5 changes: 3 additions & 2 deletions optimizely/src/client/user.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// External imports
use fasthash::murmur3::hash32_with_seed as murmur3_hash;
use murmur3::murmur3_32 as murmur3_hash;
use std::collections::HashMap;

// Imports from crate
Expand Down Expand Up @@ -223,7 +223,8 @@ impl UserContext<'_> {

// To hash the bucket key it needs to be converted to an array of `u8` bytes
// Use Murmur3 (32-bit) with seed
let hash_value = murmur3_hash(bucketing_key.as_bytes(), HASH_SEED);
let mut bytes = bucketing_key.as_bytes();
let hash_value = murmur3_hash(&mut bytes, HASH_SEED).unwrap();

// Bring the hash into a range of 0 to 10_000
let bucket_value = ((hash_value as f64) / (u32::MAX as f64) * MAX_OF_RANGE) as u64;
Expand Down

0 comments on commit e6d84bf

Please sign in to comment.