summaryrefslogtreecommitdiff
path: root/crates/typst-utils/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-06-09 15:23:56 +0200
committerGitHub <noreply@github.com>2024-06-09 13:23:56 +0000
commitf91cad7d7829556e24d219e55db7da56a966523f (patch)
treeadce387eb0008198852dd243693512237d3b5f2a /crates/typst-utils/src
parentcc3e9c86022c5ae5a82938b0b65e3d2dca93b1ed (diff)
Pure location assignment (#4352)
Diffstat (limited to 'crates/typst-utils/src')
-rw-r--r--crates/typst-utils/src/hash.rs19
1 files changed, 10 insertions, 9 deletions
diff --git a/crates/typst-utils/src/hash.rs b/crates/typst-utils/src/hash.rs
index 82bb76af..dabae24c 100644
--- a/crates/typst-utils/src/hash.rs
+++ b/crates/typst-utils/src/hash.rs
@@ -2,8 +2,9 @@ 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, Ordering};
+use portable_atomic::AtomicU128;
use siphasher::sip128::{Hasher128, SipHasher13};
/// A wrapper type with lazily-computed hash.
@@ -70,7 +71,9 @@ impl<T: ?Sized> LazyHash<T> {
/// Get the hash, returns zero if not computed yet.
#[inline]
fn load_hash(&self) -> u128 {
- self.hash.load(Ordering::SeqCst)
+ // We only need atomicity and no synchronization of other operations, so
+ // `Relaxed` is fine.
+ self.hash.load(Ordering::Relaxed)
}
}
@@ -78,20 +81,18 @@ 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 hash = self.load_hash();
+ let mut hash = self.load_hash();
if hash == 0 {
- let hashed = hash_item(&self.value);
- self.hash.store(hashed, Ordering::SeqCst);
- hashed
- } else {
- hash
+ 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
+ // Because we have a mutable reference, we can skip the atomic.
*self.hash.get_mut() = 0;
}
}