Skip to content

Commit 398d282

Browse files
committed
Encode the allocation size so we can skip ahead
1 parent 94eef34 commit 398d282

File tree

5 files changed

+52
-32
lines changed

5 files changed

+52
-32
lines changed

src/librustc/mir/interpret/mod.rs

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub use self::error::{EvalError, EvalResult, EvalErrorKind, AssertMessage};
1212

1313
pub use self::value::{PrimVal, PrimValKind, Value, Pointer, ConstValue};
1414

15-
use std::collections::hash_map::Entry;
1615
use std::fmt;
1716
use mir;
1817
use hir::def_id::DefId;
@@ -195,7 +194,21 @@ where
195194
assert!(cache(encoder).insert(alloc_id, pos).is_none());
196195
trace!("encoding {:?} with {:#?}", alloc_id, alloc);
197196
AllocKind::Alloc.encode(encoder)?;
197+
198+
// Write placeholder for size
199+
let size_pos = encoder.position();
200+
0usize.encode(encoder)?;
201+
202+
let start = encoder.position();
198203
alloc.encode(encoder)?;
204+
let end = encoder.position();
205+
206+
// Overwrite the placeholder with the real size
207+
let size: usize = end - start;
208+
encoder.set_position(size_pos);
209+
size.encode(encoder)?;
210+
encoder.set_position(end);
211+
199212
}
200213
AllocType::Function(fn_instance) => {
201214
trace!("encoding {:?} with {:#?}", alloc_id, fn_instance);
@@ -230,40 +243,23 @@ pub fn specialized_decode_alloc_id<
230243
decoder.with_position(real_pos, AllocId::decode)
231244
},
232245
AllocKind::Alloc => {
233-
let alloc_id = {
234-
let mut cache = global_cache(decoder);
235-
let entry = cache.entry(pos);
236-
match entry {
237-
Entry::Occupied(occupied) => {
238-
let id = occupied.get().0;
239-
240-
// If the alloc id is fully loaded we just return here.
241-
if occupied.get().1 {
242-
return Ok(id)
243-
}
244-
245-
// It was only partially loaded.
246-
// This may be loading further up the stack
247-
// or concurrently in another thread.
248-
id
249-
}
250-
Entry::Vacant(vacant) => {
251-
// Insert early to allow recursive allocs
252-
let id = tcx.alloc_map.lock().reserve();
253-
vacant.insert((id, false));
254-
id
255-
}
256-
}
257-
};
258-
259-
// Insert early to allow recursive allocs and ot indicate that the current
260-
// session will eventually fully load this alloc id
261-
if !local_cache(decoder).insert(alloc_id) {
246+
// Read the size of the allocation.
247+
// Used to skip ahead if we don't need to decode this.
248+
let alloc_size = usize::decode(decoder)?;
249+
250+
let (alloc_id, fully_loaded) = *global_cache(decoder).entry(pos).or_insert_with(|| {
251+
// Create an id which is not fully loaded
252+
(tcx.alloc_map.lock().reserve(), false)
253+
});
254+
if fully_loaded || !local_cache(decoder).insert(alloc_id) {
262255
// We have started decoding this alloc id already, so just return it.
263256
// Its content is already filled in or will be filled in by functions
264257
// further up the stack.
265-
return Ok(alloc_id);
266-
258+
259+
// Skip the allocation
260+
let pos = decoder.position();
261+
decoder.set_position(pos + alloc_size);
262+
return Ok(alloc_id)
267263
}
268264

269265
let allocation = <&'tcx Allocation as Decodable>::decode(decoder)?;

src/librustc/ty/codec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ impl<'tcx> EncodableWithShorthand for ty::Predicate<'tcx> {
5252

5353
pub trait TyEncoder: Encoder {
5454
fn position(&self) -> usize;
55+
fn set_position(&mut self, usize);
5556
}
5657

5758
impl<'buf> TyEncoder for opaque::Encoder<'buf> {
5859
#[inline]
5960
fn position(&self) -> usize {
6061
self.position()
6162
}
63+
#[inline]
64+
fn set_position(&mut self, p: usize) {
65+
self.cursor.set_position(p as u64)
66+
}
6267
}
6368

6469
/// Encode the given value or a previously cached shorthand.
@@ -123,6 +128,8 @@ pub trait TyDecoder<'a, 'tcx: 'a>: Decoder {
123128

124129
fn position(&self) -> usize;
125130

131+
fn set_position(&mut self, usize);
132+
126133
fn cached_ty_for_shorthand<F>(&mut self,
127134
shorthand: usize,
128135
or_insert_with: F)

src/librustc/ty/maps/on_disk_cache.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,11 @@ impl<'a, 'tcx: 'a, 'x> ty_codec::TyDecoder<'a, 'tcx> for CacheDecoder<'a, 'tcx,
525525
self.opaque.position()
526526
}
527527

528+
#[inline]
529+
fn set_position(&mut self, p: usize) {
530+
self.opaque.set_position(p)
531+
}
532+
528533
#[inline]
529534
fn peek_byte(&self) -> u8 {
530535
self.opaque.data[self.opaque.position()]
@@ -860,6 +865,10 @@ impl<'enc, 'a, 'tcx, E> ty_codec::TyEncoder for CacheEncoder<'enc, 'a, 'tcx, E>
860865
fn position(&self) -> usize {
861866
self.encoder.position()
862867
}
868+
#[inline]
869+
fn set_position(&mut self, p: usize) {
870+
self.encoder.set_position(p)
871+
}
863872
}
864873

865874
impl<'enc, 'a, 'tcx, E> SpecializedEncoder<CrateNum> for CacheEncoder<'enc, 'a, 'tcx, E>

src/librustc_metadata/decoder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ impl<'a, 'tcx: 'a> TyDecoder<'a, 'tcx> for DecodeContext<'a, 'tcx> {
195195
self.opaque.position()
196196
}
197197

198+
#[inline]
199+
fn set_position(&mut self, p: usize) {
200+
self.opaque.set_position(p)
201+
}
202+
198203
fn cached_ty_for_shorthand<F>(&mut self,
199204
shorthand: usize,
200205
or_insert_with: F)

src/librustc_metadata/encoder.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> {
234234
fn position(&self) -> usize {
235235
self.opaque.position()
236236
}
237+
fn set_position(&mut self, p: usize) {
238+
self.opaque.set_position(p)
239+
}
237240
}
238241

239242
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

0 commit comments

Comments
 (0)