Skip to content

Commit

Permalink
Auto merge of #50866 - michaelwoerister:relocations-in-vec, r=<try>
Browse files Browse the repository at this point in the history
[WIP] 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
  • Loading branch information
bors committed May 18, 2018
2 parents df40e61 + 34b4f03 commit cff7e0f
Show file tree
Hide file tree
Showing 5 changed files with 558 additions and 26 deletions.
38 changes: 34 additions & 4 deletions src/librustc/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub use self::error::{EvalError, EvalResult, EvalErrorKind, AssertMessage};

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

use std::collections::BTreeMap;
use std::fmt;
use mir;
use hir::def_id::DefId;
Expand All @@ -21,8 +20,10 @@ use ty::layout::{self, Align, HasDataLayout};
use middle::region;
use std::iter;
use std::io;
use std::ops::{Deref, DerefMut};
use syntax::ast::Mutability;
use rustc_serialize::{Encoder, Decoder, Decodable, Encodable};
use rustc_data_structures::sorted_map::SortedMap;
use byteorder::{WriteBytesExt, ReadBytesExt, LittleEndian, BigEndian};

#[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)]
Expand Down Expand Up @@ -244,7 +245,7 @@ pub struct Allocation {
pub bytes: Vec<u8>,
/// Maps from byte addresses to allocations.
/// Only the first byte of a pointer is inserted into the map.
pub relocations: BTreeMap<u64, AllocId>,
pub relocations: Relocations,
/// Denotes undefined memory. Reading from undefined memory is forbidden in miri
pub undef_mask: UndefMask,
/// The alignment of the allocation to detect unaligned reads.
Expand All @@ -261,7 +262,7 @@ impl Allocation {
undef_mask.grow(slice.len() as u64, true);
Self {
bytes: slice.to_owned(),
relocations: BTreeMap::new(),
relocations: Relocations::new(),
undef_mask,
align,
runtime_mutability: Mutability::Immutable,
Expand All @@ -276,7 +277,7 @@ impl Allocation {
assert_eq!(size as usize as u64, size);
Allocation {
bytes: vec![0; size as usize],
relocations: BTreeMap::new(),
relocations: Relocations::new(),
undef_mask: UndefMask::new(size),
align,
runtime_mutability: Mutability::Immutable,
Expand All @@ -286,6 +287,35 @@ impl Allocation {

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

#[derive(Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
pub struct Relocations(SortedMap<u64, AllocId>);

impl Relocations {
pub fn new() -> Relocations {
Relocations(SortedMap::new())
}

// The caller must guarantee that the given relocations are already sorted
// by address and contain no duplicates.
pub fn from_presorted(r: Vec<(u64, AllocId)>) -> Relocations {
Relocations(SortedMap::from_presorted_elements(r))
}
}

impl Deref for Relocations {
type Target = SortedMap<u64, AllocId>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for Relocations {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

////////////////////////////////////////////////////////////////////////////////
// Methods to access integers in the target endianness
////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn const_alloc_to_llvm(cx: &CodegenCx, alloc: &Allocation) -> ValueRef {
let pointer_size = layout.pointer_size.bytes() as usize;

let mut next_offset = 0;
for (&offset, &alloc_id) in &alloc.relocations {
for &(offset, alloc_id) in alloc.relocations.iter() {
assert_eq!(offset as usize as u64, offset);
let offset = offset as usize;
if offset > next_offset {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_data_structures/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub mod control_flow_graph;
pub mod flock;
pub mod sync;
pub mod owning_ref;
pub mod sorted_map;

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

Expand Down
Loading

0 comments on commit cff7e0f

Please sign in to comment.