Skip to content

Commit c923fc2

Browse files
committed
Small improvements
1 parent 699e22b commit c923fc2

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
@@ -24,7 +24,6 @@ use rustc_macros::{Decodable, Encodable, StableHash};
2424
use rustc_span::{ErrorGuaranteed, ExpnId, Span};
2525

2626
use crate::query::Providers;
27-
use crate::ty::print::with_no_trimmed_paths;
2827
use crate::ty::{ResolverAstLowering, TyCtxt};
2928

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

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

228213
if !krate.delayed_ids.is_empty() {
229-
self.sandbox(|| {
214+
self.with_sandbox(|| {
230215
self.ensure_done().hir_crate_items(());
231216
self.ensure_done().crate_inherent_impls(());
232217

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

@@ -67,6 +67,7 @@ use crate::thir::Thir;
6767
use crate::traits;
6868
use crate::traits::solve::{ExternalConstraints, ExternalConstraintsData, PredefinedOpaques};
6969
use crate::ty::predicate::ExistentialPredicateStableCmpExt as _;
70+
use crate::ty::print::with_no_trimmed_paths;
7071
use crate::ty::{
7172
self, AdtDef, AdtDefData, AdtKind, Binder, Clause, Clauses, Const, FnSigKind, GenericArg,
7273
GenericArgs, GenericArgsRef, GenericParamDefKind, List, ListWithCachedTypeInfo, ParamConst,
@@ -836,17 +837,22 @@ impl CurrentGcx {
836837
}
837838

838839
impl<'tcx> TyCtxt<'tcx> {
839-
#[inline]
840840
pub fn is_in_sandbox(self) -> bool {
841-
self.is_in_sandbox.load(std::sync::atomic::Ordering::Relaxed)
841+
self.is_in_sandbox.load(AtomicOrdering::Relaxed)
842842
}
843843

844844
pub fn with_sandbox(self, op: impl FnOnce()) {
845-
self.is_in_sandbox.store(true, std::sync::atomic::Ordering::Relaxed);
845+
self.is_in_sandbox.store(true, AtomicOrdering::Relaxed);
846+
self.enter_query_sandbox();
846847

847-
op();
848+
self.dep_graph.with_sandbox(|| {
849+
with_no_trimmed_paths!({
850+
op();
851+
});
852+
});
848853

849-
self.is_in_sandbox.store(false, std::sync::atomic::Ordering::Relaxed);
854+
self.leave_query_sandbox();
855+
self.is_in_sandbox.store(false, AtomicOrdering::Relaxed);
850856

851857
self.clauses_cache.borrow_mut().clear();
852858
self.highest_var_in_clauses_cache.borrow_mut().clear();

0 commit comments

Comments
 (0)