Skip to content

Optimize query_cache_hit to reduce code size of the query hot path. #107529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_data_structures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![feature(associated_type_bounds)]
#![feature(auto_traits)]
#![feature(cell_leak)]
#![feature(core_intrinsics)]
#![feature(extend_one)]
#![feature(hash_raw_entry)]
#![feature(hasher_prefixfree_extras)]
Expand Down
39 changes: 21 additions & 18 deletions compiler/rustc_data_structures/src/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ use std::borrow::Borrow;
use std::collections::hash_map::Entry;
use std::error::Error;
use std::fs;
use std::intrinsics::unlikely;
use std::path::Path;
use std::process;
use std::sync::Arc;
Expand Down Expand Up @@ -395,11 +396,18 @@ impl SelfProfilerRef {
/// Record a query in-memory cache hit.
#[inline(always)]
pub fn query_cache_hit(&self, query_invocation_id: QueryInvocationId) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same should probably be done for the other profiling events as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem was that it generated code for TimingGuard which was unused. The other events do make use of it so it outlining doesn't help.

self.instant_query_event(
|profiler| profiler.query_cache_hit_event_kind,
query_invocation_id,
EventFilter::QUERY_CACHE_HITS,
);
#[inline(never)]
#[cold]
fn cold_call(profiler_ref: &SelfProfilerRef, query_invocation_id: QueryInvocationId) {
profiler_ref.instant_query_event(
|profiler| profiler.query_cache_hit_event_kind,
query_invocation_id,
);
}

if unlikely(self.event_filter_mask.contains(EventFilter::QUERY_CACHE_HITS)) {
cold_call(self, query_invocation_id);
}
}

/// Start profiling a query being blocked on a concurrent execution.
Expand Down Expand Up @@ -444,20 +452,15 @@ impl SelfProfilerRef {
&self,
event_kind: fn(&SelfProfiler) -> StringId,
query_invocation_id: QueryInvocationId,
event_filter: EventFilter,
) {
drop(self.exec(event_filter, |profiler| {
let event_id = StringId::new_virtual(query_invocation_id.0);
let thread_id = get_thread_id();

profiler.profiler.record_instant_event(
event_kind(profiler),
EventId::from_virtual(event_id),
thread_id,
);

TimingGuard::none()
}));
let event_id = StringId::new_virtual(query_invocation_id.0);
let thread_id = get_thread_id();
let profiler = self.profiler.as_ref().unwrap();
profiler.profiler.record_instant_event(
event_kind(profiler),
EventId::from_virtual(event_id),
thread_id,
);
}

pub fn with_profiler(&self, f: impl FnOnce(&SelfProfiler)) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_query_system/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl DepNodeIndex {
}

impl From<DepNodeIndex> for QueryInvocationId {
#[inline]
#[inline(always)]
fn from(dep_node_index: DepNodeIndex) -> Self {
QueryInvocationId(dep_node_index.as_u32())
}
Expand Down
12 changes: 3 additions & 9 deletions compiler/rustc_query_system/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,7 @@ where
{
match cache.lookup(&key) {
Some((value, index)) => {
if std::intrinsics::unlikely(tcx.profiler().enabled()) {
tcx.profiler().query_cache_hit(index.into());
}
tcx.profiler().query_cache_hit(index.into());
tcx.dep_graph().read_index(index);
Some(value)
}
Expand Down Expand Up @@ -408,9 +406,7 @@ where
panic!("value must be in cache after waiting")
};

if std::intrinsics::unlikely(qcx.dep_context().profiler().enabled()) {
qcx.dep_context().profiler().query_cache_hit(index.into());
}
qcx.dep_context().profiler().query_cache_hit(index.into());
query_blocked_prof_timer.finish_with_query_invocation_id(index.into());

(v, Some(index))
Expand Down Expand Up @@ -776,9 +772,7 @@ where
// Ensure that only one of them runs the query.
let cache = Q::query_cache(qcx);
if let Some((_, index)) = cache.lookup(&key) {
if std::intrinsics::unlikely(qcx.dep_context().profiler().enabled()) {
qcx.dep_context().profiler().query_cache_hit(index.into());
}
qcx.dep_context().profiler().query_cache_hit(index.into());
return;
}

Expand Down