From da3b2ca95636eea18c62089bae5e7443ecf94c9a Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 22 Feb 2022 18:05:51 -0500 Subject: [PATCH 1/3] Provide raw &str access from Decoder --- compiler/rustc_serialize/src/opaque.rs | 5 ++--- compiler/rustc_serialize/src/serialize.rs | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 7a05d2b762a..8e2c866cd38 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -1,6 +1,5 @@ use crate::leb128::{self, max_leb128_len}; use crate::serialize::{self, Encoder as _}; -use std::borrow::Cow; use std::convert::TryInto; use std::fs::File; use std::io::{self, Write}; @@ -663,7 +662,7 @@ fn read_char(&mut self) -> char { } #[inline] - fn read_str(&mut self) -> Cow<'_, str> { + fn read_str(&mut self) -> &str { let len = self.read_usize(); let sentinel = self.data[self.position + len]; assert!(sentinel == STR_SENTINEL); @@ -671,7 +670,7 @@ fn read_str(&mut self) -> Cow<'_, str> { std::str::from_utf8_unchecked(&self.data[self.position..self.position + len]) }; self.position += len + 1; - Cow::Borrowed(s) + s } #[inline] diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index a012be2857e..fbbd13657ba 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -198,7 +198,7 @@ pub trait Decoder { fn read_f64(&mut self) -> f64; fn read_f32(&mut self) -> f32; fn read_char(&mut self) -> char; - fn read_str(&mut self) -> Cow<'_, str>; + fn read_str(&mut self) -> &str; fn read_raw_bytes_into(&mut self, s: &mut [u8]); } @@ -313,7 +313,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { impl Decodable for String { fn decode(d: &mut D) -> String { - d.read_str().into_owned() + d.read_str().to_owned() } } From 2098ea6eba5ac7901b419841972c9bbffbf49a93 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 22 Feb 2022 18:11:59 -0500 Subject: [PATCH 2/3] Provide copy-free access to raw Decoder bytes --- .../rustc_data_structures/src/fingerprint.rs | 4 +--- compiler/rustc_metadata/src/rmeta/decoder.rs | 2 +- compiler/rustc_middle/src/ty/codec.rs | 6 +++--- compiler/rustc_serialize/src/opaque.rs | 19 ++++++------------- compiler/rustc_serialize/src/serialize.rs | 2 +- 5 files changed, 12 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_data_structures/src/fingerprint.rs b/compiler/rustc_data_structures/src/fingerprint.rs index e931379dd3a..c88f3e73cff 100644 --- a/compiler/rustc_data_structures/src/fingerprint.rs +++ b/compiler/rustc_data_structures/src/fingerprint.rs @@ -153,9 +153,7 @@ fn encode(&self, s: &mut E) -> Result<(), E::Error> { impl Decodable for Fingerprint { #[inline] fn decode(d: &mut D) -> Self { - let mut bytes = [0u8; 16]; - d.read_raw_bytes_into(&mut bytes); - Fingerprint::from_le_bytes(bytes) + Fingerprint::from_le_bytes(d.read_raw_bytes(16).try_into().unwrap()) } } diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 66968c9ba54..b715f6c3f1f 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -316,7 +316,7 @@ fn read_lazy_with_meta(&mut self, meta: T::Meta) -> Lazy &'a [u8] { + pub fn read_raw_bytes(&mut self, len: usize) -> &[u8] { self.opaque.read_raw_bytes(len) } } diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index ecd30ba441f..7a6cbea00d8 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -485,12 +485,12 @@ impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> { read_f64 -> f64; read_f32 -> f32; read_char -> char; - read_str -> Cow<'_, str>; + read_str -> &str; } #[inline] - fn read_raw_bytes_into(&mut self, bytes: &mut [u8]) { - self.opaque.read_raw_bytes_into(bytes) + fn read_raw_bytes(&mut self, len: usize) -> &[u8] { + self.opaque.read_raw_bytes(len) } } } diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 8e2c866cd38..1a71ee9038b 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -1,5 +1,5 @@ use crate::leb128::{self, max_leb128_len}; -use crate::serialize::{self, Encoder as _}; +use crate::serialize::{self, Decoder as _, Encoder as _}; use std::convert::TryInto; use std::fs::File; use std::io::{self, Write}; @@ -548,13 +548,6 @@ pub fn set_position(&mut self, pos: usize) { pub fn advance(&mut self, bytes: usize) { self.position += bytes; } - - #[inline] - pub fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] { - let start = self.position; - self.position += bytes; - &self.data[start..self.position] - } } macro_rules! read_leb128 { @@ -662,7 +655,7 @@ fn read_char(&mut self) -> char { } #[inline] - fn read_str(&mut self) -> &str { + fn read_str(&mut self) -> &'a str { let len = self.read_usize(); let sentinel = self.data[self.position + len]; assert!(sentinel == STR_SENTINEL); @@ -674,10 +667,10 @@ fn read_str(&mut self) -> &str { } #[inline] - fn read_raw_bytes_into(&mut self, s: &mut [u8]) { + fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] { let start = self.position; - self.position += s.len(); - s.copy_from_slice(&self.data[start..self.position]); + self.position += bytes; + &self.data[start..self.position] } } @@ -745,10 +738,10 @@ impl<'a> serialize::Decodable> for IntEncodedWithFixedSize { fn decode(decoder: &mut Decoder<'a>) -> IntEncodedWithFixedSize { let _start_pos = decoder.position(); let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE); + let value = u64::from_le_bytes(bytes.try_into().unwrap()); let _end_pos = decoder.position(); debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE); - let value = u64::from_le_bytes(bytes.try_into().unwrap()); IntEncodedWithFixedSize(value) } } diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index fbbd13657ba..7b6dd8b60f8 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -199,7 +199,7 @@ pub trait Decoder { fn read_f32(&mut self) -> f32; fn read_char(&mut self) -> char; fn read_str(&mut self) -> &str; - fn read_raw_bytes_into(&mut self, s: &mut [u8]); + fn read_raw_bytes(&mut self, len: usize) -> &[u8]; } /// Trait for types that can be serialized From f1bcb0f3afb20e63f94bfff396f96027eecd0ad1 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Tue, 22 Feb 2022 18:14:51 -0500 Subject: [PATCH 3/3] Delete Decoder::read_unit --- compiler/rustc_ast/src/ast.rs | 3 +-- compiler/rustc_middle/src/mir/predecessors.rs | 3 +-- compiler/rustc_middle/src/ty/codec.rs | 2 -- compiler/rustc_serialize/src/opaque.rs | 5 ----- compiler/rustc_serialize/src/serialize.rs | 8 ++------ 5 files changed, 4 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index e9135b71630..0449d715a5e 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2418,8 +2418,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { } impl rustc_serialize::Decodable for AttrId { - fn decode(d: &mut D) -> AttrId { - d.read_unit(); + fn decode(_: &mut D) -> AttrId { crate::attr::mk_attr_id() } } diff --git a/compiler/rustc_middle/src/mir/predecessors.rs b/compiler/rustc_middle/src/mir/predecessors.rs index 2562baac911..4fe2cde7532 100644 --- a/compiler/rustc_middle/src/mir/predecessors.rs +++ b/compiler/rustc_middle/src/mir/predecessors.rs @@ -63,8 +63,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { impl serialize::Decodable for PredecessorCache { #[inline] - fn decode(d: &mut D) -> Self { - let () = d.read_unit(); + fn decode(_: &mut D) -> Self { Self::new() } } diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 7a6cbea00d8..23fb7a49d9c 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -465,8 +465,6 @@ mod __ty_decoder_impl { impl<$($typaram ),*> Decoder for $DecoderName<$($typaram),*> { $crate::__impl_decoder_methods! { - read_unit -> (); - read_u128 -> u128; read_u64 -> u64; read_u32 -> u32; diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 1a71ee9038b..5e5cbacbcff 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -555,11 +555,6 @@ macro_rules! read_leb128 { } impl<'a> serialize::Decoder for Decoder<'a> { - #[inline] - fn read_unit(&mut self) -> () { - () - } - #[inline] fn read_u128(&mut self) -> u128 { read_leb128!(self, read_u128_leb128) diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index 7b6dd8b60f8..42bf6ff2a98 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -181,7 +181,6 @@ fn emit_map_elt_val(&mut self, f: F) -> Result<(), Self::Error> // concise. pub trait Decoder { // Primitive types: - fn read_unit(&mut self) -> (); fn read_usize(&mut self) -> usize; fn read_u128(&mut self) -> u128; fn read_u64(&mut self) -> u64; @@ -324,9 +323,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { } impl Decodable for () { - fn decode(d: &mut D) -> () { - d.read_unit() - } + fn decode(_: &mut D) -> () {} } impl Encodable for PhantomData { @@ -336,8 +333,7 @@ fn encode(&self, s: &mut S) -> Result<(), S::Error> { } impl Decodable for PhantomData { - fn decode(d: &mut D) -> PhantomData { - d.read_unit(); + fn decode(_: &mut D) -> PhantomData { PhantomData } }