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 1c6009a

Browse files
committedMar 16, 2025
less decoding if it has the same syntax context
1 parent 5f3b84a commit 1c6009a

File tree

6 files changed

+144
-97
lines changed

6 files changed

+144
-97
lines changed
 

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

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

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

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

+9-8
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, 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

@@ -742,7 +743,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
742743
exported_symbols,
743744
interpret_alloc_index,
744745
tables,
745-
syntax_contexts,
746+
syntax_context_keys,
746747
expn_data,
747748
expn_hashes,
748749
def_path_hash_map,
@@ -1853,15 +1854,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18531854
self.lazy_array(foreign_modules.iter().map(|(_, m)| m).cloned())
18541855
}
18551856

1856-
fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) {
1857-
let mut syntax_contexts: TableBuilder<_, _> = Default::default();
1857+
fn encode_hygiene(&mut self) -> (SyntaxContextKeyTable, ExpnDataTable, ExpnHashTable) {
1858+
let mut syntax_context_keys: TableBuilder<_, _> = Default::default();
18581859
let mut expn_data_table: TableBuilder<_, _> = Default::default();
18591860
let mut expn_hash_table: TableBuilder<_, _> = Default::default();
18601861

18611862
self.hygiene_ctxt.encode(
1862-
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
1863-
|(this, syntax_contexts, _, _), index, ctxt_data| {
1864-
syntax_contexts.set_some(index, this.lazy(ctxt_data));
1863+
&mut (&mut *self, &mut syntax_context_keys, &mut expn_data_table, &mut expn_hash_table),
1864+
|(this, syntax_context_keys, _, _), index, ctxt_data| {
1865+
syntax_context_keys.set_some(index, this.lazy(ctxt_data));
18651866
},
18661867
|(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
18671868
if let Some(index) = index.as_local() {
@@ -1872,7 +1873,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18721873
);
18731874

18741875
(
1875-
syntax_contexts.encode(&mut self.opaque),
1876+
syntax_context_keys.encode(&mut self.opaque),
18761877
expn_data_table.encode(&mut self.opaque),
18771878
expn_hash_table.encode(&mut self.opaque),
18781879
)

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ use rustc_serialize::opaque::FileEncoder;
3636
use rustc_session::config::{SymbolManglingVersion, TargetModifier};
3737
use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
3838
use rustc_span::edition::Edition;
39-
use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextData};
40-
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Ident, Span, Symbol};
39+
use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextKey};
40+
use rustc_span::symbol::{Ident, Symbol};
41+
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Span};
4142
use rustc_target::spec::{PanicStrategy, TargetTuple};
4243
use table::TableBuilder;
4344
use {rustc_ast as ast, rustc_attr_parsing as attr, rustc_hir as hir};
@@ -193,7 +194,7 @@ enum LazyState {
193194
Previous(NonZero<usize>),
194195
}
195196

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

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

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

279-
syntax_contexts: SyntaxContextTable,
280+
syntax_context_keys: SyntaxContextKeyTable,
280281
expn_data: ExpnDataTable,
281282
expn_hashes: ExpnHashTable,
282283

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

+14-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixed
1616
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
1717
use rustc_session::Session;
1818
use rustc_span::hygiene::{
19-
ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextData,
19+
ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextKey,
2020
};
2121
use rustc_span::source_map::Spanned;
2222
use rustc_span::{
@@ -39,7 +39,7 @@ const TAG_FULL_SPAN: u8 = 0;
3939
const TAG_PARTIAL_SPAN: u8 = 1;
4040
const TAG_RELATIVE_SPAN: u8 = 2;
4141

42-
const TAG_SYNTAX_CONTEXT: u8 = 0;
42+
const TAG_SYNTAX_CONTEXT_KEY: u8 = 0;
4343
const TAG_EXPN_DATA: u8 = 1;
4444

4545
// Tags for encoding Symbol's
@@ -79,7 +79,7 @@ pub struct OnDiskCache {
7979
// to represent the fact that we are storing *encoded* ids. When we decode
8080
// a `SyntaxContext`, a new id will be allocated from the global `HygieneData`,
8181
// which will almost certainly be different than the serialized id.
82-
syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
82+
syntax_context_keys: FxHashMap<u32, AbsoluteBytePos>,
8383
// A map from the `DefPathHash` of an `ExpnId` to the position
8484
// of their associated `ExpnData`. Ideally, we would store a `DefId`,
8585
// but we need to decode this before we've constructed a `TyCtxt` (which
@@ -110,7 +110,7 @@ struct Footer {
110110
// without measurable overhead. This permits larger const allocations without ICEing.
111111
interpret_alloc_index: Vec<u64>,
112112
// See `OnDiskCache.syntax_contexts`
113-
syntax_contexts: FxHashMap<u32, AbsoluteBytePos>,
113+
syntax_context_keys: FxHashMap<u32, AbsoluteBytePos>,
114114
// See `OnDiskCache.expn_data`
115115
expn_data: UnhashMap<ExpnHash, AbsoluteBytePos>,
116116
foreign_expn_data: UnhashMap<ExpnHash, u32>,
@@ -180,7 +180,7 @@ impl OnDiskCache {
180180
query_result_index: footer.query_result_index.into_iter().collect(),
181181
prev_side_effects_index: footer.side_effects_index.into_iter().collect(),
182182
alloc_decoding_state: AllocDecodingState::new(footer.interpret_alloc_index),
183-
syntax_contexts: footer.syntax_contexts,
183+
syntax_context_keys: footer.syntax_context_keys,
184184
expn_data: footer.expn_data,
185185
foreign_expn_data: footer.foreign_expn_data,
186186
hygiene_context: Default::default(),
@@ -196,7 +196,7 @@ impl OnDiskCache {
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 OnDiskCache {
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 OnDiskCache {
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 OnDiskCache {
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
},
@@ -441,7 +441,7 @@ impl OnDiskCache {
441441
file_index_to_file: &self.file_index_to_file,
442442
file_index_to_stable_id: &self.file_index_to_stable_id,
443443
alloc_decoding_session: self.alloc_decoding_state.new_decoding_session(),
444-
syntax_contexts: &self.syntax_contexts,
444+
syntax_context_keys: &self.syntax_context_keys,
445445
expn_data: &self.expn_data,
446446
foreign_expn_data: &self.foreign_expn_data,
447447
hygiene_context: &self.hygiene_context,
@@ -461,7 +461,7 @@ pub struct CacheDecoder<'a, 'tcx> {
461461
file_index_to_file: &'a Lock<FxHashMap<SourceFileIndex, Arc<SourceFile>>>,
462462
file_index_to_stable_id: &'a FxHashMap<SourceFileIndex, EncodedSourceFileId>,
463463
alloc_decoding_session: AllocDecodingSession<'a>,
464-
syntax_contexts: &'a FxHashMap<u32, AbsoluteBytePos>,
464+
syntax_context_keys: &'a FxHashMap<u32, AbsoluteBytePos>,
465465
expn_data: &'a UnhashMap<ExpnHash, AbsoluteBytePos>,
466466
foreign_expn_data: &'a UnhashMap<ExpnHash, u32>,
467467
hygiene_context: &'a HygieneDecodeContext,
@@ -576,13 +576,12 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
576576

577577
impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
578578
fn decode_syntax_context(&mut self) -> SyntaxContext {
579-
let syntax_contexts = self.syntax_contexts;
580579
rustc_span::hygiene::decode_syntax_context(self, self.hygiene_context, |this, id| {
581580
// This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
582581
// We look up the position of the associated `SyntaxData` and decode it.
583-
let pos = syntax_contexts.get(&id).unwrap();
582+
let pos = this.syntax_context_keys.get(&id).unwrap();
584583
this.with_position(pos.to_usize(), |decoder| {
585-
let data: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
584+
let data: SyntaxContextKey = decode_tagged(decoder, TAG_SYNTAX_CONTEXT_KEY);
586585
data
587586
})
588587
})

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

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ trivially_parameterized_over_tcx! {
111111
rustc_span::Span,
112112
rustc_span::Symbol,
113113
rustc_span::def_id::DefPathHash,
114+
rustc_span::hygiene::SyntaxContextKey,
114115
rustc_span::hygiene::SyntaxContextData,
115116
rustc_span::Ident,
116117
rustc_type_ir::Variance,
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.