Skip to content

Commit 1f67ba6

Browse files
committed
Rewrite MemDecoder around pointers not a slice
1 parent 39c6804 commit 1f67ba6

File tree

11 files changed

+174
-130
lines changed

11 files changed

+174
-130
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+5-20
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,6 @@ impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> {
373373
self.tcx()
374374
}
375375

376-
#[inline]
377-
fn peek_byte(&self) -> u8 {
378-
self.opaque.data[self.opaque.position()]
379-
}
380-
381-
#[inline]
382-
fn position(&self) -> usize {
383-
self.opaque.position()
384-
}
385-
386376
fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
387377
where
388378
F: FnOnce(&mut Self) -> Ty<'tcx>,
@@ -404,7 +394,7 @@ impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> {
404394
where
405395
F: FnOnce(&mut Self) -> R,
406396
{
407-
let new_opaque = MemDecoder::new(self.opaque.data, pos);
397+
let new_opaque = MemDecoder::new(self.opaque.data(), pos);
408398
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
409399
let old_state = mem::replace(&mut self.lazy_state, LazyState::NoNode);
410400
let r = f(self);
@@ -625,17 +615,12 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Symbol {
625615
SYMBOL_OFFSET => {
626616
// read str offset
627617
let pos = d.read_usize();
628-
let old_pos = d.opaque.position();
629618

630619
// move to str offset and read
631-
d.opaque.set_position(pos);
632-
let s = d.read_str();
633-
let sym = Symbol::intern(s);
634-
635-
// restore position
636-
d.opaque.set_position(old_pos);
637-
638-
sym
620+
d.opaque.with_position(pos, |d| {
621+
let s = d.read_str();
622+
Symbol::intern(s)
623+
})
639624
}
640625
SYMBOL_PREINTERNED => {
641626
let symbol_index = d.read_u32();

compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs

-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for DefPathHashMapRef<'tcx> {
4545

4646
impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for DefPathHashMapRef<'static> {
4747
fn decode(d: &mut DecodeContext<'a, 'tcx>) -> DefPathHashMapRef<'static> {
48-
// Import TyDecoder so we can access the DecodeContext::position() method
49-
use crate::rustc_middle::ty::codec::TyDecoder;
50-
5148
let len = d.read_usize();
5249
let pos = d.position();
5350
let o = slice_owned(d.blob().clone(), |blob| &blob[pos..pos + len]);

compiler/rustc_middle/src/ty/codec.rs

+10
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,16 @@ macro_rules! implement_ty_decoder {
519519
fn read_raw_bytes(&mut self, len: usize) -> &[u8] {
520520
self.opaque.read_raw_bytes(len)
521521
}
522+
523+
#[inline]
524+
fn position(&self) -> usize {
525+
self.opaque.position()
526+
}
527+
528+
#[inline]
529+
fn peek_byte(&self) -> u8 {
530+
self.opaque.peek_byte()
531+
}
522532
}
523533
}
524534
}

compiler/rustc_query_impl/src/on_disk_cache.rs

+12-44
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,12 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
169169

170170
// Decode the *position* of the footer, which can be found in the
171171
// last 8 bytes of the file.
172-
decoder.set_position(data.len() - IntEncodedWithFixedSize::ENCODED_SIZE);
173-
let footer_pos = IntEncodedWithFixedSize::decode(&mut decoder).0 as usize;
174-
172+
let footer_pos = decoder
173+
.with_position(decoder.len() - IntEncodedWithFixedSize::ENCODED_SIZE, |decoder| {
174+
IntEncodedWithFixedSize::decode(decoder).0 as usize
175+
});
175176
// Decode the file footer, which contains all the lookup tables, etc.
176-
decoder.set_position(footer_pos);
177-
178-
decode_tagged(&mut decoder, TAG_FILE_FOOTER)
177+
decoder.with_position(footer_pos, |decoder| decode_tagged(decoder, TAG_FILE_FOOTER))
179178
};
180179

181180
Self {
@@ -522,29 +521,13 @@ impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
522521
}
523522
}
524523

