Skip to content

Commit

Permalink
Auto merge of #97905 - nnethercote:revert-infallible-encoder, r=bjorn3
Browse files Browse the repository at this point in the history
Revert part of #94372 to improve performance

#94732 was supposed to give small but widespread performance improvements, as judged from three per-merge performance runs. But the performance run that occurred after merging included a roughly equal number of improvements and regressions, for unclear reasons.

This PR is for a test run reverting those changes, to see what happens.

r? `@ghost`
  • Loading branch information
bors committed Jun 11, 2022
2 parents 75307c2 + 3186e31 commit c845946
Show file tree
Hide file tree
Showing 25 changed files with 250 additions and 173 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_macros::HashStable_Generic;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize::{self, Decoder, Encoder};
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
Expand Down Expand Up @@ -2488,11 +2488,11 @@ rustc_index::newtype_index! {
}
}

impl<S: Encoder> Encodable<S> for AttrId {
impl<S: Encoder> rustc_serialize::Encodable<S> for AttrId {
fn encode(&self, _s: &mut S) {}
}

impl<D: Decoder> Decodable<D> for AttrId {
impl<D: Decoder> rustc_serialize::Decodable<D> for AttrId {
fn decode(_: &mut D) -> AttrId {
crate::attr::mk_attr_id()
}
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_codegen_ssa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ use rustc_middle::dep_graph::WorkProduct;
use rustc_middle::middle::dependency_format::Dependencies;
use rustc_middle::middle::exported_symbols::SymbolExportKind;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_serialize::opaque::{MemDecoder, MemEncoder};
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
use rustc_session::cstore::{self, CrateSource};
use rustc_session::utils::NativeLibKind;
Expand Down Expand Up @@ -204,14 +203,14 @@ const RUSTC_VERSION: Option<&str> = option_env!("CFG_VERSION");

impl CodegenResults {
pub fn serialize_rlink(codegen_results: &CodegenResults) -> Vec<u8> {
let mut encoder = MemEncoder::new();
let mut encoder = opaque::Encoder::new();
encoder.emit_raw_bytes(RLINK_MAGIC);
// `emit_raw_bytes` is used to make sure that the version representation does not depend on
// Encoder's inner representation of `u32`.
encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
encoder.emit_str(RUSTC_VERSION.unwrap());
Encodable::encode(codegen_results, &mut encoder);
encoder.finish()
encoder.finish().unwrap()
}

pub fn deserialize_rlink(data: Vec<u8>) -> Result<Self, String> {
Expand All @@ -231,7 +230,7 @@ impl CodegenResults {
return Err(".rlink file was produced with encoding version {version_array}, but the current version is {RLINK_VERSION}".to_string());
}

let mut decoder = MemDecoder::new(&data[4..], 0);
let mut decoder = opaque::Decoder::new(&data[4..], 0);
let rustc_version = decoder.read_str();
let current_version = RUSTC_VERSION.unwrap();
if rustc_version != current_version {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_data_structures/src/fingerprint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::stable_hasher;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize::{Decodable, Encodable};
use std::convert::TryInto;
use std::hash::{Hash, Hasher};

Expand Down Expand Up @@ -142,14 +142,14 @@ impl stable_hasher::StableHasherResult for Fingerprint {

impl_stable_hash_via_hash!(Fingerprint);

impl<E: Encoder> Encodable<E> for Fingerprint {
impl<E: rustc_serialize::Encoder> Encodable<E> for Fingerprint {
#[inline]
fn encode(&self, s: &mut E) {
s.emit_raw_bytes(&self.to_le_bytes());
}
}

impl<D: Decoder> Decodable<D> for Fingerprint {
impl<D: rustc_serialize::Decoder> Decodable<D> for Fingerprint {
#[inline]
fn decode(d: &mut D) -> Self {
Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap())
Expand Down Expand Up @@ -184,7 +184,7 @@ impl std::fmt::Display for PackedFingerprint {
}
}

impl<E: Encoder> Encodable<E> for PackedFingerprint {
impl<E: rustc_serialize::Encoder> Encodable<E> for PackedFingerprint {
#[inline]
fn encode(&self, s: &mut E) {
// Copy to avoid taking reference to packed field.
Expand All @@ -193,7 +193,7 @@ impl<E: Encoder> Encodable<E> for PackedFingerprint {
}
}

impl<D: Decoder> Decodable<D> for PackedFingerprint {
impl<D: rustc_serialize::Decoder> Decodable<D> for PackedFingerprint {
#[inline]
fn decode(d: &mut D) -> Self {
Self(Fingerprint::decode(d))
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_incremental/src/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::memmap::Mmap;
use rustc_middle::dep_graph::{SerializedDepGraph, WorkProduct, WorkProductId};
use rustc_middle::ty::OnDiskCache;
use rustc_serialize::opaque::MemDecoder;
use rustc_serialize::opaque::Decoder;
use rustc_serialize::Decodable;
use rustc_session::config::IncrementalStateAssertion;
use rustc_session::Session;
Expand Down Expand Up @@ -156,7 +156,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {

if let LoadResult::Ok { data: (work_products_data, start_pos) } = load_result {
// Decode the list of work_products
let mut work_product_decoder = MemDecoder::new(&work_products_data[..], start_pos);
let mut work_product_decoder = Decoder::new(&work_products_data[..], start_pos);
let work_products: Vec<SerializedWorkProduct> =
Decodable::decode(&mut work_product_decoder);

Expand Down Expand Up @@ -193,7 +193,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
LoadResult::DataOutOfDate => LoadResult::DataOutOfDate,
LoadResult::Error { message } => LoadResult::Error { message },
LoadResult::Ok { data: (bytes, start_pos) } => {
let mut decoder = MemDecoder::new(&bytes, start_pos);
let mut decoder = Decoder::new(&bytes, start_pos);
let prev_commandline_args_hash = u64::decode(&mut decoder);

if prev_commandline_args_hash != expected_hash {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_incremental/src/persist/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_data_structures::sync::join;
use rustc_middle::dep_graph::{DepGraph, SerializedDepGraph, WorkProduct, WorkProductId};
use rustc_middle::ty::TyCtxt;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_serialize::Encodable;
use rustc_serialize::{Encodable as RustcEncodable, Encoder};
use rustc_session::Session;
use std::fs;

Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ use rustc_middle::ty::codec::TyDecoder;
use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::GeneratorDiagnosticData;
use rustc_middle::ty::{self, ParameterizedOverTcx, Ty, TyCtxt, Visibility};
use rustc_serialize::opaque::MemDecoder;
use rustc_serialize::{Decodable, Decoder};
use rustc_serialize::{opaque, Decodable, Decoder};
use rustc_session::cstore::{
CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib,
};
Expand Down Expand Up @@ -155,7 +154,7 @@ struct ImportedSourceFile {
}

pub(super) struct DecodeContext<'a, 'tcx> {
opaque: MemDecoder<'a>,
opaque: opaque::Decoder<'a>,
cdata: Option<CrateMetadataRef<'a>>,
blob: &'a MetadataBlob,
sess: Option<&'tcx Session>,
Expand Down Expand Up @@ -187,7 +186,7 @@ pub(super) trait Metadata<'a, 'tcx>: Copy {
fn decoder(self, pos: usize) -> DecodeContext<'a, 'tcx> {
let tcx = self.tcx();
DecodeContext {
opaque: MemDecoder::new(self.blob(), pos),
opaque: opaque::Decoder::new(self.blob(), pos),
cdata: self.cdata(),
blob: self.blob(),
sess: self.sess().or(tcx.map(|tcx| tcx.sess)),
Expand Down Expand Up @@ -419,7 +418,7 @@ impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> {
where
F: FnOnce(&mut Self) -> R,
{
let new_opaque = MemDecoder::new(self.opaque.data, pos);
let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
let r = f(self);
Expand Down
16 changes: 11 additions & 5 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use rustc_middle::ty::codec::TyEncoder;
use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
use rustc_serialize::opaque::MemEncoder;
use rustc_serialize::{Encodable, Encoder};
use rustc_serialize::{opaque, Encodable, Encoder};
use rustc_session::config::CrateType;
use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib};
use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind};
Expand All @@ -44,7 +43,7 @@ use std::num::NonZeroUsize;
use tracing::{debug, trace};

pub(super) struct EncodeContext<'a, 'tcx> {
opaque: MemEncoder,
opaque: opaque::Encoder,
tcx: TyCtxt<'tcx>,
feat: &'tcx rustc_feature::Features,

Expand Down Expand Up @@ -94,6 +93,9 @@ macro_rules! encoder_methods {
}

impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
type Ok = <opaque::Encoder as Encoder>::Ok;
type Err = <opaque::Encoder as Encoder>::Err;

encoder_methods! {
emit_usize(usize);
emit_u128(u128);
Expand All @@ -116,6 +118,10 @@ impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> {
emit_str(&str);
emit_raw_bytes(&[u8]);
}

fn finish(self) -> Result<Self::Ok, Self::Err> {
self.opaque.finish()
}
}

impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyValue<T> {
Expand Down Expand Up @@ -2182,7 +2188,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata {
}

fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
let mut encoder = MemEncoder::new();
let mut encoder = opaque::Encoder::new();
encoder.emit_raw_bytes(METADATA_HEADER);

// Will be filled with the root position after encoding everything.
Expand Down Expand Up @@ -2217,7 +2223,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
// culminating in the `CrateRoot` which points to all of it.
let root = ecx.encode_crate_root();

let mut result = ecx.opaque.finish();
let mut result = ecx.opaque.finish().unwrap();

// Encode the root position.
let header = METADATA_HEADER.len();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, ReprOptions, Ty};
use rustc_middle::ty::{GeneratorDiagnosticData, ParameterizedOverTcx, TyCtxt};
use rustc_serialize::opaque::MemEncoder;
use rustc_serialize::opaque::Encoder;
use rustc_session::config::SymbolManglingVersion;
use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
use rustc_span::edition::Edition;
Expand Down Expand Up @@ -323,7 +323,7 @@ macro_rules! define_tables {
}

impl TableBuilders {
fn encode(&self, buf: &mut MemEncoder) -> LazyTables {
fn encode(&self, buf: &mut Encoder) -> LazyTables {
LazyTables {
$($name: self.$name.encode(buf)),+
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/rmeta/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use rustc_data_structures::fingerprint::Fingerprint;
use rustc_hir::def::{CtorKind, CtorOf};
use rustc_index::vec::Idx;
use rustc_middle::ty::ParameterizedOverTcx;
use rustc_serialize::opaque::MemEncoder;
use rustc_serialize::Encoder;
use rustc_serialize::opaque::Encoder;
use rustc_serialize::Encoder as _;
use rustc_span::hygiene::MacroKind;
use std::convert::TryInto;
use std::marker::PhantomData;
Expand Down Expand Up @@ -281,7 +281,7 @@ where
Some(value).write_to_bytes(&mut self.blocks[i]);
}

pub(crate) fn encode<const N: usize>(&self, buf: &mut MemEncoder) -> LazyTable<I, T>
pub(crate) fn encode<const N: usize>(&self, buf: &mut Encoder) -> LazyTable<I, T>
where
Option<T>: FixedSizeEncoding<ByteArray = [u8; N]>,
{
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_middle/src/mir/graph_cyclic_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_data_structures::graph::{
};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::OnceCell;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize as serialize;

/// Helper type to cache the result of `graph::is_cyclic`.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -36,17 +36,17 @@ impl GraphIsCyclicCache {
}
}

impl<S: Encoder> Encodable<S> for GraphIsCyclicCache {
impl<S: serialize::Encoder> serialize::Encodable<S> for GraphIsCyclicCache {
#[inline]
fn encode(&self, s: &mut S) {
Encodable::encode(&(), s);
serialize::Encodable::encode(&(), s);
}
}

impl<D: Decoder> Decodable<D> for GraphIsCyclicCache {
impl<D: serialize::Decoder> serialize::Decodable<D> for GraphIsCyclicCache {
#[inline]
fn decode(d: &mut D) -> Self {
let () = Decodable::decode(d);
let () = serialize::Decodable::decode(d);
Self::new()
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/mir/predecessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::OnceCell;
use rustc_index::vec::IndexVec;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize as serialize;
use smallvec::SmallVec;

use crate::mir::{BasicBlock, BasicBlockData};
Expand Down Expand Up @@ -54,12 +54,12 @@ impl PredecessorCache {
}
}

impl<S: Encoder> Encodable<S> for PredecessorCache {
impl<S: serialize::Encoder> serialize::Encodable<S> for PredecessorCache {
#[inline]
fn encode(&self, _s: &mut S) {}
}

impl<D: Decoder> Decodable<D> for PredecessorCache {
impl<D: serialize::Decoder> serialize::Decodable<D> for PredecessorCache {
#[inline]
fn decode(_: &mut D) -> Self {
Self::new()
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/mir/switch_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::stable_map::FxHashMap;
use rustc_data_structures::sync::OnceCell;
use rustc_index::vec::IndexVec;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize as serialize;
use smallvec::SmallVec;

use crate::mir::{BasicBlock, BasicBlockData, Terminator, TerminatorKind};
Expand Down Expand Up @@ -54,12 +54,12 @@ impl SwitchSourceCache {
}
}

impl<S: Encoder> Encodable<S> for SwitchSourceCache {
impl<S: serialize::Encoder> serialize::Encodable<S> for SwitchSourceCache {
#[inline]
fn encode(&self, _s: &mut S) {}
}

impl<D: Decoder> Decodable<D> for SwitchSourceCache {
impl<D: serialize::Decoder> serialize::Decodable<D> for SwitchSourceCache {
#[inline]
fn decode(_: &mut D) -> Self {
Self::new()
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_middle/src/mir/traversal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::OnceCell;
use rustc_index::bit_set::BitSet;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize as serialize;

use super::*;

Expand Down Expand Up @@ -365,12 +365,12 @@ impl PostorderCache {
}
}

impl<S: Encoder> Encodable<S> for PostorderCache {
impl<S: serialize::Encoder> serialize::Encodable<S> for PostorderCache {
#[inline]
fn encode(&self, _s: &mut S) {}
}

impl<D: Decoder> Decodable<D> for PostorderCache {
impl<D: serialize::Decoder> serialize::Decodable<D> for PostorderCache {
#[inline]
fn decode(_: &mut D) -> Self {
Self::new()
Expand Down
Loading

0 comments on commit c845946

Please sign in to comment.