Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 73a955e

Browse files
committedAug 31, 2024
Auto merge of #129827 - bvanjoi:less-decoding, r=<try>
perform less decoding if it has the same syntax context Following this [comment](#127279 (comment)) r? `@petrochenkov`
2 parents d571ae8 + 772fe96 commit 73a955e

File tree

6 files changed

+196
-132
lines changed

6 files changed

+196
-132
lines changed
 

‎compiler/rustc_metadata/src/rmeta/decoder.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -468,15 +468,28 @@ impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
468468
};
469469

470470
let cname = cdata.root.name();
471-
rustc_span::hygiene::decode_syntax_context(self, &cdata.hygiene_context, |_, id| {
472-
debug!("SpecializedDecoder<SyntaxContext>: decoding {}", id);
473-
cdata
474-
.root
475-
.syntax_contexts
476-
.get(cdata, id)
477-
.unwrap_or_else(|| panic!("Missing SyntaxContext {id:?} for crate {cname:?}"))
478-
.decode((cdata, sess))
479-
})
471+
rustc_span::hygiene::decode_syntax_context(
472+
self,
473+
&cdata.hygiene_context,
474+
|_, id| {
475+
debug!("SpecializedDecoder<SyntaxContextKey>: decoding {}", id);
476+
cdata
477+
.root
478+
.syntax_context_keys
479+
.get(cdata, id)
480+
.unwrap_or_else(|| panic!("Missing SyntaxContext {id:?} for crate {cname:?}"))
481+
.decode((cdata, sess))
482+
},
483+
|_, id| {
484+
debug!("SpecializedDecoder<SyntaxContextData>: decoding {}", id);
485+
cdata
486+
.root
487+
.syntax_context_data
488+
.get(cdata, id)
489+
.unwrap_or_else(|| panic!("Missing SyntaxContext {id:?} for crate {cname:?}"))
490+
.decode((cdata, sess))
491+
},
492+
)
480493
}
481494

482495
fn decode_expn_id(&mut self) -> ExpnId {

‎compiler/rustc_metadata/src/rmeta/encoder.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
684684
// the incremental cache. If this causes us to deserialize a `Span`, then we may load
685685
// additional `SyntaxContext`s into the global `HygieneData`. Therefore, we need to encode
686686
// the hygiene data last to ensure that we encode any `SyntaxContext`s that might be used.
687-
let (syntax_contexts, expn_data, expn_hashes) = stat!("hygiene", || self.encode_hygiene());
687+
let (syntax_context_keys, syntax_context_data, expn_data, expn_hashes) =
688+
stat!("hygiene", || self.encode_hygiene());
688689

689690
let def_path_hash_map = stat!("def-path-hash-map", || self.encode_def_path_hash_map());
690691

@@ -737,7 +738,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
737738
exported_symbols,
738739
interpret_alloc_index,
739740
tables,
740-
syntax_contexts,
741+
syntax_context_keys,
742+
syntax_context_data,
741743
expn_data,
742744
expn_hashes,
743745
def_path_hash_map,
@@ -1807,17 +1809,27 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18071809
self.lazy_array(foreign_modules.iter().map(|(_, m)| m).cloned())
18081810
}
18091811

