Skip to content

Commit e8014fd

Browse files
committed
Small improvements
1 parent 61c0346 commit e8014fd

5 files changed

Lines changed: 35 additions & 51 deletions

File tree

compiler/rustc_middle/src/dep_graph/graph.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub enum QuerySideEffect {
5555
#[derive(Clone)]
5656
pub struct DepGraph {
5757
is_in_sandbox: Arc<AtomicBool>,
58-
data: Option<Arc<DepGraphData>>,
58+
data: [Option<Arc<DepGraphData>>; 2],
5959

6060
/// This field is used for assigning DepNodeIndices when running in
6161
/// non-incremental mode. Even in non-incremental mode we make sure that
@@ -170,34 +170,31 @@ impl DepGraph {
170170
}
171171

172172
DepGraph {
173-
data: Some(Arc::new(DepGraphData {
174-
previous_work_products: prev_work_products,
175-
current,
176-
previous: prev_graph,
177-
colors,
178-
debug_loaded_from_disk: Default::default(),
179-
})),
173+
data: [
174+
Some(Arc::new(DepGraphData {
175+
previous_work_products: prev_work_products,
176+
current,
177+
previous: prev_graph,
178+
colors,
179+
debug_loaded_from_disk: Default::default(),
180+
})),
181+
None,
182+
],
180183
virtual_dep_node_index: Arc::new(AtomicU32::new(0)),
181184
is_in_sandbox: Default::default(),
182185
}
183186
}
184187

185188
pub fn new_disabled() -> DepGraph {
186189
DepGraph {
187-
data: None,
190+
data: [None, None],
188191
virtual_dep_node_index: Arc::new(AtomicU32::new(0)),
189192
is_in_sandbox: Default::default(),
190193
}
191194
}
192195

193-
#[inline]
194196
pub fn data(&self) -> Option<&DepGraphData> {
195-
self.data.as_deref().filter(|_| !self.is_in_sandbox())
196-
}
197-
198-
#[inline]
199-
pub fn is_in_sandbox(&self) -> bool {
200-
self.is_in_sandbox.load(Ordering::Relaxed)
197+
self.data[self.is_in_sandbox.load(Ordering::Relaxed) as usize].as_deref()
201198
}
202199

203200
/// Returns `true` if we are actually building the full dep-graph, and `false` otherwise.

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_macros::{Decodable, Encodable, HashStable};
2323
use rustc_span::{ErrorGuaranteed, ExpnId, HashStableContext, Span};
2424

2525
use crate::query::Providers;
26-
use crate::ty::print::with_no_trimmed_paths;
2726
use crate::ty::{ResolverAstLowering, TyCtxt};
2827

2928
/// The top-level data structure that stores the entire contents of
@@ -207,25 +206,11 @@ impl ModuleItems {
207206
}
208207

209208
impl<'tcx> TyCtxt<'tcx> {
210-
fn sandbox(self, action: impl FnOnce() -> ()) {
211-
self.with_sandbox(|| {
212-
self.enter_query_sandbox();
213-
214-
self.dep_graph.with_sandbox(|| {
215-
with_no_trimmed_paths!({
216-
action();
217-
});
218-
});
219-
220-
self.leave_query_sandbox();
221-
});
222-
}
223-
224209
pub fn force_delayed_owners_lowering(self) {
225210
let krate = self.hir_crate(());
226211

227212
if !krate.delayed_ids.is_empty() {
228-
self.sandbox(|| {
213+
self.with_sandbox(|| {
229214
self.ensure_done().hir_crate_items(());
230215
self.ensure_done().crate_inherent_impls(());
231216

compiler/rustc_middle/src/query/caches.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::sync::Mutex;
2-
31
use rustc_data_structures::sharded::ShardedHashMap;
2+
use rustc_data_structures::sync::Lock;
43
pub use rustc_data_structures::vec_cache::VecCache;
54
use rustc_hir::def_id::LOCAL_CRATE;
65
use rustc_index::Idx;
@@ -99,12 +98,12 @@ where
9998
/// In-memory cache for queries whose key type only has one value (e.g. `()`).
10099
/// The cache therefore only needs to store one query return value.
101100
pub struct SingleCache<V> {
102-
cache: Mutex<Option<(V, DepNodeIndex)>>,
101+
cache: Lock<Option<(V, DepNodeIndex)>>,
103102
}
104103

105104
impl<V> Default for SingleCache<V> {
106105
fn default() -> Self {
107-
SingleCache { cache: Mutex::new(None) }
106+
SingleCache { cache: Default::default() }
108107
}
109108
}
110109

@@ -117,27 +116,27 @@ where
117116

118117
#[inline(always)]
119118
fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> {
120-
self.cache.get_cloned().unwrap()
119+
self.cache.lock().clone()
121120
}
122121

123122
#[inline]
124123
fn complete(&self, _key: (), value: V, index: DepNodeIndex) {
125-
self.cache.set(Some((value, index))).ok();
124+
*self.cache.lock() = Some((value, index))
126125
}
127126

128127
fn for_each(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex)) {
129-
if let Some(value) = self.cache.get_cloned().unwrap() {
128+
if let Some(value) = self.cache.lock().clone() {
130129
f(&(), &value.0, value.1)
131130
}
132131
}
133132

134133
fn len(&self) -> usize {
135-
self.cache.get_cloned().unwrap().is_some().into()
134+
self.cache.lock().is_some().into()
136135
}
137136

138137
fn invalidate(&self, selector: impl Fn(Self::Key) -> bool) {
139138
if selector(()) {
140-
self.cache.set(None).ok();
139+
*self.cache.lock() = None;
141140
}
142141
}
143142
}

compiler/rustc_middle/src/query/inner.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ where
2020
{
2121
match cache.lookup(&key) {
2222
Some((value, index)) => {
23-
if !tcx.is_in_sandbox() {
24-
tcx.prof.query_cache_hit(index.into());
25-
}
26-
23+
tcx.prof.query_cache_hit(index.into());
2724
tcx.dep_graph.read_index(index);
2825

2926
Some(value)

compiler/rustc_middle/src/ty/context.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::ffi::OsStr;
1212
use std::hash::{Hash, Hasher};
1313
use std::marker::{PhantomData, PointeeSized};
1414
use std::ops::{Bound, Deref};
15-
use std::sync::atomic::AtomicBool;
15+
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
1616
use std::sync::{Arc, OnceLock};
1717
use std::{fmt, iter, mem};
1818

@@ -69,6 +69,7 @@ use crate::thir::Thir;
6969
use crate::traits;
7070
use crate::traits::solve::{ExternalConstraints, ExternalConstraintsData, PredefinedOpaques};
7171
use crate::ty::predicate::ExistentialPredicateStableCmpExt as _;
72+
use crate::ty::print::with_no_trimmed_paths;
7273
use crate::ty::{
7374
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Clauses, Const, GenericArg, GenericArgs,
7475
GenericArgsRef, GenericParamDefKind, List, ListWithCachedTypeInfo, ParamConst, Pattern,
@@ -956,17 +957,22 @@ impl CurrentGcx {
956957
}
957958

958959
impl<'tcx> TyCtxt<'tcx> {
959-
#[inline]
960960
pub fn is_in_sandbox(self) -> bool {
961-
self.is_in_sandbox.load(std::sync::atomic::Ordering::Relaxed)
961+
self.is_in_sandbox.load(AtomicOrdering::Relaxed)
962962
}
963963

964964
pub fn with_sandbox(self, op: impl FnOnce()) {
965-
self.is_in_sandbox.store(true, std::sync::atomic::Ordering::Relaxed);
965+
self.is_in_sandbox.store(true, AtomicOrdering::Relaxed);
966+
self.enter_query_sandbox();
966967

967-
op();
968+
self.dep_graph.with_sandbox(|| {
969+
with_no_trimmed_paths!({
970+
op();
971+
});
972+
});
968973

969-
self.is_in_sandbox.store(false, std::sync::atomic::Ordering::Relaxed);
974+
self.leave_query_sandbox();
975+
self.is_in_sandbox.store(false, AtomicOrdering::Relaxed);
970976

971977
self.clauses_cache.borrow_mut().clear();
972978
self.highest_var_in_clauses_cache.borrow_mut().clear();

0 commit comments

Comments
 (0)