@@ -21,6 +21,7 @@ use std::collections::HashMap;
21
21
22
22
use syntax:: ast;
23
23
use syntax:: attr;
24
+ use syntax:: codemap:: CodeMap ;
24
25
use syntax:: ext:: hygiene:: SyntaxContext ;
25
26
use syntax:: symbol:: Symbol ;
26
27
use syntax_pos:: Span ;
@@ -36,13 +37,17 @@ use rustc_data_structures::accumulate_vec::AccumulateVec;
36
37
/// things (e.g. each DefId/DefPath is only hashed once).
37
38
pub struct StableHashingContext < ' a , ' gcx : ' a +' tcx , ' tcx : ' a > {
38
39
tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
39
- codemap : CachingCodemapView < ' gcx > ,
40
40
hash_spans : bool ,
41
41
hash_bodies : bool ,
42
42
overflow_checks_enabled : bool ,
43
43
node_id_hashing_mode : NodeIdHashingMode ,
44
44
// A sorted array of symbol keys for fast lookup.
45
45
ignored_attr_names : Vec < Symbol > ,
46
+
47
+ // Very often, we are hashing something that does not need the
48
+ // CachingCodemapView, so we initialize it lazily.
49
+ raw_codemap : & ' gcx CodeMap ,
50
+ caching_codemap : Option < CachingCodemapView < ' gcx > > ,
46
51
}
47
52
48
53
#[ derive( PartialEq , Eq , Clone , Copy ) ]
@@ -66,7 +71,8 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
66
71
67
72
StableHashingContext {
68
73
tcx,
69
- codemap : CachingCodemapView :: new ( tcx) ,
74
+ caching_codemap : None ,
75
+ raw_codemap : tcx. sess . codemap ( ) ,
70
76
hash_spans : hash_spans_initial,
71
77
hash_bodies : true ,
72
78
overflow_checks_enabled : check_overflow_initial,
@@ -132,7 +138,15 @@ impl<'a, 'gcx, 'tcx> StableHashingContext<'a, 'gcx, 'tcx> {
132
138
133
139
#[ inline]
134
140
pub fn codemap ( & mut self ) -> & mut CachingCodemapView < ' gcx > {
135
- & mut self . codemap
141
+ match self . caching_codemap {
142
+ Some ( ref mut cm) => {
143
+ cm
144
+ }
145
+ ref mut none => {
146
+ * none = Some ( CachingCodemapView :: new ( self . raw_codemap ) ) ;
147
+ none. as_mut ( ) . unwrap ( )
148
+ }
149
+ }
136
150
}
137
151
138
152
#[ inline]
0 commit comments