1810-
fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) {
1811-
let mut syntax_contexts: TableBuilder<_, _> = Default::default();
1812+
fn encode_hygiene(
1813+
&mut self,
1814+
) -> (SyntaxContextKeyTable, SyntaxContextDataTable, ExpnDataTable, ExpnHashTable) {
1815+
let mut syntax_context_keys: TableBuilder<_, _> = Default::default();
1816+
let mut syntax_context_data: TableBuilder<_, _> = Default::default();
18121817
let mut expn_data_table: TableBuilder<_, _> = Default::default();
18131818
let mut expn_hash_table: TableBuilder<_, _> = Default::default();
18141819

18151820
self.hygiene_ctxt.encode(
1816-
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
1817-
|(this, syntax_contexts, _, _), index, ctxt_data| {
1818-
syntax_contexts.set_some(index, this.lazy(ctxt_data));
1821+
&mut (
1822+
&mut *self,
1823+
&mut syntax_context_keys,
1824+
&mut syntax_context_data,
1825+
&mut expn_data_table,
1826+
&mut expn_hash_table,
1827+
),
1828+
|(this, syntax_context_keys, syntax_context_data, _, _), index, ctxt_data| {
1829+
syntax_context_keys.set_some(index, this.lazy(&ctxt_data.0));
1830+
syntax_context_data.set_some(index, this.lazy(&ctxt_data.1));
18191831
},
1820-
|(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
1832+
|(this, _, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
18211833
if let Some(index) = index.as_local() {
18221834
expn_data_table.set_some(index.as_raw(), this.lazy(expn_data));
18231835
expn_hash_table.set_some(index.as_raw(), this.lazy(hash));
@@ -1826,7 +1838,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18261838
);
18271839

18281840
(
1829-
syntax_contexts.encode(&mut self.opaque),
1841+
syntax_context_keys.encode(&mut self.opaque),
1842+
syntax_context_data.encode(&mut self.opaque),
18301843
expn_data_table.encode(&mut self.opaque),
18311844
expn_hash_table.encode(&mut self.opaque),
18321845
)

‎compiler/rustc_metadata/src/rmeta/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_serialize::opaque::FileEncoder;
3434
use rustc_session::config::SymbolManglingVersion;
3535
use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
3636
use rustc_span::edition::Edition;
37-
use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextData};
37+
use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextData, SyntaxContextKey};
3838
use rustc_span::symbol::{Ident, Symbol};
3939
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Span};
4040
use rustc_target::abi::{FieldIdx, VariantIdx};
@@ -193,7 +193,8 @@ enum LazyState {
193193
Previous(NonZero<usize>),
194194
}
195195

196-
type SyntaxContextTable = LazyTable<u32, Option<LazyValue<SyntaxContextData>>>;
196+
type SyntaxContextKeyTable = LazyTable<u32, Option<LazyValue<SyntaxContextKey>>>;
197+
type SyntaxContextDataTable = LazyTable<u32, Option<LazyValue<SyntaxContextData>>>;
197198
type ExpnDataTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnData>>>;
198199
type ExpnHashTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnHash>>>;
199200

@@ -276,7 +277,8 @@ pub(crate) struct CrateRoot {
276277

277278
exported_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
278279

279-
syntax_contexts: SyntaxContextTable,
280+
syntax_context_keys: SyntaxContextKeyTable,
281+
syntax_context_data: SyntaxContextDataTable,
280282
expn_data: ExpnDataTable,
281283
expn_hashes: ExpnHashTable,
282284

‎compiler/rustc_middle/src/query/on_disk_cache.rs

+46-22
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
2121
use rustc_session::Session;
2222
use rustc_span::hygiene::{
2323
ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextData,
24+
SyntaxContextKey,
2425
};
2526
use rustc_span::source_map::SourceMap;
2627
use rustc_span::{
@@ -36,8 +37,9 @@ const TAG_FULL_SPAN: u8 = 0;
3637
const TAG_PARTIAL_SPAN: u8 = 1;
3738
const TAG_RELATIVE_SPAN: u8 = 2;
3839

39-
const TAG_SYNTAX_CONTEXT: u8 = 0;
40-
const TAG_EXPN_DATA: u8 = 1;
40+
const TAG_SYNTAX_CONTEXT_KEY: u8 = 0;
41+
const TAG_SYNTAX_CONTEXT_DATA: u8 = 1;
42+
const TAG_EXPN_DATA: u8 = 2;
4143

4244
// Tags for encoding Symbol's
4345
const SYMBOL_STR: u8 = 0;
@@ -77,7 +79,8 @@ pub struct OnDiskCache<'sess> {
7779
// to represent the fact that we are storing *encoded* ids. When we decode
7880
// a `SyntaxContext`, a new id will be allocated from the global `HygieneData`,
7981
// which will almost certainly be different than the serialized id.
80-
syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
82+
syntax_context_keys: FxHashMap<u32, AbsoluteBytePos>,
83+
syntax_context_data: FxHashMap<u32, AbsoluteBytePos>,
8184
// A map from the `DefPathHash` of an `ExpnId` to the position
8285
// of their associated `ExpnData`. Ideally, we would store a `DefId`,
8386
// but we need to decode this before we've constructed a `TyCtxt` (which
@@ -108,7 +111,8 @@ struct Footer {
108111
// without measurable overhead. This permits larger const allocations without ICEing.
109112
interpret_alloc_index: Vec<u64>,
110113
// See `OnDiskCache.syntax_contexts`
111-
syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
114+
syntax_context_keys: FxHashMap<u32, AbsoluteBytePos>,
115+
syntax_context_data: FxHashMap<u32, AbsoluteBytePos>,
112116
// See `OnDiskCache.expn_data`
113117
expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
114118
foreign_expn_data: UnhashMap<ExpnHash, u32>,
@@ -179,7 +183,8 @@ impl<'sess> OnDiskCache<'sess> {
179183
query_result_index: footer.query_result_index.into_iter().collect(),
180184
prev_side_effects_index: footer.side_effects_index.into_iter().collect(),
181185
alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
182-
syntax_contexts: footer.syntax_contexts,
186+
syntax_context_keys: footer.syntax_context_keys,
187+
syntax_context_data: footer.syntax_context_data,
183188
expn_data: footer.expn_data,
184189
foreign_expn_data: footer.foreign_expn_data,
185190
hygiene_context: Default::default(),
@@ -196,7 +201,8 @@ impl<'sess> OnDiskCache<'sess> {
196201
query_result_index: Default::default(),
197202
prev_side_effects_index: Default::default(),
198203
alloc_decoding_state: AllocDecodingState::new(Vec::new()),
199-
syntax_contexts: FxHashMap::default(),
204+
syntax_context_keys: FxHashMap::default(),
205+
syntax_context_data: FxHashMap::default(),
200206
expn_data: UnhashMap::default(),
201207
foreign_expn_data: UnhashMap::default(),
202208
hygiene_context: Default::default(),
@@ -301,7 +307,8 @@ impl<'sess> OnDiskCache<'sess> {
301307
interpret_alloc_index
302308
};
303309

304-
let mut syntax_contexts = FxHashMap::default();
310+
let mut syntax_context_keys = FxHashMap::default();
311+
let mut syntax_context_data = FxHashMap::default();
305312
let mut expn_data = UnhashMap::default();
306313
let mut foreign_expn_data = UnhashMap::default();
307314

@@ -312,8 +319,12 @@ impl<'sess> OnDiskCache<'sess> {
312319
&mut encoder,
313320
|encoder, index, ctxt_data| {
314321
let pos = AbsoluteBytePos::new(encoder.position());
315-
encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data);
316-
syntax_contexts.insert(index, pos);
322+
encoder.encode_tagged(TAG_SYNTAX_CONTEXT_KEY, &ctxt_data.0);
323+
syntax_context_keys.insert(index, pos);
324+
325+
let pos = AbsoluteBytePos::new(encoder.position());
326+
encoder.encode_tagged(TAG_SYNTAX_CONTEXT_DATA, &ctxt_data.1);
327+
syntax_context_data.insert(index, pos);
317328
},
318329
|encoder, expn_id, data, hash| {
319330
if expn_id.krate == LOCAL_CRATE {
@@ -335,7 +346,8 @@ impl<'sess> OnDiskCache<'sess> {
335346
query_result_index,
336347
side_effects_index,
337348
interpret_alloc_index,
338-
syntax_contexts,
349+
syntax_context_keys,
350+
syntax_context_data,
339351
expn_data,
340352
foreign_expn_data,
341353
},
@@ -442,7 +454,8 @@ impl<'sess> OnDiskCache<'sess> {
442454
file_index_to_file: &self.file_index_to_file,
443455
file_index_to_stable_id: &self.file_index_to_stable_id,
444456
alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
445-
syntax_contexts: &self.syntax_contexts,
457+
syntax_context_keys: &self.syntax_context_keys,
458+
syntax_context_data: &self.syntax_context_data,
446459
expn_data: &self.expn_data,
447460
foreign_expn_data: &self.foreign_expn_data,
448461
hygiene_context: &self.hygiene_context,
@@ -463,7 +476,8 @@ pub struct CacheDecoder<'a, 'tcx> {
463476
file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
464477
file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
465478
alloc_decoding_session: AllocDecodingSession<'a>,
466-
syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
479+
syntax_context_keys: &'a FxHashMap<u32, AbsoluteBytePos>,
480+
syntax_context_data: &'a FxHashMap<u32, AbsoluteBytePos>,
467481
expn_data: &'a UnhashMap<ExpnHash, AbsoluteBytePos>,
468482
foreign_expn_data: &'a UnhashMap<ExpnHash, u32>,
469483
hygiene_context: &'a HygieneDecodeContext,
@@ -584,16 +598,26 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
584598

585599
impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
586600
fn decode_syntax_context(&mut self) -> SyntaxContext {
587-
let syntax_contexts = self.syntax_contexts;
588-
rustc_span::hygiene::decode_syntax_context(self, self.hygiene_context, |this, id| {
589-
// This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
590-
// We look up the position of the associated `SyntaxData` and decode it.
591-
let pos = syntax_contexts.get(&id).unwrap();
592-
this.with_position(pos.to_usize(), |decoder| {
593-
let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
594-
data
595-
})
596-
})
601+
rustc_span::hygiene::decode_syntax_context(
602+
self,
603+
self.hygiene_context,
604+
|this, id| {
605+
// This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
606+
// We look up the position of the associated `SyntaxData` and decode it.
607+
let pos = this.syntax_context_keys.get(&id).unwrap();
608+
this.with_position(pos.to_usize(), |decoder| {
609+
let data: SyntaxContextKey = decode_tagged(decoder, TAG_SYNTAX_CONTEXT_KEY);
610+
data
611+
})
612+
},
613+
|this, id| {
614+
let pos = this.syntax_context_data.get(&id).unwrap();
615+
this.with_position(pos.to_usize(), |decoder| {
616+
let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT_DATA);
617+
data
618+
})
619+
},
620+
)
597621
}
598622

599623
fn decode_expn_id(&mut self) -> ExpnId {

‎compiler/rustc_middle/src/ty/parameterized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ trivially_parameterized_over_tcx! {
107107
rustc_span::Span,
108108
rustc_span::Symbol,
109109
rustc_span::def_id::DefPathHash,
110+
rustc_span::hygiene::SyntaxContextKey,
110111
rustc_span::hygiene::SyntaxContextData,
111112
rustc_span::symbol::Ident,
112113
rustc_type_ir::Variance,
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.