Don't create a new try_load_from_disk closure for each query

Instead, define a single function, parameterized only by the return type.
This commit is contained in:
Joshua Nelson 2022-09-01 22:20:17 -05:00
parent 112419c9f0
commit b164dbc271
3 changed files with 27 additions and 2 deletions

View File

@ -255,7 +255,7 @@ fn add_query_description_impl(query: &Query, impls: &mut proc_macro2::TokenStrea
} }
const TRY_LOAD_FROM_DISK: Option<fn(QueryCtxt<'tcx>, SerializedDepNodeIndex) -> Option<Self::Value>> const TRY_LOAD_FROM_DISK: Option<fn(QueryCtxt<'tcx>, SerializedDepNodeIndex) -> Option<Self::Value>>
= Some(|tcx, id| tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id)); = Some(crate::plumbing::try_load_from_disk::<Self::Value>);
} }
} else { } else {
quote! { quote! {

View File

@ -17,7 +17,7 @@ extern crate rustc_middle;
use rustc_data_structures::sync::AtomicU64; use rustc_data_structures::sync::AtomicU64;
use rustc_middle::arena::Arena; use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::{self, DepKindStruct, SerializedDepNodeIndex}; use rustc_middle::dep_graph::{self, DepKindStruct};
use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values}; use rustc_middle::ty::query::{query_keys, query_storage, query_stored, query_values};
use rustc_middle::ty::query::{ExternProviders, Providers, QueryEngine}; use rustc_middle::ty::query::{ExternProviders, Providers, QueryEngine};
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
@ -34,6 +34,7 @@ pub use rustc_query_system::query::{deadlock, QueryContext};
mod keys; mod keys;
use keys::Key; use keys::Key;
use rustc_query_system::dep_graph::SerializedDepNodeIndex;
pub use rustc_query_system::query::QueryConfig; pub use rustc_query_system::query::QueryConfig;
pub(crate) use rustc_query_system::query::{QueryDescription, QueryVTable}; pub(crate) use rustc_query_system::query::{QueryDescription, QueryVTable};

View File

@ -3,6 +3,7 @@
//! manage the caches, and so forth. //! manage the caches, and so forth.
use crate::keys::Key; use crate::keys::Key;
use crate::on_disk_cache::CacheDecoder;
use crate::{on_disk_cache, Queries}; use crate::{on_disk_cache, Queries};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::Lock; use rustc_data_structures::sync::Lock;
@ -19,6 +20,7 @@ use rustc_query_system::query::{
QuerySideEffects, QueryStackFrame, QuerySideEffects, QueryStackFrame,
}; };
use rustc_query_system::Value; use rustc_query_system::Value;
use rustc_serialize::Decodable;
use std::any::Any; use std::any::Any;
use std::num::NonZeroU64; use std::num::NonZeroU64;
use thin_vec::ThinVec; use thin_vec::ThinVec;
@ -253,6 +255,18 @@ macro_rules! get_provider {
}; };
} }
macro_rules! should_ever_cache_on_disk {
([]) => {{
None
}};
([(cache) $($rest:tt)*]) => {{
Some($crate::plumbing::try_load_from_disk::<Self::Value>)
}};
([$other:tt $($modifiers:tt)*]) => {
should_ever_cache_on_disk!([$($modifiers)*])
};
}
pub(crate) fn create_query_frame< pub(crate) fn create_query_frame<
'tcx, 'tcx,
K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>, K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
@ -313,6 +327,16 @@ where
} }
} }
pub(crate) fn try_load_from_disk<'tcx, V>(
tcx: QueryCtxt<'tcx>,
id: SerializedDepNodeIndex,
) -> Option<V>
where
V: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
{
tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id)
}
fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
where where
Q: QueryDescription<QueryCtxt<'tcx>>, Q: QueryDescription<QueryCtxt<'tcx>>,