Skip to content

Commit 9539737

Browse files
committed
Make an optimal cold path for query_cache_hit
1 parent e60ccfc commit 9539737

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(associated_type_bounds)]
1212
#![feature(auto_traits)]
1313
#![feature(cell_leak)]
14+
#![feature(core_intrinsics)]
1415
#![feature(extend_one)]
1516
#![feature(hash_raw_entry)]
1617
#![feature(hasher_prefixfree_extras)]

compiler/rustc_data_structures/src/profiling.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ use std::borrow::Borrow;
8888
use std::collections::hash_map::Entry;
8989
use std::error::Error;
9090
use std::fs;
91+
use std::intrinsics::unlikely;
9192
use std::path::Path;
9293
use std::process;
9394
use std::sync::Arc;
@@ -393,13 +394,20 @@ impl SelfProfilerRef {
393394
}
394395

395396
/// Record a query in-memory cache hit.
396-
#[inline(never)]
397+
#[inline(always)]
397398
pub fn query_cache_hit(&self, query_invocation_id: QueryInvocationId) {
398-
self.instant_query_event(
399-
|profiler| profiler.query_cache_hit_event_kind,
400-
query_invocation_id,
401-
EventFilter::QUERY_CACHE_HITS,
402-
);
399+
#[inline(never)]
400+
#[cold]
401+
fn cold_call(profiler_ref: &SelfProfilerRef, query_invocation_id: QueryInvocationId) {
402+
profiler_ref.instant_query_event(
403+
|profiler| profiler.query_cache_hit_event_kind,
404+
query_invocation_id,
405+
);
406+
}
407+
408+
if unlikely(self.event_filter_mask.contains(EventFilter::QUERY_CACHE_HITS)) {
409+
cold_call(self, query_invocation_id);
410+
}
403411
}
404412

405413
/// Start profiling a query being blocked on a concurrent execution.
@@ -444,20 +452,15 @@ impl SelfProfilerRef {
444452
&self,
445453
event_kind: fn(&SelfProfiler) -> StringId,
446454
query_invocation_id: QueryInvocationId,
447-
event_filter: EventFilter,
448455
) {
449-
drop(self.exec(event_filter, |profiler| {
450-
let event_id = StringId::new_virtual(query_invocation_id.0);
451-
let thread_id = get_thread_id();
452-
453-
profiler.profiler.record_instant_event(
454-
event_kind(profiler),
455-
EventId::from_virtual(event_id),
456-
thread_id,
457-
);
458-
459-
TimingGuard::none()
460-
}));
456+
let event_id = StringId::new_virtual(query_invocation_id.0);
457+
let thread_id = get_thread_id();
458+
let profiler = self.profiler.as_ref().unwrap();
459+
profiler.profiler.record_instant_event(
460+
event_kind(profiler),
461+
EventId::from_virtual(event_id),
462+
thread_id,
463+
);
461464
}
462465

463466
pub fn with_profiler(&self, f: impl FnOnce(&SelfProfiler)) {

compiler/rustc_query_system/src/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl DepNodeIndex {
4747
}
4848

4949
impl From<DepNodeIndex> for QueryInvocationId {
50-
#[inline]
50+
#[inline(always)]
5151
fn from(dep_node_index: DepNodeIndex) -> Self {
5252
QueryInvocationId(dep_node_index.as_u32())
5353
}

compiler/rustc_query_system/src/query/plumbing.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,7 @@ where
346346
{
347347
match cache.lookup(&key) {
348348
Some((value, index)) => {
349-
if std::intrinsics::unlikely(tcx.profiler().enabled()) {
350-
tcx.profiler().query_cache_hit(index.into());
351-
}
349+
tcx.profiler().query_cache_hit(index.into());
352350
tcx.dep_graph().read_index(index);
353351
Some(value)
354352
}
@@ -408,9 +406,7 @@ where
408406
panic!("value must be in cache after waiting")
409407
};
410408

411-
if std::intrinsics::unlikely(qcx.dep_context().profiler().enabled()) {
412-
qcx.dep_context().profiler().query_cache_hit(index.into());
413-
}
409+
qcx.dep_context().profiler().query_cache_hit(index.into());
414410
query_blocked_prof_timer.finish_with_query_invocation_id(index.into());
415411

416412
(v, Some(index))
@@ -722,9 +718,7 @@ where
722718
}
723719
Some((_, dep_node_index)) => {
724720
dep_graph.read_index(dep_node_index);
725-
if std::intrinsics::unlikely(qcx.dep_context().profiler().enabled()) {
726-
qcx.dep_context().profiler().query_cache_hit(dep_node_index.into());
727-
}
721+
qcx.dep_context().profiler().query_cache_hit(dep_node_index.into());
728722
(false, None)
729723
}
730724
}
@@ -778,9 +772,7 @@ where
778772
// Ensure that only one of them runs the query.
779773
let cache = Q::query_cache(qcx);
780774
if let Some((_, index)) = cache.lookup(&key) {
781-
if std::intrinsics::unlikely(qcx.dep_context().profiler().enabled()) {
782-
qcx.dep_context().profiler().query_cache_hit(index.into());
783-
}
775+
qcx.dep_context().profiler().query_cache_hit(index.into());
784776
return;
785777
}
786778

0 commit comments

Comments
 (0)