Skip to content

Commit cbac5f7

Browse files
committed
Replace atomic operations with OnceLock in backend/util
1 parent c35cc93 commit cbac5f7

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

crates/backend/src/util.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use std::env;
66
use std::fmt;
77
use std::hash::{Hash, Hasher};
88
use std::iter::FromIterator;
9-
use std::sync::atomic::AtomicBool;
10-
use std::sync::atomic::AtomicUsize;
11-
use std::sync::atomic::Ordering::SeqCst;
9+
use std::sync::atomic::{AtomicUsize, Ordering};
10+
use std::sync::Once;
1211

1312
use crate::ast;
1413
use proc_macro2::{self, Ident};
@@ -133,13 +132,13 @@ pub fn wrap_import_function(function: ast::ImportFunction) -> ast::Import {
133132
pub struct ShortHash<T>(pub T);
134133

135134
impl<T: Hash> fmt::Display for ShortHash<T> {
136-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
137-
static HASHED: AtomicBool = AtomicBool::new(false);
138-
static HASH: AtomicUsize = AtomicUsize::new(0);
135+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136+
static INIT: Once = Once::new();
137+
static HASH_SEED: AtomicUsize = AtomicUsize::new(0);
139138

140139
// Try to amortize the cost of loading env vars a lot as we're gonna be
141140
// hashing for a lot of symbols.
142-
if !HASHED.load(SeqCst) {
141+
INIT.call_once(|| {
143142
let mut h = DefaultHasher::new();
144143
env::var("CARGO_PKG_NAME")
145144
.expect("should have CARGO_PKG_NAME env var")
@@ -149,13 +148,26 @@ impl<T: Hash> fmt::Display for ShortHash<T> {
149148
.hash(&mut h);
150149
// This may chop off 32 bits on 32-bit platforms, but that's ok, we
151150
// just want something to mix in below anyway.
152-
HASH.store(h.finish() as usize, SeqCst);
153-
HASHED.store(true, SeqCst);
154-
}
151+
HASH_SEED.store(h.finish() as usize, Ordering::Release);
152+
});
153+
154+
let seed = HASH_SEED.load(Ordering::Acquire);
155155

156156
let mut h = DefaultHasher::new();
157-
HASH.load(SeqCst).hash(&mut h);
157+
seed.hash(&mut h);
158158
self.0.hash(&mut h);
159159
write!(f, "{:016x}", h.finish())
160160
}
161161
}
162+
163+
#[cfg(test)]
164+
mod tests {
165+
use super::*;
166+
167+
#[test]
168+
fn test_short_hash_eq() {
169+
let hash = ShortHash("Hello World");
170+
let hash_str = format!("{}", hash);
171+
assert_eq!(&hash_str, "0711bda1e8c6f44a");
172+
}
173+
}

0 commit comments

Comments
 (0)