pub mod prelude { pub use crate::default; } pub mod futures; pub mod label; mod short_names; pub use short_names::get_short_name; pub mod synccell; mod default; mod float_ord; pub use ahash::AHasher; pub use default::default; pub use float_ord::*; pub use hashbrown; pub use instant::{Duration, Instant}; pub use tracing; pub use uuid::Uuid; use ahash::RandomState; use hashbrown::hash_map::RawEntryMut; use std::{ fmt::Debug, future::Future, hash::{BuildHasher, Hash, Hasher}, marker::PhantomData, ops::Deref, pin::Pin, }; #[cfg(not(target_arch = "wasm32"))] pub type BoxedFuture<'a, T> = Pin + Send + 'a>>; #[cfg(target_arch = "wasm32")] pub type BoxedFuture<'a, T> = Pin + 'a>>; pub type Entry<'a, K, V> = hashbrown::hash_map::Entry<'a, K, V, RandomState>; /// A hasher builder that will create a fixed hasher. #[derive(Debug, Clone, Default)] pub struct FixedState; impl std::hash::BuildHasher for FixedState { type Hasher = AHasher; #[inline] fn build_hasher(&self) -> AHasher { AHasher::new_with_keys( 0b1001010111101110000001001100010000000011001001101011001001111000, 0b1100111101101011011110001011010100000100001111100011010011010101, ) } } /// A [`HashMap`][hashbrown::HashMap] implementing aHash, a high /// speed keyed hashing algorithm intended for use in in-memory hashmaps. /// /// aHash is designed for performance and is NOT cryptographically secure. pub type HashMap = hashbrown::HashMap; /// A stable hash map implementing aHash, a high speed keyed hashing algorithm /// intended for use in in-memory hashmaps. /// /// Unlike [`HashMap`] this has an iteration order that only depends on the order /// of insertions and deletions and not a random source. /// /// aHash is designed for performance and is NOT cryptographically secure. pub type StableHashMap = hashbrown::HashMap; /// A [`HashSet`][hashbrown::HashSet] implementing aHash, a high /// speed keyed hashing algorithm intended for use in in-memory hashmaps. /// /// aHash is designed for performance and is NOT cryptographically secure. pub type HashSet = hashbrown::HashSet; /// A stable hash set implementing aHash, a high speed keyed hashing algorithm /// intended for use in in-memory hashmaps. /// /// Unlike [`HashSet`] this has an iteration order that only depends on the order /// of insertions and deletions and not a random source. /// /// aHash is designed for performance and is NOT cryptographically secure. pub type StableHashSet = hashbrown::HashSet; /// A pre-hashed value of a specific type. Pre-hashing enables memoization of hashes that are expensive to compute. /// It also enables faster [`PartialEq`] comparisons by short circuiting on hash equality. /// See [`PassHash`] and [`PassHasher`] for a "pass through" [`BuildHasher`] and [`Hasher`] implementation /// designed to work with [`Hashed`] /// See [`PreHashMap`] for a hashmap pre-configured to use [`Hashed`] keys. pub struct Hashed { hash: u64, value: V, marker: PhantomData, } impl Hashed { /// Pre-hashes the given value using the [`BuildHasher`] configured in the [`Hashed`] type. pub fn new(value: V) -> Self { let builder = H::default(); let mut hasher = builder.build_hasher(); value.hash(&mut hasher); Self { hash: hasher.finish(), value, marker: PhantomData, } } /// The pre-computed hash. #[inline] pub fn hash(&self) -> u64 { self.hash } } impl Hash for Hashed { #[inline] fn hash(&self, state: &mut R) { state.write_u64(self.hash); } } impl Deref for Hashed { type Target = V; #[inline] fn deref(&self) -> &Self::Target { &self.value } } impl PartialEq for Hashed { /// A fast impl of [`PartialEq`] that first checks that `other`'s pre-computed hash /// matches this value's pre-computed hash. #[inline] fn eq(&self, other: &Self) -> bool { self.hash == other.hash && self.value.eq(&other.value) } } impl Debug for Hashed { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Hashed") .field("hash", &self.hash) .field("value", &self.value) .finish() } } impl Clone for Hashed { #[inline] fn clone(&self) -> Self { Self { hash: self.hash, value: self.value.clone(), marker: PhantomData, } } } impl Eq for Hashed {} /// A [`BuildHasher`] that results in a [`PassHasher`]. #[derive(Default)] pub struct PassHash; impl BuildHasher for PassHash { type Hasher = PassHasher; fn build_hasher(&self) -> Self::Hasher { PassHasher::default() } } #[derive(Debug, Default)] pub struct PassHasher { hash: u64, } impl Hasher for PassHasher { fn write(&mut self, _bytes: &[u8]) { panic!("can only hash u64 using PassHasher"); } #[inline] fn write_u64(&mut self, i: u64) { self.hash = i; } #[inline] fn finish(&self) -> u64 { self.hash } } /// A [`HashMap`] pre-configured to use [`Hashed`] keys and [`PassHash`] passthrough hashing. pub type PreHashMap = hashbrown::HashMap, V, PassHash>; /// Extension methods intended to add functionality to [`PreHashMap`]. pub trait PreHashMapExt { /// Tries to get or insert the value for the given `key` using the pre-computed hash first. /// If the [`PreHashMap`] does not already contain the `key`, it will clone it and insert /// the value returned by `func`. fn get_or_insert_with V>(&mut self, key: &Hashed, func: F) -> &mut V; } impl PreHashMapExt for PreHashMap { #[inline] fn get_or_insert_with V>(&mut self, key: &Hashed, func: F) -> &mut V { let entry = self .raw_entry_mut() .from_key_hashed_nocheck(key.hash(), key); match entry { RawEntryMut::Occupied(entry) => entry.into_mut(), RawEntryMut::Vacant(entry) => { let (_, value) = entry.insert_hashed_nocheck(key.hash(), key.clone(), func()); value } } } }