typst_utils/hash.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
use std::any::Any;
use std::fmt::{self, Debug};
use std::hash::{Hash, Hasher};
use std::ops::{Deref, DerefMut};
use std::sync::atomic::Ordering;
use portable_atomic::AtomicU128;
use siphasher::sip128::{Hasher128, SipHasher13};
/// A wrapper type with lazily-computed hash.
///
/// This is useful if you want to pass large values of `T` to memoized
/// functions. Especially recursive structures like trees benefit from
/// intermediate prehashed nodes.
///
/// Note that for a value `v` of type `T`, `hash(v)` is not necessarily equal to
/// `hash(LazyHash::new(v))`. Writing the precomputed hash into a hasher's
/// state produces different output than writing the value's parts directly.
/// However, that seldom matters as you are typically either dealing with values
/// of type `T` or with values of type `LazyHash<T>`, not a mix of both.
///
/// # Equality
/// Because Typst uses high-quality 128 bit hashes in all places, the risk of a
/// hash collision is reduced to an absolute minimum. Therefore, this type
/// additionally provides `PartialEq` and `Eq` implementations that compare by
/// hash instead of by value. For this to be correct, your hash implementation
/// **must feed all information relevant to the `PartialEq` impl to the
/// hasher.**
///
/// # Usage
/// If the value is expected to be cloned, it is best used inside of an `Arc`
/// or `Rc` to best re-use the hash once it has been computed.
pub struct LazyHash<T: ?Sized> {
/// The hash for the value.
hash: AtomicU128,
/// The underlying value.
value: T,
}
impl<T: Default> Default for LazyHash<T> {
#[inline]
fn default() -> Self {
Self::new(Default::default())
}
}
impl<T> LazyHash<T> {
/// Wraps an item without pre-computed hash.
#[inline]
pub fn new(value: T) -> Self {
Self { hash: AtomicU128::new(0), value }
}
/// Wrap an item with a pre-computed hash.
///
/// **Important:** The hash must be correct for the value. This cannot be
/// enforced at compile time, so use with caution.
#[inline]
pub fn reuse<U: ?Sized>(value: T, existing: &LazyHash<U>) -> Self {
LazyHash { hash: AtomicU128::new(existing.load_hash()), value }
}
/// Returns the wrapped value.
#[inline]
pub fn into_inner(self) -> T {
self.value
}
}
impl<T: ?Sized> LazyHash<T> {
/// Get the hash, returns zero if not computed yet.
#[inline]
fn load_hash(&self) -> u128 {
// We only need atomicity and no synchronization of other operations, so
// `Relaxed` is fine.
self.hash.load(Ordering::Relaxed)
}
}
impl<T: Hash + ?Sized + 'static> LazyHash<T> {
/// Get the hash or compute it if not set yet.
#[inline]
fn load_or_compute_hash(&self) -> u128 {
let mut hash = self.load_hash();
if hash == 0 {
hash = hash_item(&self.value);
self.hash.store(hash, Ordering::Relaxed);
}
hash
}
/// Reset the hash to zero.
#[inline]
fn reset_hash(&mut self) {
// Because we have a mutable reference, we can skip the atomic.
*self.hash.get_mut() = 0;
}
}
/// Hash the item.
#[inline]
fn hash_item<T: Hash + ?Sized + 'static>(item: &T) -> u128 {
// Also hash the TypeId because the type might be converted
// through an unsized coercion.
let mut state = SipHasher13::new();
item.type_id().hash(&mut state);
item.hash(&mut state);
state.finish128().as_u128()
}
impl<T: Hash + ?Sized + 'static> Hash for LazyHash<T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u128(self.load_or_compute_hash());
}
}
impl<T> From<T> for LazyHash<T> {
#[inline]
fn from(value: T) -> Self {
Self::new(value)
}
}
impl<T: Hash + ?Sized + 'static> Eq for LazyHash<T> {}
impl<T: Hash + ?Sized + 'static> PartialEq for LazyHash<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.load_or_compute_hash() == other.load_or_compute_hash()
}
}
impl<T: ?Sized> Deref for LazyHash<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T: Hash + ?Sized + 'static> DerefMut for LazyHash<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.reset_hash();
&mut self.value
}
}
impl<T: Hash + Clone + 'static> Clone for LazyHash<T> {
fn clone(&self) -> Self {
Self {
hash: AtomicU128::new(self.load_hash()),
value: self.value.clone(),
}
}
}
impl<T: Debug> Debug for LazyHash<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
}
}
/// A wrapper type with a manually computed hash.
///
/// This can be used to turn an unhashable type into a hashable one where the
/// hash is provided manually. Typically, the hash is derived from the data
/// which was used to construct to the unhashable type.
///
/// For instance, you could hash the bytes that were parsed into an unhashable
/// data structure.
///
/// # Equality
/// Because Typst uses high-quality 128 bit hashes in all places, the risk of a
/// hash collision is reduced to an absolute minimum. Therefore, this type
/// additionally provides `PartialEq` and `Eq` implementations that compare by
/// hash instead of by value. For this to be correct, your hash implementation
/// **must feed all information relevant to the `PartialEq` impl to the
/// hasher.**
#[derive(Clone)]
pub struct ManuallyHash<T: ?Sized> {
/// A manually computed hash.
hash: u128,
/// The underlying value.
value: T,
}
impl<T> ManuallyHash<T> {
/// Wraps an item with a pre-computed hash.
///
/// The hash should be computed with `typst_utils::hash128`.
#[inline]
pub fn new(value: T, hash: u128) -> Self {
Self { hash, value }
}
/// Returns the wrapped value.
#[inline]
pub fn into_inner(self) -> T {
self.value
}
}
impl<T: ?Sized> Hash for ManuallyHash<T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u128(self.hash);
}
}
impl<T: ?Sized> Eq for ManuallyHash<T> {}
impl<T: ?Sized> PartialEq for ManuallyHash<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash
}
}
impl<T: ?Sized> Deref for ManuallyHash<T> {
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<T: Debug> Debug for ManuallyHash<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.value.fmt(f)
}
}