From bb02cc47c49ee3cc1e913c243d69ee7cb23df598 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 16 Jun 2022 16:00:25 +1000 Subject: Move `finish` out of the `Encoder` trait. This simplifies things, but requires making `CacheEncoder` non-generic. (This was previously merged as commit 4 in #94732 and then was reverted in #97905 because it caused a perf regression.) --- compiler/rustc_query_impl/src/on_disk_cache.rs | 92 ++++++++------------------ compiler/rustc_query_impl/src/plumbing.rs | 3 +- 2 files changed, 27 insertions(+), 68 deletions(-) (limited to 'compiler/rustc_query_impl/src') diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index d7327ca4bc6..0fb1d728293 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -25,6 +25,7 @@ use rustc_span::hygiene::{ use rustc_span::source_map::{SourceMap, StableSourceFileId}; use rustc_span::CachingSourceMapView; use rustc_span::{BytePos, ExpnData, ExpnHash, Pos, SourceFile, Span}; +use std::io; use std::mem; const TAG_FILE_FOOTER: u128 = 0xC0FFEE_C0FFEE_C0FFEE_C0FFEE_C0FFEE; @@ -807,21 +808,10 @@ impl_ref_decoder! {<'tcx> //- ENCODING ------------------------------------------------------------------- -pub trait OpaqueEncoder: Encoder { - fn position(&self) -> usize; -} - -impl OpaqueEncoder for FileEncoder { - #[inline] - fn position(&self) -> usize { - FileEncoder::position(self) - } -} - /// An encoder that can write to the incremental compilation cache. -pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> { +pub struct CacheEncoder<'a, 'tcx> { tcx: TyCtxt<'tcx>, - encoder: E, + encoder: FileEncoder, type_shorthands: FxHashMap, usize>, predicate_shorthands: FxHashMap, usize>, interpret_allocs: FxIndexSet, @@ -830,10 +820,7 @@ pub struct CacheEncoder<'a, 'tcx, E: OpaqueEncoder> { hygiene_context: &'a HygieneEncodeContext, } -impl<'a, 'tcx, E> CacheEncoder<'a, 'tcx, E> -where - E: OpaqueEncoder, -{ +impl<'a, 'tcx> CacheEncoder<'a, 'tcx> { fn source_file_index(&mut self, source_file: Lrc) -> SourceFileIndex { self.file_to_file_index[&(&*source_file as *const SourceFile)] } @@ -852,32 +839,27 @@ where let end_pos = self.position(); ((end_pos - start_pos) as u64).encode(self); } + + fn finish(self) -> Result { + self.encoder.finish() + } } -impl<'a, 'tcx, E> Encodable> for SyntaxContext -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for SyntaxContext { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s); } } -impl<'a, 'tcx, E> Encodable> for ExpnId -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for ExpnId { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { s.hygiene_context.schedule_expn_data_for_encoding(*self); self.expn_hash().encode(s); } } -impl<'a, 'tcx, E> Encodable> for Span -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for Span { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { let span_data = self.data_untracked(); span_data.ctxt.encode(s); span_data.parent.encode(s); @@ -920,10 +902,7 @@ where } } -impl<'a, 'tcx, E> TyEncoder for CacheEncoder<'a, 'tcx, E> -where - E: OpaqueEncoder, -{ +impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> { type I = TyCtxt<'tcx>; const CLEAR_CROSS_CRATE: bool = false; @@ -943,29 +922,20 @@ where } } -impl<'a, 'tcx, E> Encodable> for CrateNum -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for CrateNum { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { s.tcx.stable_crate_id(*self).encode(s); } } -impl<'a, 'tcx, E> Encodable> for DefId -where - E: OpaqueEncoder, -{ - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for DefId { + fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { s.tcx.def_path_hash(*self).encode(s); } } -impl<'a, 'tcx, E> Encodable> for DefIndex -where - E: OpaqueEncoder, -{ - fn encode(&self, _: &mut CacheEncoder<'a, 'tcx, E>) { +impl<'a, 'tcx> Encodable> for DefIndex { + fn encode(&self, _: &mut CacheEncoder<'a, 'tcx>) { bug!("encoding `DefIndex` without context"); } } @@ -979,13 +949,7 @@ macro_rules! encoder_methods { } } -impl<'a, 'tcx, E> Encoder for CacheEncoder<'a, 'tcx, E> -where - E: OpaqueEncoder, -{ - type Ok = E::Ok; - type Err = E::Err; - +impl<'a, 'tcx> Encoder for CacheEncoder<'a, 'tcx> { encoder_methods! { emit_usize(usize); emit_u128(u128); @@ -1008,30 +972,26 @@ where emit_str(&str); emit_raw_bytes(&[u8]); } - - fn finish(self) -> Result { - self.encoder.finish() - } } // This ensures that the `Encodable::encode` specialization for byte slices // is used when a `CacheEncoder` having an `opaque::FileEncoder` is passed to `Encodable::encode`. // Unfortunately, we have to manually opt into specializations this way, given how `CacheEncoder` // and the encoding traits currently work. -impl<'a, 'tcx> Encodable> for [u8] { - fn encode(&self, e: &mut CacheEncoder<'a, 'tcx, FileEncoder>) { +impl<'a, 'tcx> Encodable> for [u8] { + fn encode(&self, e: &mut CacheEncoder<'a, 'tcx>) { self.encode(&mut e.encoder); } } pub fn encode_query_results<'a, 'tcx, CTX, Q>( tcx: CTX, - encoder: &mut CacheEncoder<'a, 'tcx, FileEncoder>, + encoder: &mut CacheEncoder<'a, 'tcx>, query_result_index: &mut EncodedDepNodeIndex, ) where CTX: QueryContext + 'tcx, Q: super::QueryDescription, - Q::Value: Encodable>, + Q::Value: Encodable>, { let _timer = tcx .dep_context() diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 87aedc6542d..66f4508f6b4 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -12,7 +12,6 @@ use rustc_query_system::query::{QueryContext, QueryJobId, QueryMap, QuerySideEff use rustc_data_structures::sync::Lock; use rustc_data_structures::thin_vec::ThinVec; use rustc_errors::{Diagnostic, Handler}; -use rustc_serialize::opaque; use std::any::Any; use std::num::NonZeroU64; @@ -140,7 +139,7 @@ impl<'tcx> QueryCtxt<'tcx> { pub(super) fn encode_query_results( self, - encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx, opaque::FileEncoder>, + encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>, query_result_index: &mut on_disk_cache::EncodedDepNodeIndex, ) { macro_rules! encode_queries { -- cgit 1.4.1-3-g733a5