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 6fcc18a

Browse files
committedSep 10, 2024
perform less decoding if it has the same syntax context
1 parent f827364 commit 6fcc18a

File tree

6 files changed

+142
-96
lines changed

6 files changed

+142
-96
lines changed
 

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,10 @@ impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> {
469469

470470
let cname = cdata.root.name();
471471
rustc_span::hygiene::decode_syntax_context(self, &cdata.hygiene_context, |_, id| {
472-
debug!("SpecializedDecoder<SyntaxContext>: decoding {}", id);
472+
debug!("SpecializedDecoder<SyntaxContextKey>: decoding {}", id);
473473
cdata
474474
.root
475-
.syntax_contexts
475+
.syntax_context_keys
476476
.get(cdata, id)
477477
.unwrap_or_else(|| panic!("Missing SyntaxContext {id:?} for crate {cname:?}"))
478478
.decode((cdata, sess))

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

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

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

@@ -738,7 +739,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
738739
exported_symbols,
739740
interpret_alloc_index,
740741
tables,
741-
syntax_contexts,
742+
syntax_context_keys,
742743
expn_data,
743744
expn_hashes,
744745
def_path_hash_map,
@@ -1810,15 +1811,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18101811
self.lazy_array(foreign_modules.iter().map(|(_, m)| m).cloned())
18111812
}
18121813

1813-
fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) {
1814-
let mut syntax_contexts: TableBuilder<_, _> = Default::default();
1814+
fn encode_hygiene(&mut self) -> (SyntaxContextKeyTable, ExpnDataTable, ExpnHashTable) {
1815+
let mut syntax_context_keys: TableBuilder<_, _> = Default::default();
18151816
let mut expn_data_table: TableBuilder<_, _> = Default::default();
18161817
let mut expn_hash_table: TableBuilder<_, _> = Default::default();
18171818

18181819
self.hygiene_ctxt.encode(
1819-
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
1820-
|(this, syntax_contexts, _, _), index, ctxt_data| {
1821-
syntax_contexts.set_some(index, this.lazy(ctxt_data));
1820+
&mut (&mut *self, &mut syntax_context_keys, &mut expn_data_table, &mut expn_hash_table),
1821+
|(this, syntax_context_keys, _, _), index, ctxt_data| {
1822+
syntax_context_keys.set_some(index, this.lazy(ctxt_data));
18221823
},
18231824
|(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
18241825
if let Some(index) = index.as_local() {
@@ -1829,7 +1830,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18291830
);
18301831

18311832
(
1832-
syntax_contexts.encode(&mut self.opaque),
1833+
syntax_context_keys.encode(&mut self.opaque),
18331834
expn_data_table.encode(&mut self.opaque),
18341835
expn_hash_table.encode(&mut self.opaque),
18351836
)

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

+3-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, 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,7 @@ enum LazyState {
193193
Previous(NonZero<usize>),
194194
}
195195

196-
type SyntaxContextTable = LazyTable<u32, Option<LazyValue<SyntaxContextData>>>;
196+
type SyntaxContextKeyTable = LazyTable<u32, Option<LazyValue<SyntaxContextKey>>>;
197197
type ExpnDataTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnData>>>;
198198
type ExpnHashTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnHash>>>;
199199

@@ -276,7 +276,7 @@ pub(crate) struct CrateRoot {
276276

277277
exported_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
278278

279-
syntax_contexts: SyntaxContextTable,
279+
syntax_context_keys: SyntaxContextKeyTable,
280280
expn_data: ExpnDataTable,
281281
expn_hashes: ExpnHashTable,
282282

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

+14-15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixed
2020
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
2121
use rustc_session::Session;
2222
use rustc_span::hygiene::{
23-
ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextData,
23+
ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextKey,
2424
};
2525
use rustc_span::source_map::SourceMap;
2626
use rustc_span::{
@@ -36,7 +36,7 @@ const TAG_FULL_SPAN: u8 = 0;
3636
const TAG_PARTIAL_SPAN: u8 = 1;
3737
const TAG_RELATIVE_SPAN: u8 = 2;
3838

39-
const TAG_SYNTAX_CONTEXT: u8 = 0;
39+
const TAG_SYNTAX_CONTEXT_KEY: u8 = 0;
4040
const TAG_EXPN_DATA: u8 = 1;
4141

4242
// Tags for encoding Symbol's
@@ -77,7 +77,7 @@ pub struct OnDiskCache<'sess> {
7777
// to represent the fact that we are storing *encoded* ids. When we decode
7878
// a `SyntaxContext`, a new id will be allocated from the global `HygieneData`,
7979
// which will almost certainly be different than the serialized id.
80-
syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
80+
syntax_context_keys: FxHashMap<u32, AbsoluteBytePos>,
8181
// A map from the `DefPathHash` of an `ExpnId` to the position
8282
// of their associated `ExpnData`. Ideally, we would store a `DefId`,
8383
// but we need to decode this before we've constructed a `TyCtxt` (which
@@ -108,7 +108,7 @@ struct Footer {
108108
// without measurable overhead. This permits larger const allocations without ICEing.
109109
interpret_alloc_index: Vec<u64>,
110110
// See `OnDiskCache.syntax_contexts`
111-
syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
111+
syntax_context_keys: FxHashMap<u32, AbsoluteBytePos>,
112112
// See `OnDiskCache.expn_data`
113113
expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
114114
foreign_expn_data: UnhashMap<ExpnHash, u32>,
@@ -179,7 +179,7 @@ impl<'sess> OnDiskCache<'sess> {
179179
query_result_index: footer.query_result_index.into_iter().collect(),
180180
prev_side_effects_index: footer.side_effects_index.into_iter().collect(),
181181
alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
182-
syntax_contexts: footer.syntax_contexts,
182+
syntax_context_keys: footer.syntax_context_keys,
183183
expn_data: footer.expn_data,
184184
foreign_expn_data: footer.foreign_expn_data,
185185
hygiene_context: Default::default(),
@@ -196,7 +196,7 @@ impl<'sess> OnDiskCache<'sess> {
196196
query_result_index: Default::default(),
197197
prev_side_effects_index: Default::default(),
198198
alloc_decoding_state: AllocDecodingState::new(Vec::new()),
199-
syntax_contexts: FxHashMap::default(),
199+
syntax_context_keys: FxHashMap::default(),
200200
expn_data: UnhashMap::default(),
201201
foreign_expn_data: UnhashMap::default(),
202202
hygiene_context: Default::default(),
@@ -301,7 +301,7 @@ impl<'sess> OnDiskCache<'sess> {
301301
interpret_alloc_index
302302
};
303303

304-
let mut syntax_contexts = FxHashMap::default();
304+
let mut syntax_context_keys = FxHashMap::default();
305305
let mut expn_data = UnhashMap::default();
306306
let mut foreign_expn_data = UnhashMap::default();
307307

@@ -312,8 +312,8 @@ impl<'sess> OnDiskCache<'sess> {
312312
&mut encoder,
313313
|encoder, index, ctxt_data| {
314314
let pos = AbsoluteBytePos::new(encoder.position());
315-
encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data);
316-
syntax_contexts.insert(index, pos);
315+
encoder.encode_tagged(TAG_SYNTAX_CONTEXT_KEY, &ctxt_data);
316+
syntax_context_keys.insert(index, pos);
317317
},
318318
|encoder, expn_id, data, hash| {
319319
if expn_id.krate == LOCAL_CRATE {
@@ -335,7 +335,7 @@ impl<'sess> OnDiskCache<'sess> {
335335
query_result_index,
336336
side_effects_index,
337337
interpret_alloc_index,
338-
syntax_contexts,
338+
syntax_context_keys,
339339
expn_data,
340340
foreign_expn_data,
341341
},
@@ -442,7 +442,7 @@ impl<'sess> OnDiskCache<'sess> {
442442
file_index_to_file: &self.file_index_to_file,
443443
file_index_to_stable_id: &self.file_index_to_stable_id,
444444
alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
445-
syntax_contexts: &self.syntax_contexts,
445+
syntax_context_keys: &self.syntax_context_keys,
446446
expn_data: &self.expn_data,
447447
foreign_expn_data: &self.foreign_expn_data,
448448
hygiene_context: &self.hygiene_context,
@@ -463,7 +463,7 @@ pub struct CacheDecoder<'a, 'tcx> {
463463
file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Lrc<SourceFile>>>,
464464
file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
465465
alloc_decoding_session: AllocDecodingSession<'a>,
466-
syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
466+
syntax_context_keys: &'a FxHashMap<u32, AbsoluteBytePos>,
467467
expn_data: &'a UnhashMap<ExpnHash, AbsoluteBytePos>,
468468
foreign_expn_data: &'a UnhashMap<ExpnHash, u32>,
469469
hygiene_context: &'a HygieneDecodeContext,
@@ -584,13 +584,12 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
584584

585585
impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
586586
fn decode_syntax_context(&mut self) -> SyntaxContext {
587-
let syntax_contexts = self.syntax_contexts;
588587
rustc_span::hygiene::decode_syntax_context(self, self.hygiene_context, |this, id| {
589588
// This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
590589
// We look up the position of the associated `SyntaxData` and decode it.
591-
let pos = syntax_contexts.get(&id).unwrap();
590+
let pos = this.syntax_context_keys.get(&id).unwrap();
592591
this.with_position(pos.to_usize(), |decoder| {
593-
let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
592+
let data: SyntaxContextKey = decode_tagged(decoder, TAG_SYNTAX_CONTEXT_KEY);
594593
data
595594
})
596595
})

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

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

0 commit comments

Comments
 (0)
Failed to load comments.