Skip to content

Commit a00e24d

Browse files
committed
Auto merge of #107529 - Zoxc:inline-tweak-profile, r=cjgillot
Optimize query_cache_hit to reduce code size of the query hot path. A small tweak which improves performance on check builds by 0.33% and reduces `rustc_driver` size by 1%. <table><tr><td rowspan="2">Benchmark</td><td colspan="1"><b>Before</b></th><td colspan="2"><b>Before</b></th><td colspan="2"><b>After</b></th></tr><tr><td align="right">Time</td><td align="right">Time</td><td align="right">%</th><td align="right">Time</td><td align="right">%</th></tr><tr><td>🟣 <b>clap</b>:check</td><td align="right">1.7978s</td><td align="right">1.7980s</td><td align="right"> 0.01%</td><td align="right">1.7930s</td><td align="right"> -0.27%</td></tr><tr><td>🟣 <b>hyper</b>:check</td><td align="right">0.2594s</td><td align="right">0.2591s</td><td align="right"> -0.12%</td><td align="right">0.2592s</td><td align="right"> -0.09%</td></tr><tr><td>🟣 <b>syntex_syntax</b>:check</td><td align="right">6.2522s</td><td align="right">6.2540s</td><td align="right"> 0.03%</td><td align="right">6.2358s</td><td align="right"> -0.26%</td></tr><tr><td>🟣 <b>syn</b>:check</td><td align="right">1.5889s</td><td align="right">1.5880s</td><td align="right"> -0.05%</td><td align="right">1.5799s</td><td align="right"> -0.57%</td></tr><tr><td>🟣 <b>regex</b>:check</td><td align="right">0.9941s</td><td align="right">0.9939s</td><td align="right"> -0.02%</td><td align="right">0.9893s</td><td align="right"> -0.49%</td></tr><tr><td>Total</td><td align="right">10.8925s</td><td align="right">10.8930s</td><td align="right"> 0.01%</td><td align="right">10.8572s</td><td align="right"> -0.32%</td></tr><tr><td>Summary</td><td align="right">1.0000s</td><td align="right">0.9997s</td><td align="right"> -0.03%</td><td align="right">0.9967s</td><td align="right"> -0.33%</td></tr></table> r? `@cjgillot`
2 parents 6eb9f2d + 9539737 commit a00e24d

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
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

+21-18
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;
@@ -395,11 +396,18 @@ impl SelfProfilerRef {
395396
/// Record a query in-memory cache hit.
396397
#[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

+3-9
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))
@@ -776,9 +772,7 @@ where
776772
// Ensure that only one of them runs the query.
777773
let cache = Q::query_cache(qcx);
778774
if let Some((_, index)) = cache.lookup(&key) {
779-
if std::intrinsics::unlikely(qcx.dep_context().profiler().enabled()) {
780-
qcx.dep_context().profiler().query_cache_hit(index.into());
781-
}
775+
qcx.dep_context().profiler().query_cache_hit(index.into());
782776
return;
783777
}
784778

0 commit comments

Comments
 (0)