incr.comp.: Initialize the CachingCodemapView in StableHashingContext lazily.

This commit is contained in:
Michael Woerister 2017-09-14 12:08:03 +02:00
parent 67c84e05e7
commit dd501735ac
2 changed files with 21 additions and 9 deletions

View File

@ -11,7 +11,6 @@
use std::rc::Rc; use std::rc::Rc;
use syntax::codemap::CodeMap; use syntax::codemap::CodeMap;
use syntax_pos::{BytePos, FileMap}; use syntax_pos::{BytePos, FileMap};
use ty::TyCtxt;
#[derive(Clone)] #[derive(Clone)]
struct CacheEntry { struct CacheEntry {
@ -23,15 +22,14 @@ struct CacheEntry {
file_index: usize, file_index: usize,
} }
pub struct CachingCodemapView<'tcx> { pub struct CachingCodemapView<'cm> {
codemap: &'tcx CodeMap, codemap: &'cm CodeMap,
line_cache: [CacheEntry; 3], line_cache: [CacheEntry; 3],
time_stamp: usize, time_stamp: usize,
} }
impl<'gcx> CachingCodemapView<'gcx> { impl<'cm> CachingCodemapView<'cm> {
pub fn new<'a, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> CachingCodemapView<'gcx> { pub fn new(codemap: &'cm CodeMap) -> CachingCodemapView<'cm> {
let codemap = tcx.sess.codemap();
let files = codemap.files(); let files = codemap.files();
let first_file = files[0].clone(); let first_file = files[0].clone();
let entry = CacheEntry { let entry = CacheEntry {

View File

@ -21,6 +21,7 @@ use std::collections::HashMap;
use syntax::ast; use syntax::ast;
use syntax::attr; use syntax::attr;
use syntax::codemap::CodeMap;
use syntax::ext::hygiene::SyntaxContext; use syntax::ext::hygiene::SyntaxContext;
use syntax::symbol::Symbol; use syntax::symbol::Symbol;
use syntax_pos::Span; use syntax_pos::Span;
@ -36,13 +37,17 @@ use rustc_data_structures::accumulate_vec::AccumulateVec;
/// things (e.g. each DefId/DefPath is only hashed once). /// things (e.g. each DefId/DefPath is only hashed once).
pub struct StableHashingContext<'a, 'gcx: 'a+'tcx, 'tcx: 'a> { pub struct StableHashingContext<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
tcx: TyCtxt<'a, 'gcx, 'tcx>, tcx: TyCtxt<'a, 'gcx, 'tcx>,
codemap: CachingCodemapView<'gcx>,
hash_spans: bool, hash_spans: bool,
hash_bodies: bool, hash_bodies: bool,
overflow_checks_enabled: bool, overflow_checks_enabled: bool,
node_id_hashing_mode: NodeIdHashingMode, node_id_hashing_mode: NodeIdHashingMode,
// A sorted array of symbol keys for fast lookup. // A sorted array of symbol keys for fast lookup.
ignored_attr_names: Vec<Symbol>, ignored_attr_names: Vec<Symbol>,
// Very often, we are hashing something that does not need the
// CachingCodemapView, so we initialize it lazily.
raw_codemap: &'gcx CodeMap,
caching_codemap: Option<CachingCodemapView<'gcx>>,
} }
#[derive(PartialEq, Eq, Clone, Copy)] #[derive(PartialEq, Eq, Clone, Copy)]
@ -66,7 +71,8 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
StableHashingContext { StableHashingContext {
tcx, tcx,
codemap: CachingCodemapView::new(tcx), caching_codemap: None,
raw_codemap: tcx.sess.codemap(),
hash_spans: hash_spans_initial, hash_spans: hash_spans_initial,
hash_bodies: true, hash_bodies: true,
overflow_checks_enabled: check_overflow_initial, overflow_checks_enabled: check_overflow_initial,
@ -132,7 +138,15 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
#[inline] #[inline]
pub fn codemap(&mut self) -> &mut CachingCodemapView<'gcx> { pub fn codemap(&mut self) -> &mut CachingCodemapView<'gcx> {
&mut self.codemap match self.caching_codemap {
Some(ref mut cm) => {
cm
}
ref mut none => {
*none = Some(CachingCodemapView::new(self.raw_codemap));
none.as_mut().unwrap()
}
}
} }
#[inline] #[inline]