Skip to content

Commit 790f4c5

Browse files
committed
Auto merge of #56613 - Zoxc:query-perf1, r=michaelwoerister
Tweak query code for performance Split from #56509 r? @michaelwoerister
2 parents 0a4a4ff + f0adf5a commit 790f4c5

File tree

12 files changed

+167
-75
lines changed

12 files changed

+167
-75
lines changed

src/librustc/dep_graph/dep_node.rs

+13-10
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,7 +173,7 @@ macro_rules! define_dep_nodes {
171173
}
172174
}
173175

174-
#[inline]
176+
#[inline(always)]
175177
pub fn is_input(&self) -> bool {
176178
match *self {
177179
$(
@@ -180,7 +182,7 @@ macro_rules! define_dep_nodes {
180182
}
181183
}
182184

183-
#[inline]
185+
#[inline(always)]
184186
pub fn is_eval_always(&self) -> bool {
185187
match *self {
186188
$(
@@ -190,7 +192,7 @@ macro_rules! define_dep_nodes {
190192
}
191193

192194
#[allow(unreachable_code)]
193-
#[inline]
195+
#[inline(always)]
194196
pub fn has_params(&self) -> bool {
195197
match *self {
196198
$(
@@ -230,6 +232,7 @@ macro_rules! define_dep_nodes {
230232

231233
impl DepNode {
232234
#[allow(unreachable_code, non_snake_case)]
235+
#[inline(always)]
233236
pub fn new<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
234237
dep: DepConstructor<'gcx>)
235238
-> DepNode
@@ -299,11 +302,11 @@ macro_rules! define_dep_nodes {
299302
/// Construct a DepNode from the given DepKind and DefPathHash. This
300303
/// method will assert that the given DepKind actually requires a
301304
/// single DefId/DefPathHash parameter.
302-
#[inline]
305+
#[inline(always)]
303306
pub fn from_def_path_hash(kind: DepKind,
304307
def_path_hash: DefPathHash)
305308
-> DepNode {
306-
assert!(kind.can_reconstruct_query_key() && kind.has_params());
309+
debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
307310
DepNode {
308311
kind,
309312
hash: def_path_hash.0,
@@ -313,9 +316,9 @@ macro_rules! define_dep_nodes {
313316
/// Create a new, parameterless DepNode. This method will assert
314317
/// that the DepNode corresponding to the given DepKind actually
315318
/// does not require any parameters.
316-
#[inline]
319+
#[inline(always)]
317320
pub fn new_no_params(kind: DepKind) -> DepNode {
318-
assert!(!kind.has_params());
321+
debug_assert!(!kind.has_params());
319322
DepNode {
320323
kind,
321324
hash: Fingerprint::ZERO,
@@ -418,14 +421,14 @@ impl fmt::Debug for DepNode {
418421

419422

420423
impl DefPathHash {
421-
#[inline]
424+
#[inline(always)]
422425
pub fn to_dep_node(self, kind: DepKind) -> DepNode {
423426
DepNode::from_def_path_hash(kind, self)
424427
}
425428
}
426429

427430
impl DefId {
428-
#[inline]
431+
#[inline(always)]
429432
pub fn to_dep_node(self, tcx: TyCtxt<'_, '_, '_>, kind: DepKind) -> DepNode {
430433
DepNode::from_def_path_hash(kind, tcx.def_path_hash(self))
431434
}

src/librustc/hir/map/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ impl Forest {
159159
self.dep_graph.read(DepNode::new_no_params(DepKind::Krate));
160160
&self.krate
161161
}
162+
163+
/// This is internally in the depedency tracking system.
164+
/// Use the `krate` method to ensure your dependency on the
165+
/// crate is tracked.
166+
pub fn untracked_krate<'hir>(&'hir self) -> &'hir Crate {
167+
&self.krate
168+
}
162169
}
163170

164171
/// 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
@@ -131,6 +131,9 @@ pub struct Session {
131131
/// Used by -Z profile-queries in util::common
132132
pub profile_channel: Lock<Option<mpsc::Sender<ProfileQueriesMsg>>>,
133133

134+
/// Used by -Z self-profile
135+
pub self_profiling_active: bool,
136+
134137
/// Used by -Z self-profile
135138
pub self_profiling: Lock<SelfProfiler>,
136139

@@ -823,10 +826,17 @@ impl Session {
823826
}
824827
}
825828

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

@@ -1145,6 +1155,9 @@ pub fn build_session_(
11451155
CguReuseTracker::new_disabled()
11461156
};
11471157

1158+
let self_profiling_active = sopts.debugging_opts.self_profile ||
1159+
sopts.debugging_opts.profile_json;
1160+
11481161
let sess = Session {
11491162
target: target_cfg,
11501163
host,
@@ -1177,6 +1190,7 @@ pub fn build_session_(
11771190
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
11781191
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
11791192
cgu_reuse_tracker,
1193+
self_profiling_active,
11801194
self_profiling: Lock::new(SelfProfiler::new()),
11811195
profile_channel: Lock::new(None),
11821196
perf_stats: PerfStats {

src/librustc/ty/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13431343
self.cstore.crate_data_as_rc_any(cnum)
13441344
}
13451345

1346+
#[inline(always)]
13461347
pub fn create_stable_hashing_context(self) -> StableHashingContext<'a> {
1347-
let krate = self.dep_graph.with_ignore(|| self.hir().krate());
1348+
let krate = self.gcx.hir_map.forest.untracked_krate();
13481349

13491350
StableHashingContext::new(self.sess,
13501351
krate,

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 cycle_error<'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)