525-
trait DecoderWithPosition: Decoder {
526-
fn position(&self) -> usize;
527-
}
528-
529-
impl<'a> DecoderWithPosition for MemDecoder<'a> {
530-
fn position(&self) -> usize {
531-
self.position()
532-
}
533-
}
534-
535-
impl<'a, 'tcx> DecoderWithPosition for CacheDecoder<'a, 'tcx> {
536-
fn position(&self) -> usize {
537-
self.opaque.position()
538-
}
539-
}
540-
541524
// Decodes something that was encoded with `encode_tagged()` and verify that the
542525
// tag matches and the correct amount of bytes was read.
543526
fn decode_tagged<D, T, V>(decoder: &mut D, expected_tag: T) -> V
544527
where
545528
T: Decodable<D> + Eq + std::fmt::Debug,
546529
V: Decodable<D>,
547-
D: DecoderWithPosition,
530+
D: Decoder,
548531
{
549532
let start_pos = decoder.position();
550533

@@ -568,16 +551,6 @@ impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> {
568551
self.tcx
569552
}
570553

571-
#[inline]
572-
fn position(&self) -> usize {
573-
self.opaque.position()
574-
}
575-
576-
#[inline]
577-
fn peek_byte(&self) -> u8 {
578-
self.opaque.data[self.opaque.position()]
579-
}
580-
581554
fn cached_ty_for_shorthand<F>(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx>
582555
where
583556
F: FnOnce(&mut Self) -> Ty<'tcx>,
@@ -600,9 +573,9 @@ impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> {
600573
where
601574
F: FnOnce(&mut Self) -> R,
602575
{
603-
debug_assert!(pos < self.opaque.data.len());
576+
debug_assert!(pos < self.opaque.len());
604577

605-
let new_opaque = MemDecoder::new(self.opaque.data, pos);
578+
let new_opaque = MemDecoder::new(self.opaque.data(), pos);
606579
let old_opaque = mem::replace(&mut self.opaque, new_opaque);
607580
let r = f(self);
608581
self.opaque = old_opaque;
@@ -743,17 +716,12 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Symbol {
743716
SYMBOL_OFFSET => {
744717
// read str offset
745718
let pos = d.read_usize();
746-
let old_pos = d.opaque.position();
747719

748720
// move to str offset and read
749-
d.opaque.set_position(pos);
750-
let s = d.read_str();
751-
let sym = Symbol::intern(s);
752-
753-
// restore position
754-
d.opaque.set_position(old_pos);
755-
756-
sym
721+
d.opaque.with_position(pos, |d| {
722+
let s = d.read_str();
723+
Symbol::intern(s)
724+
})
757725
}
758726
SYMBOL_PREINTERNED => {
759727
let symbol_index = d.read_u32();

compiler/rustc_query_system/src/dep_graph/serialized.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -94,21 +94,19 @@ impl<'a, K: DepKind + Decodable<MemDecoder<'a>>> Decodable<MemDecoder<'a>>
9494
{
9595
#[instrument(level = "debug", skip(d))]
9696
fn decode(d: &mut MemDecoder<'a>) -> SerializedDepGraph<K> {
97-
let start_position = d.position();
98-
9997
// The last 16 bytes are the node count and edge count.
10098
debug!("position: {:?}", d.position());
101-
d.set_position(d.data.len() - 2 * IntEncodedWithFixedSize::ENCODED_SIZE);
99+
let (node_count, edge_count) =
100+
d.with_position(d.len() - 2 * IntEncodedWithFixedSize::ENCODED_SIZE, |d| {
101+
debug!("position: {:?}", d.position());
102+
let node_count = IntEncodedWithFixedSize::decode(d).0 as usize;
103+
let edge_count = IntEncodedWithFixedSize::decode(d).0 as usize;
104+
(node_count, edge_count)
105+
});
102106
debug!("position: {:?}", d.position());
103107

104-
let node_count = IntEncodedWithFixedSize::decode(d).0 as usize;
105-
let edge_count = IntEncodedWithFixedSize::decode(d).0 as usize;
106108
debug!(?node_count, ?edge_count);
107109

108-
debug!("position: {:?}", d.position());
109-
d.set_position(start_position);
110-
debug!("position: {:?}", d.position());
111-
112110
let mut nodes = IndexVec::with_capacity(node_count);
113111
let mut fingerprints = IndexVec::with_capacity(node_count);
114112
let mut edge_list_indices = IndexVec::with_capacity(node_count);

compiler/rustc_serialize/src/leb128.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use crate::opaque::MemDecoder;
2+
use crate::serialize::Decoder;
3+
14
/// Returns the length of the longest LEB128 encoding for `T`, assuming `T` is an integer type
25
pub const fn max_leb128_len<T>() -> usize {
36
// The longest LEB128 encoding for an integer uses 7 bits per byte.
@@ -50,21 +53,19 @@ impl_write_unsigned_leb128!(write_usize_leb128, usize);
5053
macro_rules! impl_read_unsigned_leb128 {
5154
($fn_name:ident, $int_ty:ty) => {
5255
#[inline]
53-
pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty {
56+
pub fn $fn_name(decoder: &mut MemDecoder<'_>) -> $int_ty {
5457
// The first iteration of this loop is unpeeled. This is a
5558
// performance win because this code is hot and integer values less
5659
// than 128 are very common, typically occurring 50-80% or more of
5760
// the time, even for u64 and u128.
58-
let byte = slice[*position];
59-
*position += 1;
61+
let byte = decoder.read_u8();
6062
if (byte & 0x80) == 0 {
6163
return byte as $int_ty;
6264
}
6365
let mut result = (byte & 0x7F) as $int_ty;
6466
let mut shift = 7;
6567
loop {
66-
let byte = slice[*position];
67-
*position += 1;
68+
let byte = decoder.read_u8();
6869
if (byte & 0x80) == 0 {
6970
result |= (byte as $int_ty) << shift;
7071
return result;
@@ -127,14 +128,13 @@ impl_write_signed_leb128!(write_isize_leb128, isize);
127128
macro_rules! impl_read_signed_leb128 {
128129
($fn_name:ident, $int_ty:ty) => {
129130
#[inline]
130-
pub fn $fn_name(slice: &[u8], position: &mut usize) -> $int_ty {
131+
pub fn $fn_name(decoder: &mut MemDecoder<'_>) -> $int_ty {
131132
let mut result = 0;
132133
let mut shift = 0;
133134
let mut byte;
134135

135136
loop {
136-
byte = slice[*position];
137-
*position += 1;
137+
byte = decoder.read_u8();
138138
result |= <$int_ty>::from(byte & 0x7F) << shift;
139139
shift += 7;
140140

compiler/rustc_serialize/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Core encoding and decoding interfaces.
1616
#![feature(maybe_uninit_slice)]
1717
#![feature(new_uninit)]
1818
#![feature(allocator_api)]
19+
#![feature(ptr_sub_ptr)]
1920
#![cfg_attr(test, feature(test))]
2021
#![allow(rustc::internal)]
2122
#![deny(rustc::untranslatable_diagnostic)]

0 commit comments

Comments
 (0)