Skip to content

Commit 1962a70

Browse files
committed
Auto merge of #50866 - michaelwoerister:relocations-in-vec, r=oli-obk
Use different datastructure for MIRI relocations This PR makes relocations in MIRI used a sorted vector instead of a `BTreeMap` which should make a few common operations more efficient. Let's see if that's true. r? @oli-obk
2 parents c3733a7 + 95fac99 commit 1962a70

File tree

5 files changed

+545
-26
lines changed

5 files changed

+545
-26
lines changed

src/librustc/mir/interpret/mod.rs

+34-4
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::BTreeMap;
1615
use std::fmt;
1716
use mir;
1817
use hir::def_id::DefId;
@@ -21,9 +20,11 @@ use ty::layout::{self, Align, HasDataLayout, Size};
2120
use middle::region;
2221
use std::iter;
2322
use std::io;
23+
use std::ops::{Deref, DerefMut};
2424
use std::hash::Hash;
2525
use syntax::ast::Mutability;
2626
use rustc_serialize::{Encoder, Decoder, Decodable, Encodable};
27+
use rustc_data_structures::sorted_map::SortedMap;
2728
use rustc_data_structures::fx::FxHashMap;
2829
use byteorder::{WriteBytesExt, ReadBytesExt, LittleEndian, BigEndian};
2930

@@ -341,7 +342,7 @@ pub struct Allocation {
341342
pub bytes: Vec<u8>,
342343
/// Maps from byte addresses to allocations.
343344
/// Only the first byte of a pointer is inserted into the map.
344-
pub relocations: BTreeMap<Size, AllocId>,
345+
pub relocations: Relocations,
345346
/// Denotes undefined memory. Reading from undefined memory is forbidden in miri
346347
pub undef_mask: UndefMask,
347348
/// The alignment of the allocation to detect unaligned reads.
@@ -358,7 +359,7 @@ impl Allocation {
358359
undef_mask.grow(Size::from_bytes(slice.len() as u64), true);
359360
Self {
360361
bytes: slice.to_owned(),
361-
relocations: BTreeMap::new(),
362+
relocations: Relocations::new(),
362363
undef_mask,
363364
align,
364365
runtime_mutability: Mutability::Immutable,
@@ -373,7 +374,7 @@ impl Allocation {
373374
assert_eq!(size.bytes() as usize as u64, size.bytes());
374375
Allocation {
375376
bytes: vec![0; size.bytes() as usize],
376-
relocations: BTreeMap::new(),
377+
relocations: Relocations::new(),
377378
undef_mask: UndefMask::new(size),
378379
align,
379380
runtime_mutability: Mutability::Immutable,
@@ -383,6 +384,35 @@ impl Allocation {
383384

384385
impl<'tcx> ::serialize::UseSpecializedDecodable for &'tcx Allocation {}
385386

387+
#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
388+
pub struct Relocations(SortedMap<Size, AllocId>);
389+
390+
impl Relocations {
391+
pub fn new() -> Relocations {
392+
Relocations(SortedMap::new())
393+
}
394+
395+
// The caller must guarantee that the given relocations are already sorted
396+
// by address and contain no duplicates.
397+
pub fn from_presorted(r: Vec<(Size, AllocId)>) -> Relocations {
398+
Relocations(SortedMap::from_presorted_elements(r))
399+
}
400+
}
401+
402+
impl Deref for Relocations {
403+
type Target = SortedMap<Size, AllocId>;
404+
405+
fn deref(&self) -> &Self::Target {
406+
&self.0
407+
}
408+
}
409+
410+
impl DerefMut for Relocations {
411+
fn deref_mut(&mut self) -> &mut Self::Target {
412+
&mut self.0
413+
}
414+
}
415+
386416
////////////////////////////////////////////////////////////////////////////////
387417
// Methods to access integers in the target endianness
388418
////////////////////////////////////////////////////////////////////////////////

src/librustc_codegen_llvm/mir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub fn const_alloc_to_llvm(cx: &CodegenCx, alloc: &Allocation) -> ValueRef {
8383
let pointer_size = layout.pointer_size.bytes() as usize;
8484

8585
let mut next_offset = 0;
86-
for (&offset, &alloc_id) in &alloc.relocations {
86+
for &(offset, alloc_id) in alloc.relocations.iter() {
8787
let offset = offset.bytes();
8888
assert_eq!(offset as usize as u64, offset);
8989
let offset = offset as usize;

src/librustc_data_structures/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub mod control_flow_graph;
7373
pub mod flock;
7474
pub mod sync;
7575
pub mod owning_ref;
76+
pub mod sorted_map;
7677

7778
pub struct OnDrop<F: Fn()>(pub F);
7879

0 commit comments

Comments
 (0)