Skip to content

Commit cf3e67e

Browse files
committed
Tweak query code for performance
1 parent 431e0ab commit cf3e67e

File tree

13 files changed

+157
-81
lines changed

13 files changed

+157
-81
lines changed

src/librustc/dep_graph/dep_node.rs

+23-12
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,7 +240,8 @@ macro_rules! define_dep_nodes {
230240

231241
impl DepNode {
232242
#[allow(unreachable_code, non_snake_case)]
233-
pub fn new<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
243+
#[inline(always)]
244+
pub fn new_inlined<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
234245
dep: DepConstructor<'gcx>)
235246
-> DepNode
236247
where 'gcx: 'a + 'tcx,
@@ -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/dep_graph/graph.rs

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ impl DepGraph {
239239
arg: A,
240240
no_tcx: bool,
241241
task: fn(C, A) -> R,
242+
// FIXME: Take OpenTask as a parameter instead
242243
create_task: fn(DepNode) -> OpenTask,
243244
finish_task_and_alloc_depnode: fn(&Lock<CurrentDepGraph>,
244245
DepNode,

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

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
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)]

src/librustc/session/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -825,13 +825,22 @@ impl Session {
825825
}
826826
}
827827

828-
pub fn profiler<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
828+
#[inline(never)]
829+
#[cold]
830+
fn profiler_active<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
829831
if self.opts.debugging_opts.self_profile {
830832
let mut profiler = self.self_profiling.borrow_mut();
831833
f(&mut profiler);
832834
}
833835
}
834836

837+
#[inline(always)]
838+
pub fn profiler<F: FnOnce(&mut SelfProfiler) -> ()>(&self, f: F) {
839+
if unsafe { std::intrinsics::unlikely(self.opts.debugging_opts.self_profile) } {
840+
self.profiler_active(f)
841+
}
842+
}
843+
835844
pub fn print_profiler_results(&self) {
836845
let mut profiler = self.self_profiling.borrow_mut();
837846
profiler.print_results(&self.opts);

src/librustc/ty/context.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -1331,8 +1331,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13311331
self.cstore.crate_data_as_rc_any(cnum)
13321332
}
13331333

1334+
#[inline(always)]
13341335
pub fn create_stable_hashing_context(self) -> StableHashingContext<'a> {
1335-
let krate = self.dep_graph.with_ignore(|| self.gcx.hir.krate());
1336+
let krate = self.gcx.hir.forest.untracked_krate();
13361337

13371338
StableHashingContext::new(self.sess,
13381339
krate,
@@ -1349,7 +1350,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13491350
// We cannot use the query versions of crates() and crate_hash(), since
13501351
// those would need the DepNodes that we are allocating here.
13511352
for cnum in self.cstore.crates_untracked() {
1352-
let dep_node = DepNode::new(self, DepConstructor::CrateMetadata(cnum));
1353+
let dep_node = DepNode::new_inlined(self, DepConstructor::CrateMetadata(cnum));
13531354
let crate_hash = self.cstore.crate_hash_untracked(cnum);
13541355
self.dep_graph.with_task(dep_node,
13551356
self,
@@ -1926,23 +1927,33 @@ pub mod tls {
19261927

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

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

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

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

src/librustc/ty/query/job.rs

+38-25
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,41 +88,49 @@ 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-
}
95-
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);
98+
) -> TryGetJob<'a, 'tcx, D> {
99+
TryGetJob::JobCompleted(Err(Box::new(self.find_cycle_in_stack(tcx, span))))
100+
}
106101

107-
match Lrc::get_mut(&mut waiter).unwrap().cycle.get_mut().take() {
108-
None => Ok(()),
109-
Some(cycle) => Err(cycle)
110-
}
111-
})
112-
}
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+
121+
match Lrc::get_mut(&mut waiter).unwrap().cycle.get_mut().take() {
122+
None => Ok(()),
123+
Some(cycle) => Err(Box::new(cycle))
124+
}
125+
})
113126
}
114127

115128
#[cfg(not(parallel_queries))]
116129
fn find_cycle_in_stack<'lcx>(
117130
&self,
118131
tcx: TyCtxt<'_, 'tcx, 'lcx>,
119132
span: Span,
120-
) -> Result<(), CycleError<'tcx>> {
133+
) -> CycleError<'tcx> {
121134
// Get the current executing query (waiter) and find the waitee amongst its parents
122135
let mut current_job = tls::with_related_context(tcx, |icx| icx.query.clone());
123136
let mut cycle = Vec::new();
@@ -137,7 +150,7 @@ impl<'tcx> QueryJob<'tcx> {
137150
let usage = job.parent.as_ref().map(|parent| {
138151
(job.info.span, parent.info.query.clone())
139152
});
140-
return Err(CycleError { usage, cycle });
153+
return CycleError { usage, cycle };
141154
}
142155

143156
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)