Skip to content

Commit 3675fdd

Browse files
committed
Get rid of OnHit by switching on QueryMode instead of unconditionally returning Option
I think `copy` may have added some overhead? unclear what's causing the perf impact.
1 parent f8b019f commit 3675fdd

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

compiler/rustc_middle/src/ty/query.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ impl<'tcx> TyCtxt<'tcx> {
102102
}
103103
}
104104

105-
/// Helper to ensure that queries only return `Copy` types.
106-
#[inline(always)]
107-
fn copy<T: Copy>(x: &T) -> T {
108-
*x
109-
}
110-
111105
fn evaluate_query<'tcx, Cache>(
112106
tcx: TyCtxt<'tcx>,
113107
execute_query: fn(
@@ -126,11 +120,9 @@ where
126120
Cache::Stored: Copy,
127121
Cache: QueryCache,
128122
{
129-
let cached = try_get_cached(tcx, query_cache, &key, copy);
130-
131-
match cached {
132-
Ok(value) => return value,
133-
Err(()) => (),
123+
match try_get_cached(tcx, query_cache, &key, mode) {
124+
Ok(value) => value,
125+
Err(()) => execute_query(tcx.queries, tcx, span, key, mode),
134126
}
135127
}
136128

compiler/rustc_query_system/src/query/plumbing.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -303,24 +303,23 @@ where
303303
/// which will be used if the query is not in the cache and we need
304304
/// to compute it.
305305
#[inline]
306-
pub fn try_get_cached<'a, CTX, C, R, OnHit>(
306+
pub fn try_get_cached<'a, CTX, C>(
307307
tcx: CTX,
308308
cache: &'a C,
309309
key: &C::Key,
310-
// `on_hit` can be called while holding a lock to the query cache
311-
on_hit: OnHit,
312-
) -> Result<R, ()>
310+
mode: QueryMode,
311+
) -> Result<Option<C::Stored>, ()>
313312
where
314313
C: QueryCache,
314+
C::Stored: Copy,
315315
CTX: DepContext,
316-
OnHit: FnOnce(&C::Stored) -> R,
317316
{
318317
cache.lookup(&key, |value, index| {
319318
if std::intrinsics::unlikely(tcx.profiler().enabled()) {
320319
tcx.profiler().query_cache_hit(index.into());
321320
}
322321
tcx.dep_graph().read_index(index);
323-
on_hit(value)
322+
if matches!(mode, QueryMode::Ensure) { None } else { Some(*value) }
324323
})
325324
}
326325

@@ -676,7 +675,7 @@ where
676675
}
677676
}
678677

679-
#[derive(Debug)]
678+
#[derive(Copy, Clone, Debug)]
680679
pub enum QueryMode {
681680
Get,
682681
Ensure,

0 commit comments

Comments
 (0)