Skip to content

Commit ac56bf3

Browse files
committed
Tweak query code for performance
1 parent 4a45578 commit ac56bf3

File tree

12 files changed

+185
-82
lines changed

12 files changed

+185
-82
lines changed

src/librustc/dep_graph/dep_node.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ macro_rules! define_dep_nodes {
162162
}
163163
}
164164

165-
#[inline]
165+
// FIXME: Make `is_anon`, `is_input`, `is_eval_always` and `has_params` properties
166+
// of queries
167+
#[inline(always)]
166168
pub fn is_anon(&self) -> bool {
167169
match *self {
168170
$(
@@ -171,16 +173,20 @@ macro_rules! define_dep_nodes {
171173
}
172174
}
173175

174-
#[inline]
175-
pub fn is_input(&self) -> bool {
176+
#[inline(always)]
177+
pub fn is_input_inlined(&self) -> bool {
176178
match *self {
177179
$(
178180
DepKind :: $variant => { contains_input_attr!($($attr),*) }
179181
)*
180182
}
181183
}
182184

183-
#[inline]
185+
pub fn is_input(&self) -> bool {
186+
self.is_input_inlined()
187+
}
188+
189+
#[inline(always)]
184190
pub fn is_eval_always(&self) -> bool {
185191
match *self {
186192
$(
@@ -190,8 +196,8 @@ macro_rules! define_dep_nodes {
190196
}
191197

192198
#[allow(unreachable_code)]
193-
#[inline]
194-
pub fn has_params(&self) -> bool {
199+
#[inline(always)]
200+
pub fn has_params_inlined(&self) -> bool {
195201
match *self {
196202
$(
197203
DepKind :: $variant => {
@@ -212,6 +218,10 @@ macro_rules! define_dep_nodes {
212218
)*
213219
}
214220
}
221+
222+
pub fn has_params(&self) -> bool {
223+
self.has_params_inlined()
224+
}
215225
}
216226

217227
pub enum DepConstructor<$tcx> {
@@ -230,6 +240,7 @@ macro_rules! define_dep_nodes {
230240

231241
impl DepNode {
232242
#[allow(unreachable_code, non_snake_case)]
243+
#[inline(always)]
233244
pub fn new<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
234245
dep: DepConstructor<'gcx>)
235246
-> DepNode
@@ -299,7 +310,7 @@ macro_rules! define_dep_nodes {
299310
/// Construct a DepNode from the given DepKind and DefPathHash. This
300311
/// method will assert that the given DepKind actually requires a
301312
/// single DefId/DefPathHash parameter.
302-
#[inline]
313+
#[inline(always)]
303314
pub fn from_def_path_hash(kind: DepKind,
304315
def_path_hash: DefPathHash)
305316
-> DepNode {
@@ -313,9 +324,9 @@ macro_rules! define_dep_nodes {
313324
/// Create a new, parameterless DepNode. This method will assert
314325
/// that the DepNode corresponding to the given DepKind actually
315326
/// does not require any parameters.
316-
#[inline]
327+
#[inline(always)]
317328
pub fn new_no_params(kind: DepKind) -> DepNode {
318-
assert!(!kind.has_params());
329+
assert!(!kind.has_params_inlined());
319330
DepNode {
320331
kind,
321332
hash: Fingerprint::ZERO,
@@ -418,14 +429,14 @@ impl fmt::Debug for DepNode {
418429

419430

420431
impl DefPathHash {
421-
#[inline]
432+
#[inline(always)]
422433
pub fn to_dep_node(self, kind: DepKind) -> DepNode {
423434
DepNode::from_def_path_hash(kind, self)
424435
}
425436
}
426437

427438
impl DefId {
428-
#[inline]
439+
#[inline(always)]
429440
pub fn to_dep_node(self, tcx: TyCtxt<'_, '_, '_>, kind: DepKind) -> DepNode {
430441
DepNode::from_def_path_hash(kind, tcx.def_path_hash(self))
431442
}

src/librustc/hir/map/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ impl Forest {
159159
self.dep_graph.read(DepNode::new_no_params(DepKind::Krate));
160160
&self.krate
161161
}
162+
163+
pub fn untracked_krate<'hir>(&'hir self) -> &'hir Crate {
164+
&self.krate
165+
}
162166
}
163167

164168
/// Represents a mapping from Node IDs to AST elements and their parent

src/librustc/ich/hcx.rs

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ impl<'a> StableHashingContext<'a> {
8686
// The `krate` here is only used for mapping BodyIds to Bodies.
8787
// Don't use it for anything else or you'll run the risk of
8888
// leaking data out of the tracking system.
89+
#[inline]
8990
pub fn new(sess: &'a Session,
9091
krate: &'a hir::Crate,
9192
definitions: &'a Definitions,

src/librustc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@
6060
#![feature(slice_sort_by_cached_key)]
6161
#![feature(specialization)]
6262
#![feature(unboxed_closures)]
63+
#![feature(thread_local)]
6364
#![feature(trace_macros)]
6465
#![feature(trusted_len)]
6566
#![feature(vec_remove_item)]
6667
#![feature(step_trait)]
68+
#![feature(stmt_expr_attributes)]
6769
#![feature(integer_atomics)]
6870
#![feature(test)]
6971
#![feature(in_band_lifetimes)]

src/librustc/session/mod.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ pub struct Session {
128128
/// Used by -Z profile-queries in util::common
129129
pub profile_channel: Lock<Option<mpsc::Sender<ProfileQueriesMsg>>>,
130130

131+
/// Used by -Z self-profile
132+
pub self_profiling_active: bool,
133+
131134
/// Used by -Z self-profile
132135
pub self_profiling: Lock<SelfProfiler>,
133136

@@ -825,10 +828,17 @@ impl Session {
825828
}
826829
}
827830

831+
#[inline(never)]
832+
#[cold]
833+
fn profiler_active<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
834+
let mut profiler = self.self_profiling.borrow_mut();
835+
f(&mut profiler);
836+
}
837+
838+
#[inline(always)]
828839
pub fn profiler<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
829-
if self.opts.debugging_opts.self_profile || self.opts.debugging_opts.profile_json {
830-
let mut profiler = self.self_profiling.borrow_mut();
831-
f(&mut profiler);
840+
if unlikely!(self.self_profiling_active) {
841+
self.profiler_active(f)
832842
}
833843
}
834844

@@ -1138,6 +1148,9 @@ pub fn build_session_(
11381148
CguReuseTracker::new_disabled()
11391149
};
11401150

1151+
let self_profiling_active = sopts.debugging_opts.self_profile ||
1152+
sopts.debugging_opts.profile_json;
1153+
11411154
let sess = Session {
11421155
target: target_cfg,
11431156
host,
@@ -1168,6 +1181,7 @@ pub fn build_session_(
11681181
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
11691182
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
11701183
cgu_reuse_tracker,
1184+
self_profiling_active,
11711185
self_profiling: Lock::new(SelfProfiler::new()),
11721186
profile_channel: Lock::new(None),
11731187
perf_stats: PerfStats {

src/librustc/ty/context.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1336,8 +1336,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13361336
self.cstore.crate_data_as_rc_any(cnum)
13371337
}
13381338

1339+
#[inline(always)]
13391340
pub fn create_stable_hashing_context(self) -> StableHashingContext<'a> {
1340-
let krate = self.dep_graph.with_ignore(|| self.hir().krate());
1341+
let krate = self.gcx.hir_map.forest.untracked_krate();
13411342

13421343
StableHashingContext::new(self.sess,
13431344
krate,
@@ -1925,23 +1926,33 @@ pub mod tls {
19251926

19261927
/// A thread local variable which stores a pointer to the current ImplicitCtxt
19271928
#[cfg(not(parallel_queries))]
1928-
thread_local!(static TLV: Cell<usize> = Cell::new(0));
1929+
// Accessing `thread_local` in another crate is bugged, so we have
1930+
// two accessors `set_raw_tlv` and `get_tlv` which do not have an
1931+
// inline attribute to prevent that
1932+
#[thread_local]
1933+
static TLV: Cell<usize> = Cell::new(0);
1934+
1935+
/// This is used to set the pointer to the current ImplicitCtxt.
1936+
#[cfg(not(parallel_queries))]
1937+
fn set_raw_tlv(value: usize) {
1938+
TLV.set(value)
1939+
}
19291940

19301941
/// Sets TLV to `value` during the call to `f`.
19311942
/// It is restored to its previous value after.
19321943
/// This is used to set the pointer to the new ImplicitCtxt.
19331944
#[cfg(not(parallel_queries))]
19341945
fn set_tlv<F: FnOnce() -> R, R>(value: usize, f: F) -> R {
19351946
let old = get_tlv();
1936-
let _reset = OnDrop(move || TLV.with(|tlv| tlv.set(old)));
1937-
TLV.with(|tlv| tlv.set(value));
1947+
let _reset = OnDrop(move || set_raw_tlv(old));
1948+
set_raw_tlv(value);
19381949
f()
19391950
}
19401951

19411952
/// This is used to get the pointer to the current ImplicitCtxt.
19421953
#[cfg(not(parallel_queries))]
19431954
fn get_tlv() -> usize {
1944-
TLV.with(|tlv| tlv.get())
1955+
TLV.get()
19451956
}
19461957

19471958
/// This is a callback from libsyntax as it cannot access the implicit state

src/librustc/ty/query/job.rs

+41-28
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ use syntax_pos::Span;
1818
use ty::tls;
1919
use ty::query::Query;
2020
use ty::query::plumbing::CycleError;
21+
#[cfg(not(parallel_queries))]
22+
use ty::query::{
23+
plumbing::TryGetJob,
24+
config::QueryDescription,
25+
};
2126
use ty::context::TyCtxt;
2227
use errors::Diagnostic;
2328
use std::process;
@@ -83,44 +88,52 @@ impl<'tcx> QueryJob<'tcx> {
8388
///
8489
/// For single threaded rustc there's no concurrent jobs running, so if we are waiting for any
8590
/// query that means that there is a query cycle, thus this always running a cycle error.
86-
pub(super) fn await<'lcx>(
91+
#[cfg(not(parallel_queries))]
92+
#[inline(never)]
93+
#[cold]
94+
pub(super) fn await<'lcx, 'a, D: QueryDescription<'tcx>>(
8795
&self,
8896
tcx: TyCtxt<'_, 'tcx, 'lcx>,
8997
span: Span,
90-
) -> Result<(), CycleError<'tcx>> {
91-
#[cfg(not(parallel_queries))]
92-
{
93-
self.find_cycle_in_stack(tcx, span)
94-
}
98+
) -> TryGetJob<'a, 'tcx, D> {
99+
TryGetJob::JobCompleted(Err(Box::new(self.find_cycle_in_stack(tcx, span))))
100+
}
95101

96-
#[cfg(parallel_queries)]
97-
{
98-
tls::with_related_context(tcx, move |icx| {
99-
let mut waiter = Lrc::new(QueryWaiter {
100-
query: icx.query.clone(),
101-
span,
102-
cycle: Lock::new(None),
103-
condvar: Condvar::new(),
104-
});
105-
self.latch.await(&waiter);
106-
// FIXME: Get rid of this lock. We have ownership of the QueryWaiter
107-
// although another thread may still have a Lrc reference so we cannot
108-
// use Lrc::get_mut
109-
let mut cycle = waiter.cycle.lock();
110-
match cycle.take() {
111-
None => Ok(()),
112-
Some(cycle) => Err(cycle)
113-
}
114-
})
115-
}
102+
/// Awaits for the query job to complete.
103+
///
104+
/// For single threaded rustc there's no concurrent jobs running, so if we are waiting for any
105+
/// query that means that there is a query cycle, thus this always running a cycle error.
106+
#[cfg(parallel_queries)]
107+
pub(super) fn await<'lcx>(
108+
&self,
109+
tcx: TyCtxt<'_, 'tcx, 'lcx>,
110+
span: Span,
111+
) -> Result<(), Box<CycleError<'tcx>>> {
112+
tls::with_related_context(tcx, move |icx| {
113+
let mut waiter = Lrc::new(QueryWaiter {
114+
query: icx.query.clone(),
115+
span,
116+
cycle: Lock::new(None),
117+
condvar: Condvar::new(),
118+
});
119+
self.latch.await(&waiter);
120+
// FIXME: Get rid of this lock. We have ownership of the QueryWaiter
121+
// although another thread may still have a Lrc reference so we cannot
122+
// use Lrc::get_mut
123+
let mut cycle = waiter.cycle.lock();
124+
match cycle.take() {
125+
None => Ok(()),
126+
Some(cycle) => Err(Box::new(cycle))
127+
}
128+
})
116129
}
117130

118131
#[cfg(not(parallel_queries))]
119132
fn find_cycle_in_stack<'lcx>(
120133
&self,
121134
tcx: TyCtxt<'_, 'tcx, 'lcx>,
122135
span: Span,
123-
) -> Result<(), CycleError<'tcx>> {
136+
) -> CycleError<'tcx> {
124137
// Get the current executing query (waiter) and find the waitee amongst its parents
125138
let mut current_job = tls::with_related_context(tcx, |icx| icx.query.clone());
126139
let mut cycle = Vec::new();
@@ -140,7 +153,7 @@ impl<'tcx> QueryJob<'tcx> {
140153
let usage = job.parent.as_ref().map(|parent| {
141154
(job.info.span, parent.info.query.clone())
142155
});
143-
return Err(CycleError { usage, cycle });
156+
return CycleError { usage, cycle };
144157
}
145158

146159
current_job = job.parent.clone();

src/librustc/ty/query/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -705,21 +705,21 @@ impl<'a, 'tcx, 'lcx> TyCtxt<'a, 'tcx, 'lcx> {
705705
self,
706706
span: Span,
707707
key: DefId,
708-
) -> Result<&'tcx [Ty<'tcx>], DiagnosticBuilder<'a>> {
708+
) -> Result<&'tcx [Ty<'tcx>], Box<DiagnosticBuilder<'a>>> {
709709
self.try_get_query::<queries::adt_sized_constraint<'_>>(span, key)
710710
}
711711
pub fn try_needs_drop_raw(
712712
self,
713713
span: Span,
714714
key: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
715-
) -> Result<bool, DiagnosticBuilder<'a>> {
715+
) -> Result<bool, Box<DiagnosticBuilder<'a>>> {
716716
self.try_get_query::<queries::needs_drop_raw<'_>>(span, key)
717717
}
718718
pub fn try_optimized_mir(
719719
self,
720720
span: Span,
721721
key: DefId,
722-
) -> Result<&'tcx mir::Mir<'tcx>, DiagnosticBuilder<'a>> {
722+
) -> Result<&'tcx mir::Mir<'tcx>, Box<DiagnosticBuilder<'a>>> {
723723
self.try_get_query::<queries::optimized_mir<'_>>(span, key)
724724
}
725725
}

0 commit comments

Comments
 (0)