about summary refs log tree commit diff
path: root/compiler/rustc_query_system/src/query/caches.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-09-02 03:06:11 +0000
committerbors <bors@rust-lang.org>2023-09-02 03:06:11 +0000
commit9dc11a13fa848c1b09b7248c540528190dcb79c5 (patch)
treee98a3b294cebc0ae4d88cb08bb035bb8e7303af7 /compiler/rustc_query_system/src/query/caches.rs
parentf9ba43ce147707fb90d038983789d8e57451eb6d (diff)
parent90f5f94699c431d0def5776388ff42da908e54f8 (diff)
downloadrust-9dc11a13fa848c1b09b7248c540528190dcb79c5.tar.gz
rust-9dc11a13fa848c1b09b7248c540528190dcb79c5.zip
Auto merge of #115422 - Zoxc:cache-once-lock, r=cjgillot
Use `OnceLock` for `SingleCache`

This uses `OnceLock` for `SingleCache` instead of `Lock<Option<T>>` so lookups are lock-free.

r? `@cjgillot`
Diffstat (limited to 'compiler/rustc_query_system/src/query/caches.rs')
-rw-r--r--compiler/rustc_query_system/src/query/caches.rs12
1 files changed, 6 insertions, 6 deletions
diff --git a/compiler/rustc_query_system/src/query/caches.rs b/compiler/rustc_query_system/src/query/caches.rs
index a96230fdc94..d8aa377af42 100644
--- a/compiler/rustc_query_system/src/query/caches.rs
+++ b/compiler/rustc_query_system/src/query/caches.rs
@@ -2,7 +2,7 @@ use crate::dep_graph::DepNodeIndex;
 
 use rustc_data_structures::fx::FxHashMap;
 use rustc_data_structures::sharded::{self, Sharded};
-use rustc_data_structures::sync::Lock;
+use rustc_data_structures::sync::OnceLock;
 use rustc_index::{Idx, IndexVec};
 use std::fmt::Debug;
 use std::hash::Hash;
@@ -87,12 +87,12 @@ impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for SingleCacheSelector {
 }
 
 pub struct SingleCache<V> {
-    cache: Lock<Option<(V, DepNodeIndex)>>,
+    cache: OnceLock<(V, DepNodeIndex)>,
 }
 
 impl<V> Default for SingleCache<V> {
     fn default() -> Self {
-        SingleCache { cache: Lock::new(None) }
+        SingleCache { cache: OnceLock::new() }
     }
 }
 
@@ -105,16 +105,16 @@ where
 
     #[inline(always)]
     fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> {
-        *self.cache.lock()
+        self.cache.get().copied()
     }
 
     #[inline]
     fn complete(&self, _key: (), value: V, index: DepNodeIndex) {
-        *self.cache.lock() = Some((value, index));
+        self.cache.set((value, index)).ok();
     }
 
     fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
-        if let Some(value) = self.cache.lock().as_ref() {
+        if let Some(value) = self.cache.get() {
             f(&(), &value.0, value.1)
         }
     }