Skip to content

Commit 085c58f

Browse files
committed
Auto merge of #153387 - Zalathar:call-query, r=nnethercote
Get rid of `QueryVTable::call_query_method_fn` Calling the query method to promote a value is equivalent to doing a cache lookup and then calling `execute_query_fn`, so we can just do that manually instead. There are two “functional” differences here: If a cache hit occurs, we don't record the hit for self-profiling, and we don't register a read of the dep node. In the context of promotion, which touches *all* eligible cache entries just before writing the memory-cached values to disk, those two steps should be unnecessary overhead anyway. r? nnethercote (or compiler)
2 parents ea5573a + 9012d4e commit 085c58f

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
114114
pub state: QueryState<'tcx, C::Key>,
115115
pub cache: C,
116116

117-
/// Function pointer that calls `tcx.$query(key)` for this query and
118-
/// discards the returned value.
119-
///
120-
/// This is a weird thing to be doing, and probably not what you want.
121-
/// It is used for loading query results from disk-cache in some cases.
122-
pub call_query_method_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key),
123-
124117
/// Function pointer that actually calls this query's provider.
125118
/// Also performs some associated secondary tasks; see the macro-defined
126119
/// implementation in `mod invoke_provider_fn` for more details.

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ use rustc_middle::query::on_disk_cache::{
1919
};
2020
use rustc_middle::query::plumbing::QueryVTable;
2121
use rustc_middle::query::{
22-
QueryCache, QueryJobId, QueryKey, QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra,
23-
erase,
22+
QueryCache, QueryJobId, QueryKey, QueryMode, QueryStackDeferred, QueryStackFrame,
23+
QueryStackFrameExtra, erase,
2424
};
2525
use rustc_middle::ty::codec::TyEncoder;
2626
use rustc_middle::ty::print::with_reduced_queries;
2727
use rustc_middle::ty::tls::{self, ImplicitCtxt};
2828
use rustc_middle::ty::{self, TyCtxt};
2929
use rustc_serialize::{Decodable, Encodable};
30+
use rustc_span::DUMMY_SP;
3031
use rustc_span::def_id::LOCAL_CRATE;
3132

3233
use crate::error::{QueryOverflow, QueryOverflowNote};
@@ -215,10 +216,27 @@ pub(crate) fn promote_from_disk_inner<'tcx, Q: GetQueryVTable<'tcx>>(
215216
dep_node.key_fingerprint
216217
)
217218
});
218-
if (query.will_cache_on_disk_for_key_fn)(tcx, &key) {
219-
// Call `tcx.$query(key)` for its side-effect of loading the disk-cached
220-
// value into memory.
221-
(query.call_query_method_fn)(tcx, key);
219+
220+
// If the recovered key isn't eligible for cache-on-disk, then there's no
221+
// value on disk to promote.
222+
if !(query.will_cache_on_disk_for_key_fn)(tcx, &key) {
223+
return;
224+
}
225+
226+
match query.cache.lookup(&key) {
227+
// If the value is already in memory, then promotion isn't needed.
228+
Some(_) => {}
229+
230+
// "Execute" the query to load its disk-cached value into memory.
231+
//
232+
// We know that the key is cache-on-disk and its node is green,
233+
// so there _must_ be a value on disk to load.
234+
//
235+
// FIXME(Zalathar): Is there a reasonable way to skip more of the
236+
// query bookkeeping when doing this?
237+
None => {
238+
(query.execute_query_fn)(tcx, DUMMY_SP, key, QueryMode::Get);
239+
}
222240
}
223241
}
224242

@@ -421,11 +439,6 @@ macro_rules! define_queries {
421439
state: Default::default(),
422440
cache: Default::default(),
423441

424-
call_query_method_fn: |tcx, key| {
425-
// Call the query method for its side-effect of loading a value
426-
// from disk-cache; the caller doesn't need the value.
427-
let _ = tcx.$name(key);
428-
},
429442
invoke_provider_fn: self::invoke_provider_fn::__rust_begin_short_backtrace,
430443

431444
#[cfg($cache_on_disk)]

0 commit comments

Comments
 (0)