Skip to content

Commit 85704a4

Browse files
committed
Monomorphise load_from_disk_and_cache_in_memory.
1 parent 1b2deaf commit 85704a4

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

src/librustc_macros/src/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ fn add_query_description_impl(
365365
#[allow(unused_variables, unused_braces)]
366366
fn cache_on_disk(
367367
#tcx: TyCtxt<'tcx>,
368-
#key: Self::Key,
368+
#key: &Self::Key,
369369
#value: Option<&Self::Value>
370370
) -> bool {
371371
#expr
@@ -441,7 +441,7 @@ pub fn rustc_queries(input: TokenStream) -> TokenStream {
441441
.unwrap_or(false));
442442

443443
let key = <#arg as DepNodeParams<TyCtxt<'_>>>::recover($tcx, $dep_node).unwrap();
444-
if queries::#name::cache_on_disk($tcx, key, None) {
444+
if queries::#name::cache_on_disk($tcx, &key, None) {
445445
let _ = $tcx.#name(key);
446446
}
447447
}

src/librustc_middle/ty/query/on_disk_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ where
10091009

10101010
state.iter_results(|results| {
10111011
for (key, value, dep_node) in results {
1012-
if Q::cache_on_disk(tcx, key.clone(), Some(&value)) {
1012+
if Q::cache_on_disk(tcx, &key, Some(&value)) {
10131013
let dep_node = SerializedDepNodeIndex::new(dep_node.index());
10141014

10151015
// Record position of the cache entry.

src/librustc_query_system/query/config.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ pub(crate) struct QueryVtable<CTX: QueryContext, K, V> {
3131
pub compute: fn(CTX, K) -> V,
3232

3333
pub hash_result: fn(&mut CTX::StableHashingContext, &V) -> Option<Fingerprint>,
34+
pub cache_on_disk: fn(CTX, &K, Option<&V>) -> bool,
35+
pub try_load_from_disk: fn(CTX, SerializedDepNodeIndex) -> Option<V>,
36+
}
37+
38+
impl<CTX: QueryContext, K, V> QueryVtable<CTX, K, V> {
39+
pub(crate) fn compute(&self, tcx: CTX, key: K) -> V {
40+
(self.compute)(tcx, key)
41+
}
42+
43+
pub(crate) fn hash_result(
44+
&self,
45+
hcx: &mut CTX::StableHashingContext,
46+
value: &V,
47+
) -> Option<Fingerprint> {
48+
(self.hash_result)(hcx, value)
49+
}
50+
51+
pub(crate) fn cache_on_disk(&self, tcx: CTX, key: &K, value: Option<&V>) -> bool {
52+
(self.cache_on_disk)(tcx, key, value)
53+
}
54+
55+
pub(crate) fn try_load_from_disk(&self, tcx: CTX, index: SerializedDepNodeIndex) -> Option<V> {
56+
(self.try_load_from_disk)(tcx, index)
57+
}
3458
}
3559

3660
pub trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
@@ -60,7 +84,7 @@ pub trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
6084
fn describe(tcx: CTX, key: Self::Key) -> Cow<'static, str>;
6185

6286
#[inline]
63-
fn cache_on_disk(_: CTX, _: Self::Key, _: Option<&Self::Value>) -> bool {
87+
fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
6488
false
6589
}
6690

@@ -82,6 +106,8 @@ where
82106
eval_always: Q::EVAL_ALWAYS,
83107
compute: Q::compute,
84108
hash_result: Q::hash_result,
109+
cache_on_disk: Q::cache_on_disk,
110+
try_load_from_disk: Q::try_load_from_disk,
85111
};
86112
}
87113

@@ -98,7 +124,7 @@ where
98124
}
99125
}
100126

101-
default fn cache_on_disk(_: CTX, _: Self::Key, _: Option<&Self::Value>) -> bool {
127+
default fn cache_on_disk(_: CTX, _: &Self::Key, _: Option<&Self::Value>) -> bool {
102128
false
103129
}
104130

src/librustc_query_system/query/plumbing.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -439,12 +439,13 @@ where
439439
let marked = tcx.dep_graph().try_mark_green_and_read(tcx, &dep_node);
440440
marked.map(|(prev_dep_node_index, dep_node_index)| {
441441
(
442-
load_from_disk_and_cache_in_memory::<Q, _>(
442+
load_from_disk_and_cache_in_memory(
443443
tcx,
444444
key.clone(),
445445
prev_dep_node_index,
446446
dep_node_index,
447447
&dep_node,
448+
&Q::VTABLE,
448449
),
449450
dep_node_index,
450451
)
@@ -460,26 +461,26 @@ where
460461
result
461462
}
462463

463-
fn load_from_disk_and_cache_in_memory<Q, CTX>(
464+
fn load_from_disk_and_cache_in_memory<CTX, K, V>(
464465
tcx: CTX,
465-
key: Q::Key,
466+
key: K,
466467
prev_dep_node_index: SerializedDepNodeIndex,
467468
dep_node_index: DepNodeIndex,
468469
dep_node: &DepNode<CTX::DepKind>,
469-
) -> Q::Value
470+
query: &QueryVtable<CTX, K, V>,
471+
) -> V
470472
where
471473
CTX: QueryContext,
472-
Q: QueryDescription<CTX>,
473474
{
474475
// Note this function can be called concurrently from the same query
475476
// We must ensure that this is handled correctly.
476477

477478
debug_assert!(tcx.dep_graph().is_green(dep_node));
478479

479480
// First we try to load the result from the on-disk cache.
480-
let result = if Q::cache_on_disk(tcx, key.clone(), None) {
481+
let result = if query.cache_on_disk(tcx, &key, None) {
481482
let prof_timer = tcx.profiler().incr_cache_loading();
482-
let result = Q::try_load_from_disk(tcx, prev_dep_node_index);
483+
let result = query.try_load_from_disk(tcx, prev_dep_node_index);
483484
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
484485

485486
// We always expect to find a cached result for things that
@@ -503,7 +504,7 @@ where
503504
let prof_timer = tcx.profiler().query_provider();
504505

505506
// The dep-graph for this computation is already in-place.
506-
let result = tcx.dep_graph().with_ignore(|| Q::compute(tcx, key));
507+
let result = tcx.dep_graph().with_ignore(|| query.compute(tcx, key));
507508

508509
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
509510

@@ -513,22 +514,22 @@ where
513514
// If `-Zincremental-verify-ich` is specified, re-hash results from
514515
// the cache and make sure that they have the expected fingerprint.
515516
if unlikely!(tcx.incremental_verify_ich()) {
516-
incremental_verify_ich::<Q, _>(tcx, &result, dep_node, dep_node_index);
517+
incremental_verify_ich(tcx, &result, dep_node, dep_node_index, query);
517518
}
518519

519520
result
520521
}
521522

522523
#[inline(never)]
523524
#[cold]
524-
fn incremental_verify_ich<Q, CTX>(
525+
fn incremental_verify_ich<CTX, K, V>(
525526
tcx: CTX,
526-
result: &Q::Value,
527+
result: &V,
527528
dep_node: &DepNode<CTX::DepKind>,
528529
dep_node_index: DepNodeIndex,
530+
query: &QueryVtable<CTX, K, V>,
529531
) where
530532
CTX: QueryContext,
531-
Q: QueryDescription<CTX>,
532533
{
533534
assert!(
534535
Some(tcx.dep_graph().fingerprint_of(dep_node_index))
@@ -540,7 +541,7 @@ fn incremental_verify_ich<Q, CTX>(
540541
debug!("BEGIN verify_ich({:?})", dep_node);
541542
let mut hcx = tcx.create_stable_hashing_context();
542543

543-
let new_hash = Q::hash_result(&mut hcx, result).unwrap_or(Fingerprint::ZERO);
544+
let new_hash = query.hash_result(&mut hcx, result).unwrap_or(Fingerprint::ZERO);
544545
debug!("END verify_ich({:?})", dep_node);
545546

546547
let old_hash = tcx.dep_graph().fingerprint_of(dep_node_index);

0 commit comments

Comments
 (0)