From 279b6df1f2355b0b9af5993f38fd9524d74c320d Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Wed, 15 Nov 2017 14:18:00 +0100 Subject: [PATCH] incr.comp.: Refactor query cache serialization to be more re-usable. --- src/librustc/ty/context.rs | 2 +- src/librustc/ty/maps/on_disk_cache.rs | 53 ++++++++++++++++++--------- src/librustc/ty/maps/plumbing.rs | 14 ++++++- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 512860a02b0..1395f37722e 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -1313,7 +1313,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { -> Result<(), E::Error> where E: ty::codec::TyEncoder { - self.on_disk_query_result_cache.serialize(self, self.cstore, encoder) + self.on_disk_query_result_cache.serialize(self.global_tcx(), self.cstore, encoder) } } diff --git a/src/librustc/ty/maps/on_disk_cache.rs b/src/librustc/ty/maps/on_disk_cache.rs index d325d1437fc..98727b5d10d 100644 --- a/src/librustc/ty/maps/on_disk_cache.rs +++ b/src/librustc/ty/maps/on_disk_cache.rs @@ -32,7 +32,6 @@ use syntax_pos::{BytePos, Span, NO_EXPANSION, DUMMY_SP}; use ty; use ty::codec::{self as ty_codec, TyDecoder, TyEncoder}; use ty::context::TyCtxt; -use ty::maps::config::QueryDescription; use ty::subst::Substs; // Some magic values used for verifying that encoding and decoding. These are @@ -162,11 +161,11 @@ impl<'sess> OnDiskCache<'sess> { } } - pub fn serialize<'a, 'gcx, 'lcx, E>(&self, - tcx: TyCtxt<'a, 'gcx, 'lcx>, - cstore: &CrateStore, - encoder: &mut E) - -> Result<(), E::Error> + pub fn serialize<'a, 'tcx, E>(&self, + tcx: TyCtxt<'a, 'tcx, 'tcx>, + cstore: &CrateStore, + encoder: &mut E) + -> Result<(), E::Error> where E: ty_codec::TyEncoder { // Serializing the DepGraph should not modify it: @@ -232,19 +231,13 @@ impl<'sess> OnDiskCache<'sess> { // Encode query results let mut query_result_index = EncodedQueryResultIndex::new(); - // Encode TypeckTables - for (def_id, entry) in tcx.maps.typeck_tables_of.borrow().map.iter() { - if ty::maps::queries::typeck_tables_of::cache_on_disk(*def_id) { - let dep_node = SerializedDepNodeIndex::new(entry.index.index()); + { + use ty::maps::queries::*; + let enc = &mut encoder; + let qri = &mut query_result_index; - // Record position of the cache entry - query_result_index.push((dep_node, encoder.position())); - - // Encode the type check tables with the SerializedDepNodeIndex - // as tag. - let typeck_tables: &ty::TypeckTables<'gcx> = &entry.value; - encoder.encode_tagged(dep_node, typeck_tables)?; - } + // Encode TypeckTables + encode_query_results::(tcx, enc, qri)?; } // Encode query result index @@ -842,3 +835,27 @@ for CacheDecoder<'a, 'tcx, 'x> { Ok(IntEncodedWithFixedSize(value)) } } + +fn encode_query_results<'x, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>, + encoder: &mut CacheEncoder<'x, 'tcx, E>, + query_result_index: &mut EncodedQueryResultIndex) + -> Result<(), E::Error> + where Q: super::plumbing::GetCacheInternal<'tcx>, + E: 'x + TyEncoder, + Q::Value: Encodable, +{ + for (key, entry) in Q::get_cache_internal(tcx).map.iter() { + if Q::cache_on_disk(key.clone()) { + let dep_node = SerializedDepNodeIndex::new(entry.index.index()); + + // Record position of the cache entry + query_result_index.push((dep_node, encoder.position())); + + // Encode the type check tables with the SerializedDepNodeIndex + // as tag. + encoder.encode_tagged(dep_node, &entry.value)?; + } + } + + Ok(()) +} diff --git a/src/librustc/ty/maps/plumbing.rs b/src/librustc/ty/maps/plumbing.rs index 2f8f724edad..1ca8fc6eb48 100644 --- a/src/librustc/ty/maps/plumbing.rs +++ b/src/librustc/ty/maps/plumbing.rs @@ -20,7 +20,7 @@ use ty::maps::config::QueryDescription; use ty::item_path; use rustc_data_structures::fx::{FxHashMap}; -use std::cell::RefMut; +use std::cell::{Ref, RefMut}; use std::marker::PhantomData; use std::mem; use syntax_pos::Span; @@ -55,6 +55,11 @@ impl<'tcx, M: QueryDescription<'tcx>> QueryMap<'tcx, M> { } } +pub(super) trait GetCacheInternal<'tcx>: QueryDescription<'tcx> + Sized { + fn get_cache_internal<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) + -> Ref<'a, QueryMap<'tcx, Self>>; +} + pub(super) struct CycleError<'a, 'tcx: 'a> { span: Span, cycle: RefMut<'a, [(Span, Query<'tcx>)]>, @@ -242,6 +247,13 @@ macro_rules! define_maps { type Value = $V; } + impl<$tcx> GetCacheInternal<$tcx> for queries::$name<$tcx> { + fn get_cache_internal<'a>(tcx: TyCtxt<'a, $tcx, $tcx>) + -> ::std::cell::Ref<'a, QueryMap<$tcx, Self>> { + tcx.maps.$name.borrow() + } + } + impl<'a, $tcx, 'lcx> queries::$name<$tcx> { #[allow(unused)]