diff options
| author | klensy <klensy@users.noreply.github.com> | 2022-07-03 19:18:53 +0300 |
|---|---|---|
| committer | klensy <klensy@users.noreply.github.com> | 2022-08-15 17:56:37 +0300 |
| commit | adba4691f6577b35b2d48ebbe6c8e993eea09978 (patch) | |
| tree | f74e9751ed77a685ccf84fae933dbdf3b88108a6 /compiler/rustc_metadata/src | |
| parent | 801821d1560f84e4716fcbd9244ec959320a13d5 (diff) | |
| download | rust-adba4691f6577b35b2d48ebbe6c8e993eea09978.tar.gz rust-adba4691f6577b35b2d48ebbe6c8e993eea09978.zip | |
cache strings while encoding/decoding to compiler artifacts
Diffstat (limited to 'compiler/rustc_metadata/src')
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/decoder.rs | 29 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/encoder.rs | 21 | ||||
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/mod.rs | 4 |
3 files changed, 54 insertions, 0 deletions
diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 40dc4fb052d..b0d9e4a63f5 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -637,6 +637,35 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Span { } } +impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Symbol { + fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self { + let tag = d.read_u8(); + + match tag { + SYMBOL_STR => { + let s = d.read_str(); + Symbol::intern(s) + } + SYMBOL_OFFSET => { + // read str offset + let pos = d.read_usize(); + let old_pos = d.opaque.position(); + + // move to str ofset and read + d.opaque.set_position(pos); + let s = d.read_str(); + let sym = Symbol::intern(s); + + // restore position + d.opaque.set_position(old_pos); + + sym + } + _ => unreachable!(), + } + } +} + impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for &'tcx [ty::abstract_const::Node<'tcx>] { fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self { ty::codec::RefDecodable::decode(d) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index f68005c0526..13436af09b7 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -39,6 +39,7 @@ use rustc_span::{ }; use rustc_target::abi::VariantIdx; use std::borrow::Borrow; +use std::collections::hash_map::Entry; use std::hash::Hash; use std::io::{Read, Seek, Write}; use std::iter; @@ -75,6 +76,7 @@ pub(super) struct EncodeContext<'a, 'tcx> { required_source_files: Option<GrowableBitSet<usize>>, is_proc_macro: bool, hygiene_ctxt: &'a HygieneEncodeContext, + symbol_table: FxHashMap<Symbol, usize>, } /// If the current crate is a proc-macro, returns early with `Lazy:empty()`. @@ -307,6 +309,24 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span { } } +impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Symbol { + fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { + match s.symbol_table.entry(*self) { + Entry::Vacant(o) => { + s.opaque.emit_u8(SYMBOL_STR); + let pos = s.opaque.position(); + o.insert(pos); + s.emit_str(self.as_str()); + } + Entry::Occupied(o) => { + let x = o.get().clone(); + s.emit_u8(SYMBOL_OFFSET); + s.emit_usize(x); + } + } + } +} + impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; @@ -2259,6 +2279,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) { required_source_files, is_proc_macro: tcx.sess.crate_types().contains(&CrateType::ProcMacro), hygiene_ctxt: &hygiene_ctxt, + symbol_table: Default::default(), }; // Encode the rustc version string in a predictable location. diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 8efe5051b01..2bdb1feb677 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -445,6 +445,10 @@ const TAG_VALID_SPAN_LOCAL: u8 = 0; const TAG_VALID_SPAN_FOREIGN: u8 = 1; const TAG_PARTIAL_SPAN: u8 = 2; +// Tags for encoding Symbol's +const SYMBOL_STR: u8 = 0; +const SYMBOL_OFFSET: u8 = 1; + pub fn provide(providers: &mut Providers) { encoder::provide(providers); decoder::provide(providers); |
