Skip to content

Commit 864dd3c

Browse files
Alex Gaynoralex
Alex Gaynor
authored andcommitted
Refactor how opaque::Decoder represents its contents
Instead of (&[u8], position) simply store the &[u8] and reslice. This was originally written for #58475, to see if removing the arithmetic helped with avoiding integer overflow checks, however I think the result is slightly more readable in general -- specifically the removal of set_position is a nice win. I think this might be a hair faster even without the changes in #58475, but I haven't measured that.
1 parent aef540b commit 864dd3c

File tree

6 files changed

+37
-41
lines changed

6 files changed

+37
-41
lines changed

src/librustc/ty/query/on_disk_cache.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,21 @@ impl AbsoluteBytePos {
105105

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

111111
// Wrapping in a scope so we can borrow `data`
112112
let footer: Footer = {
113-
let mut decoder = opaque::Decoder::new(&data[..], start_pos);
114-
115113
// Decode the *position* of the footer which can be found in the
116114
// last 8 bytes of the file.
117-
decoder.set_position(data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
115+
let mut decoder = opaque::Decoder::new(
116+
&data, data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
118117
let query_result_index_pos = IntEncodedWithFixedSize::decode(&mut decoder)
119118
.expect("Error while trying to decode query result index position.")
120119
.0 as usize;
121120

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

541540
#[inline]
542541
fn peek_byte(&self) -> u8 {
543-
self.opaque.data[self.opaque.position()]
542+
self.opaque.data()[0]
544543
}
545544

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

574-
let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
573+
let new_opaque = opaque::Decoder::new(&self.opaque.original_data, pos);
575574
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
576575
let r = f(self);
577576
self.opaque = old_opaque;

src/librustc_incremental/persist/load.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub fn load_query_result_cache<'sess>(sess: &'sess Session) -> OnDiskCache<'sess
200200
}
201201

202202
match load_data(sess.opts.debugging_opts.incremental_info, &query_cache_path(sess)) {
203-
LoadResult::Ok{ data: (bytes, start_pos) } => OnDiskCache::new(sess, bytes, start_pos),
203+
LoadResult::Ok{ data: (bytes, _) } => OnDiskCache::new(sess, bytes),
204204
_ => OnDiskCache::new_empty(sess.source_map())
205205
}
206206
}

src/librustc_metadata/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
179179

180180
#[inline]
181181
fn peek_byte(&self) -> u8 {
182-
self.opaque.data[self.opaque.position()]
182+
self.opaque.data()[0]
183183
}
184184

185185
#[inline]
@@ -212,7 +212,7 @@ impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
212212
fn with_position<F, R>(&mut self, pos: usize, f: F) -> R
213213
where F: FnOnce(&mut Self) -> R
214214
{
215-
let new_opaque = opaque::Decoder::new(self.opaque.data, pos);
215+
let new_opaque = opaque::Decoder::new(self.opaque.original_data, pos);
216216
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
217217
let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
218218
let r = f(self);

src/libserialize/leb128.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ pub fn write_signed_leb128(out: &mut Vec<u8>, value: i128) {
114114
}
115115

116116
#[inline]
117-
pub fn read_signed_leb128(data: &[u8], start_position: usize) -> (i128, usize) {
117+
pub fn read_signed_leb128(data: &[u8]) -> (i128, usize) {
118118
let mut result = 0;
119119
let mut shift = 0;
120-
let mut position = start_position;
120+
let mut position = 0;
121121
let mut byte;
122122

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

139-
(result, position - start_position)
139+
(result, position)
140140
}
141141

142142
macro_rules! impl_test_unsigned_leb128 {
@@ -176,7 +176,7 @@ fn test_signed_leb128() {
176176
}
177177
let mut pos = 0;
178178
for &x in &values {
179-
let (value, bytes_read) = read_signed_leb128(&mut stream, pos);
179+
let (value, bytes_read) = read_signed_leb128(&mut stream[pos..]);
180180
pos += bytes_read;
181181
assert_eq!(x, value);
182182
}

src/libserialize/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Core encoding and decoding interfaces.
1515
#![feature(specialization)]
1616
#![feature(never_type)]
1717
#![feature(nll)]
18+
#![feature(ptr_wrapping_offset_from)]
1819
#![cfg_attr(test, feature(test))]
1920

2021
pub use self::serialize::{Decoder, Encoder, Decodable, Encodable};

src/libserialize/opaque.rs

+22-26
Original file line numberDiff line numberDiff line change
@@ -157,59 +157,55 @@ impl Encoder {
157157
// -----------------------------------------------------------------------------
158158

159159
pub struct Decoder<'a> {
160-
pub data: &'a [u8],
161-
position: usize,
160+
pub original_data: &'a [u8],
161+
data: &'a [u8],
162162
}
163163

164164
impl<'a> Decoder<'a> {
165165
#[inline]
166-
pub fn new(data: &'a [u8], position: usize) -> Decoder<'a> {
166+
pub fn new(data: &'a [u8], pos: usize) -> Decoder<'a> {
167167
Decoder {
168-
data,
169-
position,
168+
original_data: data,
169+
data: &data[pos..],
170170
}
171171
}
172172

173173
#[inline]
174-
pub fn position(&self) -> usize {
175-
self.position
174+
pub fn data(&self) -> &[u8] {
175+
self.data
176176
}
177177

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

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

188188
#[inline]
189189
pub fn read_raw_bytes(&mut self, s: &mut [u8]) -> Result<(), String> {
190-
let start = self.position;
191-
let end = start + s.len();
192-
193-
s.copy_from_slice(&self.data[start..end]);
194-
195-
self.position = end;
190+
s.copy_from_slice(&self.data[..s.len()]);
191+
self.advance(s.len());
196192

197193
Ok(())
198194
}
199195
}
200196

201197
macro_rules! read_uleb128 {
202198
($dec:expr, $t:ty, $fun:ident) => ({
203-
let (value, bytes_read) = leb128::$fun(&$dec.data[$dec.position ..]);
204-
$dec.position += bytes_read;
199+
let (value, bytes_read) = leb128::$fun(&$dec.data);
200+
$dec.advance(bytes_read);
205201
Ok(value)
206202
})
207203
}
208204

209205
macro_rules! read_sleb128 {
210206
($dec:expr, $t:ty) => ({
211-
let (value, bytes_read) = read_signed_leb128($dec.data, $dec.position);
212-
$dec.position += bytes_read;
207+
let (value, bytes_read) = read_signed_leb128($dec.data);
208+
$dec.advance(bytes_read);
213209
Ok(value as $t)
214210
})
215211
}
@@ -245,8 +241,8 @@ impl<'a> serialize::Decoder for Decoder<'a> {
245241

246242
#[inline]
247243
fn read_u8(&mut self) -> Result<u8, Self::Error> {
248-
let value = self.data[self.position];
249-
self.position += 1;
244+
let value = self.data[0];
245+
self.advance(1);
250246
Ok(value)
251247
}
252248

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

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

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

0 commit comments

Comments
 (0)