1
Fork 0

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`
This commit is contained in:
bors 2023-09-02 03:06:11 +00:00
commit 9dc11a13fa
6 changed files with 21 additions and 21 deletions

View file

@ -273,7 +273,7 @@ cfg_if! {
pub use std::cell::RefMut as MappedWriteGuard; pub use std::cell::RefMut as MappedWriteGuard;
pub use std::cell::RefMut as MappedLockGuard; pub use std::cell::RefMut as MappedLockGuard;
pub use std::cell::OnceCell; pub use std::cell::OnceCell as OnceLock;
use std::cell::RefCell as InnerRwLock; use std::cell::RefCell as InnerRwLock;
@ -327,7 +327,7 @@ cfg_if! {
pub use parking_lot::MappedMutexGuard as MappedLockGuard; pub use parking_lot::MappedMutexGuard as MappedLockGuard;
pub use std::sync::OnceLock as OnceCell; pub use std::sync::OnceLock;
pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64}; pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64};

View file

@ -8,7 +8,7 @@ use rustc_borrowck as mir_borrowck;
use rustc_codegen_ssa::traits::CodegenBackend; use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::parallel; use rustc_data_structures::parallel;
use rustc_data_structures::steal::Steal; use rustc_data_structures::steal::Steal;
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal}; use rustc_data_structures::sync::{Lrc, OnceLock, WorkerLocal};
use rustc_errors::PResult; use rustc_errors::PResult;
use rustc_expand::base::{ExtCtxt, LintStoreExpand}; use rustc_expand::base::{ExtCtxt, LintStoreExpand};
use rustc_feature::Features; use rustc_feature::Features;
@ -689,7 +689,7 @@ pub fn create_global_ctxt<'tcx>(
lint_store: Lrc<LintStore>, lint_store: Lrc<LintStore>,
dep_graph: DepGraph, dep_graph: DepGraph,
untracked: Untracked, untracked: Untracked,
gcx_cell: &'tcx OnceCell<GlobalCtxt<'tcx>>, gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,
arena: &'tcx WorkerLocal<Arena<'tcx>>, arena: &'tcx WorkerLocal<Arena<'tcx>>,
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>, hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
) -> &'tcx GlobalCtxt<'tcx> { ) -> &'tcx GlobalCtxt<'tcx> {

View file

@ -7,7 +7,7 @@ use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_codegen_ssa::CodegenResults; use rustc_codegen_ssa::CodegenResults;
use rustc_data_structures::steal::Steal; use rustc_data_structures::steal::Steal;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{AppendOnlyIndexVec, Lrc, OnceCell, RwLock, WorkerLocal}; use rustc_data_structures::sync::{AppendOnlyIndexVec, Lrc, OnceLock, RwLock, WorkerLocal};
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::definitions::Definitions; use rustc_hir::definitions::Definitions;
use rustc_incremental::DepGraphFuture; use rustc_incremental::DepGraphFuture;
@ -78,7 +78,7 @@ impl<T> Default for Query<T> {
pub struct Queries<'tcx> { pub struct Queries<'tcx> {
compiler: &'tcx Compiler, compiler: &'tcx Compiler,
gcx_cell: OnceCell<GlobalCtxt<'tcx>>, gcx_cell: OnceLock<GlobalCtxt<'tcx>>,
arena: WorkerLocal<Arena<'tcx>>, arena: WorkerLocal<Arena<'tcx>>,
hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>, hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>,
@ -93,7 +93,7 @@ impl<'tcx> Queries<'tcx> {
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> { pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
Queries { Queries {
compiler, compiler,
gcx_cell: OnceCell::new(), gcx_cell: OnceLock::new(),
arena: WorkerLocal::new(|_| Arena::default()), arena: WorkerLocal::new(|_| Arena::default()),
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()), hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
parse: Default::default(), parse: Default::default(),

View file

@ -9,7 +9,7 @@ use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::owned_slice::OwnedSlice; use rustc_data_structures::owned_slice::OwnedSlice;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::{AppendOnlyVec, AtomicBool, Lock, Lrc, OnceCell}; use rustc_data_structures::sync::{AppendOnlyVec, AtomicBool, Lock, Lrc, OnceLock};
use rustc_data_structures::unhash::UnhashMap; use rustc_data_structures::unhash::UnhashMap;
use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind};
use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro}; use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro};
@ -93,7 +93,7 @@ pub(crate) struct CrateMetadata {
/// For every definition in this crate, maps its `DefPathHash` to its `DefIndex`. /// For every definition in this crate, maps its `DefPathHash` to its `DefIndex`.
def_path_hash_map: DefPathHashMapRef<'static>, def_path_hash_map: DefPathHashMapRef<'static>,
/// Likewise for ExpnHash. /// Likewise for ExpnHash.
expn_hash_map: OnceCell<UnhashMap<ExpnHash, ExpnIndex>>, expn_hash_map: OnceLock<UnhashMap<ExpnHash, ExpnIndex>>,
/// Used for decoding interpret::AllocIds in a cached & thread-safe manner. /// Used for decoding interpret::AllocIds in a cached & thread-safe manner.
alloc_decoding_state: AllocDecodingState, alloc_decoding_state: AllocDecodingState,
/// Caches decoded `DefKey`s. /// Caches decoded `DefKey`s.

View file

@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::graph; use rustc_data_structures::graph;
use rustc_data_structures::graph::dominators::{dominators, Dominators}; use rustc_data_structures::graph::dominators::{dominators, Dominators};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::OnceCell; use rustc_data_structures::sync::OnceLock;
use rustc_index::{IndexSlice, IndexVec}; use rustc_index::{IndexSlice, IndexVec};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use smallvec::SmallVec; use smallvec::SmallVec;
@ -23,11 +23,11 @@ pub type SwitchSources = FxHashMap<(BasicBlock, BasicBlock), SmallVec<[Option<u1
#[derive(Clone, Default, Debug)] #[derive(Clone, Default, Debug)]
struct Cache { struct Cache {
predecessors: OnceCell<Predecessors>, predecessors: OnceLock<Predecessors>,
switch_sources: OnceCell<SwitchSources>, switch_sources: OnceLock<SwitchSources>,
is_cyclic: OnceCell<bool>, is_cyclic: OnceLock<bool>,
reverse_postorder: OnceCell<Vec<BasicBlock>>, reverse_postorder: OnceLock<Vec<BasicBlock>>,
dominators: OnceCell<Dominators<BasicBlock>>, dominators: OnceLock<Dominators<BasicBlock>>,
} }
impl<'tcx> BasicBlocks<'tcx> { impl<'tcx> BasicBlocks<'tcx> {

View file

@ -2,7 +2,7 @@ use crate::dep_graph::DepNodeIndex;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sharded::{self, Sharded}; 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 rustc_index::{Idx, IndexVec};
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::Hash; use std::hash::Hash;
@ -87,12 +87,12 @@ impl<'tcx, V: 'tcx> CacheSelector<'tcx, V> for SingleCacheSelector {
} }
pub struct SingleCache<V> { pub struct SingleCache<V> {
cache: Lock<Option<(V, DepNodeIndex)>>, cache: OnceLock<(V, DepNodeIndex)>,
} }
impl<V> Default for SingleCache<V> { impl<V> Default for SingleCache<V> {
fn default() -> Self { fn default() -> Self {
SingleCache { cache: Lock::new(None) } SingleCache { cache: OnceLock::new() }
} }
} }
@ -105,16 +105,16 @@ where
#[inline(always)] #[inline(always)]
fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> { fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> {
*self.cache.lock() self.cache.get().copied()
} }
#[inline] #[inline]
fn complete(&self, _key: (), value: V, index: DepNodeIndex) { 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)) { 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) f(&(), &value.0, value.1)
} }
} }