Skip to content

Refactor how opaque::Decoder represents its contents #58653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/librustc/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,21 @@ impl AbsoluteBytePos {

impl<'sess> OnDiskCache<'sess> {
/// Creates a new OnDiskCache instance from the serialized data in `data`.
pub fn new(sess: &'sess Session, data: Vec<u8>, start_pos: usize) -> OnDiskCache<'sess> {
pub fn new(sess: &'sess Session, data: Vec<u8>) -> OnDiskCache<'sess> {
debug_assert!(sess.opts.incremental.is_some());

// Wrapping in a scope so we can borrow `data`
let footer: Footer = {
let mut decoder = opaque::Decoder::new(&data[..], start_pos);

// Decode the *position* of the footer which can be found in the
// last 8 bytes of the file.
decoder.set_position(data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
let mut decoder = opaque::Decoder::new(
&data, data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
let query_result_index_pos = IntEncodedWithFixedSize::decode(&mut decoder)
.expect("Error while trying to decode query result index position.")
.0 as usize;

// Decoder the file footer which contains all the lookup tables, etc.
decoder.set_position(query_result_index_pos);
decoder = opaque::Decoder::new(&data, query_result_index_pos);
decode_tagged(&mut decoder, TAG_FILE_FOOTER)
.expect("Error while trying to decode query result index position.")
};
Expand Down Expand Up @@ -540,7 +539,7 @@ impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx,

#[inline]
fn peek_byte(&self) -> u8 {
self.opaque.data[self.opaque.position()]
self.opaque.data()[0]
}

fn cached_ty_for_shorthand<F>(&mut self,
Expand Down Expand Up @@ -569,9 +568,9 @@ impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx,
fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
where F: FnOnce(&mut Self) -> R
{
debug_assert!(pos < self.opaque.data.len());
debug_assert!(pos < self.opaque.original_data.len());

let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
let new_opaque = opaque::Decoder::new(&self.opaque.original_data, pos);
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
let r = f(self);
self.opaque = old_opaque;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub fn load_query_result_cache<'sess>(sess: &'sess Session) -> OnDiskCache<'sess
}

match load_data(sess.opts.debugging_opts.incremental_info, &query_cache_path(sess)) {
LoadResult::Ok{ data: (bytes, start_pos) } => OnDiskCache::new(sess, bytes, start_pos),
LoadResult::Ok{ data: (bytes, _) } => OnDiskCache::new(sess, bytes),
_ => OnDiskCache::new_empty(sess.source_map())
}
}
4 changes: 2 additions & 2 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {

#[inline]
fn peek_byte(&self) -> u8 {
self.opaque.data[self.opaque.position()]
self.opaque.data()[0]
}

#[inline]
Expand Down Expand Up @@ -212,7 +212,7 @@ impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
where F: FnOnce(&mut Self) -> R
{
let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
let new_opaque = opaque::Decoder::new(self.opaque.original_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
8 changes: 4 additions & 4 deletions src/libserialize/leb128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ pub fn write_signed_leb128(out: &mut Vec<u8>, value: i128) {
}

#[inline]
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
pub fn read_signed_leb128(data: &[u8]) -> (i128, usize) {
let mut result = 0;
let mut shift = 0;
let mut position = start_position;
let mut position = 0;
let mut byte;

loop {
Expand All @@ -136,7 +136,7 @@ pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
result |= -(1 << shift);
}

(result, position - start_position)
(result, position)
}

macro_rules! impl_test_unsigned_leb128 {
Expand Down Expand Up @@ -176,7 +176,7 @@ fn test_signed_leb128() {
}
let mut pos = 0;
for &x in &values {
let (value, bytes_read) = read_signed_leb128(&mut stream, pos);
let (value, bytes_read) = read_signed_leb128(&mut stream[pos..]);
pos += bytes_read;
assert_eq!(x, value);
}
Expand Down
1 change: 1 addition & 0 deletions src/libserialize/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Core encoding and decoding interfaces.
#![feature(specialization)]
#![feature(never_type)]
#![feature(nll)]
#![feature(ptr_wrapping_offset_from)]
#![cfg_attr(test, feature(test))]

pub use self::serialize::{Decoder, Encoder, Decodable, Encodable};
Expand Down
48 changes: 22 additions & 26 deletions src/libserialize/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,59 +157,55 @@ impl Encoder {
// -----------------------------------------------------------------------------

pub struct Decoder<'a> {
pub data: &'a [u8],
position: usize,
pub original_data: &'a [u8],
data: &'a [u8],
}

impl<'a> Decoder<'a> {
#[inline]
pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> {
pub fn new(data: &'a [u8], pos: usize) -> Decoder<'a> {
Decoder {
data,
position,
original_data: data,
data: &data[pos..],
}
}

#[inline]
pub fn position(&self) -> usize {
self.position
pub fn data(&self) -> &[u8] {
self.data
}

#[inline]
pub fn set_position(&mut self, pos: usize) {
self.position = pos
pub fn position(&self) -> usize {
self.data.as_ptr().wrapping_offset_from(self.original_data.as_ptr()) as usize
}

#[inline]
pub fn advance(&mut self, bytes: usize) {
self.position += bytes;
self.data = &self.data[bytes..];
}

#[inline]
pub fn read_raw_bytes(&mut self, s: &mut [u8]) -> Result<(), String> {
let start = self.position;
let end = start + s.len();

s.copy_from_slice(&self.data[start..end]);

self.position = end;
s.copy_from_slice(&self.data[..s.len()]);
self.advance(s.len());

Ok(())
}
}

macro_rules! read_uleb128 {
($dec:expr, $t:ty, $fun:ident) => ({
let (value, bytes_read) = leb128::$fun(&$dec.data[$dec.position ..]);
$dec.position += bytes_read;
let (value, bytes_read) = leb128::$fun(&$dec.data);
$dec.advance(bytes_read);
Ok(value)
})
}

macro_rules! read_sleb128 {
($dec:expr, $t:ty) => ({
let (value, bytes_read) = read_signed_leb128($dec.data, $dec.position);
$dec.position += bytes_read;
let (value, bytes_read) = read_signed_leb128($dec.data);
$dec.advance(bytes_read);
Ok(value as $t)
})
}
Expand Down Expand Up @@ -245,8 +241,8 @@ impl<'a> serialize::Decoder for Decoder<'a> {

#[inline]
fn read_u8(&mut self) -> Result<u8, Self::Error> {
let value = self.data[self.position];
self.position += 1;
let value = self.data[0];
self.advance(1);
Ok(value)
}

Expand Down Expand Up @@ -277,8 +273,8 @@ impl<'a> serialize::Decoder for Decoder<'a> {

#[inline]
fn read_i8(&mut self) -> Result<i8, Self::Error> {
let as_u8 = self.data[self.position];
self.position += 1;
let as_u8 = self.data[0];
self.advance(1);
unsafe { Ok(::std::mem::transmute(as_u8)) }
}

Expand Down Expand Up @@ -314,8 +310,8 @@ impl<'a> serialize::Decoder for Decoder<'a> {
#[inline]
fn read_str(&mut self) -> Result<Cow<'_, str>, Self::Error> {
let len = self.read_usize()?;
let s = ::std::str::from_utf8(&self.data[self.position..self.position + len]).unwrap();
self.position += len;
let s = ::std::str::from_utf8(&self.data[..len]).unwrap();
self.advance(len);
Ok(Cow::Borrowed(s))
}

Expand Down