Skip to content

Commit

Permalink
WIP: Cache more queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Dec 4, 2017
1 parent dbcbc37 commit 4c448b8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
78 changes: 77 additions & 1 deletion src/librustc/ty/maps/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ pub trait QueryConfig {
pub(super) trait QueryDescription<'tcx>: QueryConfig {
fn describe(tcx: TyCtxt, key: Self::Key) -> String;

#[inline]
fn cache_on_disk(_: Self::Key) -> bool {
false
}

fn try_load_from_disk(_: TyCtxt<'_, 'tcx, 'tcx>,
_: SerializedDepNodeIndex)
-> Option<Self::Value> {
bug!("QueryDescription::load_from_disk() called for unsupport query.")
bug!("QueryDescription::load_from_disk() called for an unsupported query.")
}
}

Expand Down Expand Up @@ -166,6 +167,18 @@ impl<'tcx> QueryDescription<'tcx> for queries::symbol_name<'tcx> {
fn describe(_tcx: TyCtxt, instance: ty::Instance<'tcx>) -> String {
format!("computing the symbol for `{}`", instance)
}

#[inline]
fn cache_on_disk(_: Self::Key) -> bool {
true
}

#[inline]
fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: SerializedDepNodeIndex)
-> Option<Self::Value> {
tcx.on_disk_query_result_cache.try_load_query_result(tcx, id)
}
}

impl<'tcx> QueryDescription<'tcx> for queries::describe_def<'tcx> {
Expand Down Expand Up @@ -234,6 +247,18 @@ impl<'tcx> QueryDescription<'tcx> for queries::const_is_rvalue_promotable_to_sta
format!("const checking if rvalue is promotable to static `{}`",
tcx.item_path_str(def_id))
}

#[inline]
fn cache_on_disk(_: Self::Key) -> bool {
true
}

#[inline]
fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: SerializedDepNodeIndex)
-> Option<Self::Value> {
tcx.on_disk_query_result_cache.try_load_query_result(tcx, id)
}
}

impl<'tcx> QueryDescription<'tcx> for queries::rvalue_promotable_map<'tcx> {
Expand All @@ -254,6 +279,18 @@ impl<'tcx> QueryDescription<'tcx> for queries::trans_fulfill_obligation<'tcx> {
fn describe(tcx: TyCtxt, key: (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>)) -> String {
format!("checking if `{}` fulfills its obligations", tcx.item_path_str(key.1.def_id()))
}

#[inline]
fn cache_on_disk(_: Self::Key) -> bool {
true
}

#[inline]
fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: SerializedDepNodeIndex)
-> Option<Self::Value> {
tcx.on_disk_query_result_cache.try_load_query_result(tcx, id)
}
}

impl<'tcx> QueryDescription<'tcx> for queries::trait_impls_of<'tcx> {
Expand Down Expand Up @@ -567,3 +604,42 @@ impl<'tcx> QueryDescription<'tcx> for queries::typeck_tables_of<'tcx> {
}
}

impl<'tcx> QueryDescription<'tcx> for queries::optimized_mir<'tcx> {
#[inline]
fn cache_on_disk(def_id: Self::Key) -> bool {
def_id.is_local()
}

fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: SerializedDepNodeIndex)
-> Option<Self::Value> {
let mir: Option<::mir::Mir<'tcx>> = tcx.on_disk_query_result_cache
.try_load_query_result(tcx, id);
mir.map(|x| tcx.alloc_mir(x))
}
}

macro_rules! impl_disk_cacheable_query(
($query_name:ident, |$key:tt| $cond:expr) => {
impl<'tcx> QueryDescription<'tcx> for queries::$query_name<'tcx> {
#[inline]
fn cache_on_disk($key: Self::Key) -> bool {
$cond
}

#[inline]
fn try_load_from_disk<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
id: SerializedDepNodeIndex)
-> Option<Self::Value> {
tcx.on_disk_query_result_cache.try_load_query_result(tcx, id)
}
}
}
);

impl_disk_cacheable_query!(unsafety_check_result, |def_id| def_id.is_local());
impl_disk_cacheable_query!(borrowck, |def_id| def_id.is_local());
impl_disk_cacheable_query!(mir_borrowck, |def_id| def_id.is_local());
impl_disk_cacheable_query!(mir_const_qualif, |def_id| def_id.is_local());
impl_disk_cacheable_query!(contains_extern_indicator, |_| true);
impl_disk_cacheable_query!(def_symbol_name, |_| true);
10 changes: 10 additions & 0 deletions src/librustc/ty/maps/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ impl<'sess> OnDiskCache<'sess> {

// Encode TypeckTables
encode_query_results::<typeck_tables_of, _>(tcx, enc, qri)?;
encode_query_results::<optimized_mir, _>(tcx, enc, qri)?;
encode_query_results::<unsafety_check_result, _>(tcx, enc, qri)?;
encode_query_results::<borrowck, _>(tcx, enc, qri)?;
encode_query_results::<mir_borrowck, _>(tcx, enc, qri)?;
encode_query_results::<mir_const_qualif, _>(tcx, enc, qri)?;
encode_query_results::<def_symbol_name, _>(tcx, enc, qri)?;
encode_query_results::<const_is_rvalue_promotable_to_static, _>(tcx, enc, qri)?;
encode_query_results::<contains_extern_indicator, _>(tcx, enc, qri)?;
encode_query_results::<symbol_name, _>(tcx, enc, qri)?;
encode_query_results::<trans_fulfill_obligation, _>(tcx, enc, qri)?;
}

// Encode diagnostics
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/ty/maps/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -974,4 +974,7 @@ impl_load_from_cache!(
BorrowCheck => borrowck,
MirBorrowCheck => mir_borrowck,
MirConstQualif => mir_const_qualif,
SymbolName => def_symbol_name,
ConstIsRvaluePromotableToStatic => const_is_rvalue_promotable_to_static,
ContainsExternIndicator => contains_extern_indicator,
);

0 comments on commit 4c448b8

Please sign in to comment.