Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
15 changes: 9 additions & 6 deletions src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,16 @@ impl<Static: StaticAtomSet> Atom<Static> {
Self::try_static_internal(string_to_add).ok()
}

fn try_static_internal(string_to_add: &str) -> Result<Self, phf_shared::Hashes> {
fn try_static_internal(string_to_add: &str) -> Result<Self, u64> {
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))
Expand Down Expand Up @@ -272,9 +278,6 @@ impl<'a, Static: StaticAtomSet> From<Cow<'a, str>> for Atom<Static> {
}
} 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<Entry> = dynamic_set().insert(string_to_add, hash);
let data = ptr.as_ptr().expose_provenance() as u64;
debug_assert!(0 == data & TAG_MASK);
Expand Down
11 changes: 7 additions & 4 deletions src/static_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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],
Expand Down
4 changes: 2 additions & 2 deletions string-cache-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
22 changes: 11 additions & 11 deletions string-cache-codegen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -252,12 +256,7 @@ impl AtomType {

let hashes: Vec<u64> = 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, "::");
Expand Down Expand Up @@ -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),*]
};
Expand Down
Loading