|
|
use std::{num::NonZeroU8, ptr::NonNull}; |
|
|
|
|
|
use triomphe::Arc; |
|
|
|
|
|
use crate::{ |
|
|
INLINE_TAG, INLINE_TAG_INIT, LEN_OFFSET, RcStr, STATIC_TAG, TAG_MASK, |
|
|
tagged_value::{MAX_INLINE_LEN, TaggedValue}, |
|
|
}; |
|
|
|
|
|
pub enum Payload { |
|
|
String(String), |
|
|
Ref(&'static str), |
|
|
} |
|
|
|
|
|
impl Payload { |
|
|
pub(crate) fn as_str(&self) -> &str { |
|
|
match self { |
|
|
Payload::String(s) => s, |
|
|
Payload::Ref(s) => s, |
|
|
} |
|
|
} |
|
|
pub(crate) fn into_string(self) -> String { |
|
|
match self { |
|
|
Payload::String(s) => s, |
|
|
Payload::Ref(r) => r.to_string(), |
|
|
} |
|
|
} |
|
|
} |
|
|
impl PartialEq for Payload { |
|
|
fn eq(&self, other: &Self) -> bool { |
|
|
self.as_str() == other.as_str() |
|
|
} |
|
|
} |
|
|
|
|
|
pub struct PrehashedString { |
|
|
pub value: Payload, |
|
|
|
|
|
|
|
|
pub hash: u64, |
|
|
} |
|
|
|
|
|
pub unsafe fn cast(ptr: TaggedValue) -> *const PrehashedString { |
|
|
ptr.get_ptr().cast() |
|
|
} |
|
|
|
|
|
pub(crate) unsafe fn deref_from<'i>(ptr: TaggedValue) -> &'i PrehashedString { |
|
|
unsafe { &*cast(ptr) } |
|
|
} |
|
|
|
|
|
|
|
|
pub unsafe fn restore_arc(v: TaggedValue) -> Arc<PrehashedString> { |
|
|
let ptr = v.get_ptr() as *const PrehashedString; |
|
|
unsafe { Arc::from_raw(ptr) } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub(crate) fn new_atom<T: AsRef<str> + Into<String>>(text: T) -> RcStr { |
|
|
let len = text.as_ref().len(); |
|
|
|
|
|
if len < MAX_INLINE_LEN { |
|
|
|
|
|
let tag = INLINE_TAG_INIT | ((len as u8) << LEN_OFFSET); |
|
|
let mut unsafe_data = TaggedValue::new_tag(tag); |
|
|
unsafe { |
|
|
unsafe_data.data_mut()[..len].copy_from_slice(text.as_ref().as_bytes()); |
|
|
} |
|
|
return RcStr { unsafe_data }; |
|
|
} |
|
|
|
|
|
let hash = hash_bytes(text.as_ref().as_bytes()); |
|
|
|
|
|
let entry: Arc<PrehashedString> = Arc::new(PrehashedString { |
|
|
value: Payload::String(text.into()), |
|
|
hash, |
|
|
}); |
|
|
let entry = Arc::into_raw(entry); |
|
|
|
|
|
let ptr: NonNull<PrehashedString> = unsafe { |
|
|
|
|
|
NonNull::new_unchecked(entry as *mut _) |
|
|
}; |
|
|
debug_assert!(0 == ptr.as_ptr() as u8 & TAG_MASK); |
|
|
RcStr { |
|
|
unsafe_data: TaggedValue::new_ptr(ptr), |
|
|
} |
|
|
} |
|
|
|
|
|
#[inline(always)] |
|
|
pub(crate) fn new_static_atom(string: &'static PrehashedString) -> RcStr { |
|
|
let mut entry = string as *const PrehashedString; |
|
|
debug_assert!(0 == entry as u8 & TAG_MASK); |
|
|
|
|
|
entry = ((entry as usize) | STATIC_TAG as usize) as *mut PrehashedString; |
|
|
let ptr: NonNull<PrehashedString> = unsafe { |
|
|
|
|
|
NonNull::new_unchecked(entry as *mut _) |
|
|
}; |
|
|
|
|
|
RcStr { |
|
|
unsafe_data: TaggedValue::new_ptr(ptr), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[doc(hidden)] |
|
|
pub(crate) const fn inline_atom(text: &str) -> Option<RcStr> { |
|
|
let len = text.len(); |
|
|
if len < MAX_INLINE_LEN { |
|
|
let tag = INLINE_TAG | ((len as u8) << LEN_OFFSET); |
|
|
let mut unsafe_data = TaggedValue::new_tag(NonZeroU8::new(tag).unwrap()); |
|
|
|
|
|
|
|
|
|
|
|
unsafe { |
|
|
unsafe_data |
|
|
.data_mut() |
|
|
.split_at_mut(len) |
|
|
.0 |
|
|
.copy_from_slice(text.as_bytes()); |
|
|
} |
|
|
return Some(RcStr { unsafe_data }); |
|
|
} |
|
|
None |
|
|
} |
|
|
|
|
|
|
|
|
const SEED1: u64 = 0x243f6a8885a308d3; |
|
|
const SEED2: u64 = 0x13198a2e03707344; |
|
|
const PREVENT_TRIVIAL_ZERO_COLLAPSE: u64 = 0xa4093822299f31d0; |
|
|
|
|
|
#[inline] |
|
|
const fn multiply_mix(x: u64, y: u64) -> u64 { |
|
|
#[cfg(target_pointer_width = "64")] |
|
|
{ |
|
|
|
|
|
|
|
|
let full = (x as u128) * (y as u128); |
|
|
let lo = full as u64; |
|
|
let hi = (full >> 64) as u64; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lo ^ hi |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
#[cfg(target_pointer_width = "32")] |
|
|
{ |
|
|
|
|
|
|
|
|
let lx = x as u32; |
|
|
let ly = y as u32; |
|
|
let hx = (x >> 32) as u32; |
|
|
let hy = (y >> 32) as u32; |
|
|
|
|
|
|
|
|
let afull = (lx as u64) * (hy as u64); |
|
|
let bfull = (hx as u64) * (ly as u64); |
|
|
|
|
|
|
|
|
|
|
|
afull ^ bfull.rotate_right(32) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const fn read_u64_le(bytes: &[u8], offset: usize) -> u64 { |
|
|
(bytes[offset] as u64) |
|
|
| ((bytes[offset + 1] as u64) << 8) |
|
|
| ((bytes[offset + 2] as u64) << 16) |
|
|
| ((bytes[offset + 3] as u64) << 24) |
|
|
| ((bytes[offset + 4] as u64) << 32) |
|
|
| ((bytes[offset + 5] as u64) << 40) |
|
|
| ((bytes[offset + 6] as u64) << 48) |
|
|
| ((bytes[offset + 7] as u64) << 56) |
|
|
} |
|
|
|
|
|
|
|
|
const fn read_u32_le(bytes: &[u8], offset: usize) -> u32 { |
|
|
(bytes[offset] as u32) |
|
|
| ((bytes[offset + 1] as u32) << 8) |
|
|
| ((bytes[offset + 2] as u32) << 16) |
|
|
| ((bytes[offset + 3] as u32) << 24) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
#[doc(hidden)] |
|
|
pub const fn hash_bytes(bytes: &[u8]) -> u64 { |
|
|
let len = bytes.len(); |
|
|
let mut s0 = SEED1; |
|
|
let mut s1 = SEED2; |
|
|
|
|
|
if len <= 16 { |
|
|
|
|
|
if len >= 8 { |
|
|
s0 ^= read_u64_le(bytes, 0); |
|
|
s1 ^= read_u64_le(bytes, len - 8); |
|
|
} else if len >= 4 { |
|
|
s0 ^= read_u32_le(bytes, 0) as u64; |
|
|
s1 ^= read_u32_le(bytes, len - 4) as u64; |
|
|
} else if len > 0 { |
|
|
let lo = bytes[0]; |
|
|
let mid = bytes[len / 2]; |
|
|
let hi = bytes[len - 1]; |
|
|
s0 ^= lo as u64; |
|
|
s1 ^= ((hi as u64) << 8) | mid as u64; |
|
|
} |
|
|
} else { |
|
|
|
|
|
let mut off = 0; |
|
|
while off < len - 16 { |
|
|
let x = read_u64_le(bytes, off); |
|
|
let y = read_u64_le(bytes, off + 8); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let t = multiply_mix(s0 ^ x, PREVENT_TRIVIAL_ZERO_COLLAPSE ^ y); |
|
|
s0 = s1; |
|
|
s1 = t; |
|
|
off += 16; |
|
|
} |
|
|
|
|
|
s0 ^= read_u64_le(bytes, len - 16); |
|
|
s1 ^= read_u64_le(bytes, len - 8); |
|
|
} |
|
|
|
|
|
multiply_mix(s0, s1) ^ (len as u64) |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use std::hash::{Hash, Hasher}; |
|
|
|
|
|
use rustc_hash::FxHasher; |
|
|
|
|
|
use crate::RcStr; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
fn test_hash() { |
|
|
let long_string = "A very long long long string that would not be inlined"; |
|
|
|
|
|
{ |
|
|
let u64_value = super::hash_bytes(long_string.as_bytes()); |
|
|
dbg!(u64_value); |
|
|
let mut hasher = FxHasher::default(); |
|
|
hasher.write_u64(u64_value); |
|
|
let expected = hasher.finish(); |
|
|
|
|
|
println!("Expected: {expected:?}"); |
|
|
} |
|
|
|
|
|
let str = RcStr::from(long_string); |
|
|
assert_eq!(fxhash(str.clone()), fxhash(long_string)); |
|
|
assert_eq!(fxhash((1, str, 1)), fxhash((1, long_string, 1))); |
|
|
} |
|
|
|
|
|
fn fxhash<T: Hash>(value: T) -> u64 { |
|
|
let mut hasher = FxHasher::default(); |
|
|
value.hash(&mut hasher); |
|
|
hasher.finish() |
|
|
} |
|
|
} |
|
|
|