Skip to content

Commit 279b6df

Browse files
incr.comp.: Refactor query cache serialization to be more re-usable.
1 parent 2c1aedd commit 279b6df

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

src/librustc/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13131313
-> Result<(), E::Error>
13141314
where E: ty::codec::TyEncoder
13151315
{
1316-
self.on_disk_query_result_cache.serialize(self, self.cstore, encoder)
1316+
self.on_disk_query_result_cache.serialize(self.global_tcx(), self.cstore, encoder)
13171317
}
13181318

13191319
}

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use syntax_pos::{BytePos, Span, NO_EXPANSION, DUMMY_SP};
3232
use ty;
3333
use ty::codec::{self as ty_codec, TyDecoder, TyEncoder};
3434
use ty::context::TyCtxt;
35-
use ty::maps::config::QueryDescription;
3635
use ty::subst::Substs;
3736

3837
// Some magic values used for verifying that encoding and decoding. These are
@@ -162,11 +161,11 @@ impl<'sess> OnDiskCache<'sess> {
162161
}
163162
}
164163

165-
pub fn serialize<'a, 'gcx, 'lcx, E>(&self,
166-
tcx: TyCtxt<'a, 'gcx, 'lcx>,
167-
cstore: &CrateStore,
168-
encoder: &mut E)
169-
-> Result<(), E::Error>
164+
pub fn serialize<'a, 'tcx, E>(&self,
165+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
166+
cstore: &CrateStore,
167+
encoder: &mut E)
168+
-> Result<(), E::Error>
170169
where E: ty_codec::TyEncoder
171170
{
172171
// Serializing the DepGraph should not modify it:
@@ -232,19 +231,13 @@ impl<'sess> OnDiskCache<'sess> {
232231
// Encode query results
233232
let mut query_result_index = EncodedQueryResultIndex::new();
234233

235-
// Encode TypeckTables
236-
for (def_id, entry) in tcx.maps.typeck_tables_of.borrow().map.iter() {
237-
if ty::maps::queries::typeck_tables_of::cache_on_disk(*def_id) {
238-
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
234+
{
235+
use ty::maps::queries::*;
236+
let enc = &mut encoder;
237+
let qri = &mut query_result_index;
239238

240-
// Record position of the cache entry
241-
query_result_index.push((dep_node, encoder.position()));
242-
243-
// Encode the type check tables with the SerializedDepNodeIndex
244-
// as tag.
245-
let typeck_tables: &ty::TypeckTables<'gcx> = &entry.value;
246-
encoder.encode_tagged(dep_node, typeck_tables)?;
247-
}
239+
// Encode TypeckTables
240+
encode_query_results::<typeck_tables_of, _>(tcx, enc, qri)?;
248241
}
249242

250243
// Encode query result index
@@ -842,3 +835,27 @@ for CacheDecoder<'a, 'tcx, 'x> {
842835
Ok(IntEncodedWithFixedSize(value))
843836
}
844837
}
838+
839+
fn encode_query_results<'x, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
840+
encoder: &mut CacheEncoder<'x, 'tcx, E>,
841+
query_result_index: &mut EncodedQueryResultIndex)
842+
-> Result<(), E::Error>
843+
where Q: super::plumbing::GetCacheInternal<'tcx>,
844+
E: 'x + TyEncoder,
845+
Q::Value: Encodable,
846+
{
847+
for (key, entry) in Q::get_cache_internal(tcx).map.iter() {
848+
if Q::cache_on_disk(key.clone()) {
849+
let dep_node = SerializedDepNodeIndex::new(entry.index.index());
850+
851+
// Record position of the cache entry
852+
query_result_index.push((dep_node, encoder.position()));
853+
854+
// Encode the type check tables with the SerializedDepNodeIndex
855+
// as tag.
856+
encoder.encode_tagged(dep_node, &entry.value)?;
857+
}
858+
}
859+
860+
Ok(())
861+
}

src/librustc/ty/maps/plumbing.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use ty::maps::config::QueryDescription;
2020
use ty::item_path;
2121

2222
use rustc_data_structures::fx::{FxHashMap};
23-
use std::cell::RefMut;
23+
use std::cell::{Ref, RefMut};
2424
use std::marker::PhantomData;
2525
use std::mem;
2626
use syntax_pos::Span;
@@ -55,6 +55,11 @@ impl<'tcx, M: QueryDescription<'tcx>> QueryMap<'tcx, M> {
5555
}
5656
}
5757

58+
pub(super) trait GetCacheInternal<'tcx>: QueryDescription<'tcx> + Sized {
59+
fn get_cache_internal<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
60+
-> Ref<'a, QueryMap<'tcx, Self>>;
61+
}
62+
5863
pub(super) struct CycleError<'a, 'tcx: 'a> {
5964
span: Span,
6065
cycle: RefMut<'a, [(Span, Query<'tcx>)]>,
@@ -242,6 +247,13 @@ macro_rules! define_maps {
242247
type Value = $V;
243248
}
244249

250+
impl<$tcx> GetCacheInternal<$tcx> for queries::$name<$tcx> {
251+
fn get_cache_internal<'a>(tcx: TyCtxt<'a, $tcx, $tcx>)
252+
-> ::std::cell::Ref<'a, QueryMap<$tcx, Self>> {
253+
tcx.maps.$name.borrow()
254+
}
255+
}
256+
245257
impl<'a, $tcx, 'lcx> queries::$name<$tcx> {
246258

247259
#[allow(unused)]

0 commit comments

Comments
 (0)