Skip to content

Commit 55865e2

Browse files
committed
Small improvements
1 parent a36a0dc commit 55865e2

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
@@ -56,7 +56,7 @@ pub enum QuerySideEffect {
5656
#[derive(Clone)]
5757
pub struct DepGraph {
5858
is_in_sandbox: Arc<AtomicBool>,
59-
data: Option<Arc<DepGraphData>>,
59+
data: [Option<Arc<DepGraphData>>; 2],
6060

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

173173
DepGraph {
174-
data: 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-
})),
174+
data: [
175+
Some(Arc::new(DepGraphData {
176+
previous_work_products: prev_work_products,
177+
current,
178+
previous: prev_graph,
179+
colors,
180+
debug_loaded_from_disk: Default::default(),
181+
})),
182+
None,
183+
],
181184
virtual_dep_node_index: Arc::new(AtomicU32::new(0)),
182185
is_in_sandbox: Default::default(),
183186
}
184187
}
185188

186189
pub fn new_disabled() -> DepGraph {
187190
DepGraph {
188-
data: None,
191+
data: [None, None],
189192
virtual_dep_node_index: Arc::new(AtomicU32::new(0)),
190193
is_in_sandbox: Default::default(),
191194
}
192195
}
193196

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

204201
/// 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, HashStable};
2424
use rustc_span::{ErrorGuaranteed, ExpnId, HashStableContext, 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;
@@ -101,12 +100,12 @@ where
101100
/// In-memory cache for queries whose key type only has one value (e.g. `()`).
102101
/// The cache therefore only needs to store one query return value.
103102
pub struct SingleCache<V> {
104-
cache: Mutex<Option<(V, DepNodeIndex)>>,
103+
cache: Lock<Option<(V, DepNodeIndex)>>,
105104
}
106105

107106
impl<V> Default for SingleCache<V> {
108107
fn default() -> Self {
109-
SingleCache { cache: Mutex::new(None) }
108+
SingleCache { cache: Default::default() }
110109
}
111110
}
112111

@@ -119,27 +118,27 @@ where
119118

120119
#[inline(always)]
121120
fn lookup(&self, _key: &()) -> Option<(V, DepNodeIndex)> {
122-
self.cache.get_cloned().unwrap()
121+
self.cache.lock().clone()
123122
}
124123

125124
#[inline]
126125
fn complete(&self, _key: (), value: V, index: DepNodeIndex) {
127-
self.cache.set(Some((value, index))).ok();
126+
*self.cache.lock() = Some((value, index))
128127
}
129128

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

136135
fn len(&self) -> usize {
137-
self.cache.get_cloned().unwrap().is_some().into()
136+
self.cache.lock().is_some().into()
138137
}
139138

140139
fn invalidate(&self, selector: impl Fn(Self::Key) -> bool) {
141140
if selector(()) {
142-
self.cache.set(None).ok();
141+
*self.cache.lock() = None;
143142
}
144143
}
145144
}

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, GenericArg, GenericArgs,
7273
GenericArgsRef, GenericParamDefKind, List, ListWithCachedTypeInfo, ParamConst, Pattern,
@@ -898,17 +899,22 @@ impl CurrentGcx {
898899
}
899900

900901
impl<'tcx> TyCtxt<'tcx> {
901-
#[inline]
902902
pub fn is_in_sandbox(self) -> bool {
903-
self.is_in_sandbox.load(std::sync::atomic::Ordering::Relaxed)
903+
self.is_in_sandbox.load(AtomicOrdering::Relaxed)
904904
}
905905

906906
pub fn with_sandbox(self, op: impl FnOnce()) {
907-
self.is_in_sandbox.store(true, std::sync::atomic::Ordering::Relaxed);
907+
self.is_in_sandbox.store(true, AtomicOrdering::Relaxed);
908+
self.enter_query_sandbox();
908909

909-
op();
910+
self.dep_graph.with_sandbox(|| {
911+
with_no_trimmed_paths!({
912+
op();
913+
});
914+
});
910915

911-
self.is_in_sandbox.store(false, std::sync::atomic::Ordering::Relaxed);
916+
self.leave_query_sandbox();
917+
self.is_in_sandbox.store(false, AtomicOrdering::Relaxed);
912918

913919
self.clauses_cache.borrow_mut().clear();
914920
self.highest_var_in_clauses_cache.borrow_mut().clear();

0 commit comments

Comments
 (0)