diff --git a/Cargo.toml b/Cargo.toml index 9802482..b8c542f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,7 @@ default = ["serde_support"] precomputed-hash = "0.1" serde = { version = "1", optional = true } malloc_size_of = { version = "0.1", default-features = false, optional = true } -phf_shared = "0.14" +phf_shared = { version = "0.14", features = ["ptrhash"] } new_debug_unreachable = "1.0.2" parking_lot = "0.12" diff --git a/src/atom.rs b/src/atom.rs index 31dafd2..59d7df5 100644 --- a/src/atom.rs +++ b/src/atom.rs @@ -214,10 +214,16 @@ impl Atom { Self::try_static_internal(string_to_add).ok() } - fn try_static_internal(string_to_add: &str) -> Result { + fn try_static_internal(string_to_add: &str) -> Result { let static_set = Static::get(); - let hash = phf_shared::hash(string_to_add, &static_set.key); - let index = phf_shared::get_index(&hash, static_set.disps, static_set.atoms.len()); + let hash = phf_shared::ptrhash::hash(string_to_add, &static_set.seed); + let index = phf_shared::ptrhash::get_index( + static_set.seed, + hash, + static_set.pilots, + static_set.remap, + static_set.atoms.len(), + ); if static_set.atoms[index as usize] == string_to_add { Ok(Self::pack_static(index)) @@ -272,9 +278,6 @@ impl<'a, Static: StaticAtomSet> From> for Atom { } } else { Self::try_static_internal(&string_to_add).unwrap_or_else(|hash| { - // Reconstitute 64-bit `Hash128::h1` - // https://docs.rs/phf_shared/0.14.0/src/phf_shared/lib.rs.html#45-54 - let hash = (hash.g as u64) << 32 | (hash.f1 as u64); let ptr: std::ptr::NonNull = dynamic_set().insert(string_to_add, hash); let data = ptr.as_ptr().expose_provenance() as u64; debug_assert!(0 == data & TAG_MASK); diff --git a/src/static_sets.rs b/src/static_sets.rs index 10f03bb..e9e8bbd 100644 --- a/src/static_sets.rs +++ b/src/static_sets.rs @@ -29,9 +29,11 @@ pub trait StaticAtomSet: Ord { /// [Hash, Displace and Compress]: http://cmph.sourceforge.net/papers/esa09.pdf pub struct PhfStrSet { #[doc(hidden)] - pub key: u64, + pub seed: u64, #[doc(hidden)] - pub disps: &'static [(u32, u32)], + pub pilots: &'static [u8], + #[doc(hidden)] + pub remap: &'static [u32], #[doc(hidden)] pub atoms: &'static [&'static str], #[doc(hidden)] @@ -47,8 +49,9 @@ impl StaticAtomSet for EmptyStaticAtomSet { // The name is a lie: this set is not empty (it contains the empty string) // but that’s only to avoid divisions by zero in rust-phf. static SET: PhfStrSet = PhfStrSet { - key: 0, - disps: &[(0, 0)], + seed: 0, + pilots: &[0], + remap: &[0], atoms: &[""], // "" SipHash'd, and xored with u64_hash_to_u32. hashes: &[0x3ddddef3], diff --git a/string-cache-codegen/Cargo.toml b/string-cache-codegen/Cargo.toml index 11a5b52..d751d4c 100644 --- a/string-cache-codegen/Cargo.toml +++ b/string-cache-codegen/Cargo.toml @@ -13,7 +13,7 @@ name = "string_cache_codegen" path = "lib.rs" [dependencies] -phf_generator = "0.14" -phf_shared = "0.14" +phf_generator = { version = "0.14", features = ["ptrhash"] } +phf_shared = { version = "0.14", features = ["ptrhash"] } proc-macro2 = "1" quote = "1" diff --git a/string-cache-codegen/lib.rs b/string-cache-codegen/lib.rs index 6f01343..d78e790 100644 --- a/string-cache-codegen/lib.rs +++ b/string-cache-codegen/lib.rs @@ -220,9 +220,13 @@ impl AtomType { } // Static strings - let hash_state = phf_generator::generate_hash(&static_strs); - let phf_generator::HashState { key, disps, map } = hash_state; - let (disps0, disps1): (Vec<_>, Vec<_>) = disps.into_iter().unzip(); + let hash_state = phf_generator::ptrhash::generate_hash(&static_strs); + let phf_generator::ptrhash::HashState { + seed, + pilots, + remap, + map, + } = hash_state; let atoms: Vec<&str> = map.iter().map(|&idx| static_strs[idx]).collect(); let indices = 0..atoms.len() as u32; @@ -252,12 +256,7 @@ impl AtomType { let hashes: Vec = atoms .iter() - .map(|string| { - let hash = phf_shared::hash(string, &key); - // Reconstitute 64-bit `Hash128::h1` - // https://docs.rs/phf_shared/0.14.0/src/phf_shared/lib.rs.html#45-54 - (hash.g as u64) << 32 | (hash.f1 as u64) - }) + .map(|string| phf_shared::ptrhash::hash(string, &seed)) .collect(); let mut path_parts = self.path.rsplitn(2, "::"); @@ -335,8 +334,9 @@ impl AtomType { impl ::string_cache::StaticAtomSet for #static_set_name { fn get() -> &'static ::string_cache::PhfStrSet { static SET: ::string_cache::PhfStrSet = ::string_cache::PhfStrSet { - key: #key, - disps: &[#((#disps0, #disps1)),*], + seed: #seed, + pilots: &[#(#pilots),*], + remap: &[#(#remap),*], atoms: &[#(#atoms),*], hashes: &[#(#hashes),*] };