Skip to content

Commit 4f43709

Browse files
committed
Auto merge of #1202 - RalfJung:symbols, r=RalfJung
switch extern_statics map to symbols, and use FxHashMap everywhere Cc @eddyb
2 parents 3311756 + 92a28f8 commit 4f43709

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

src/intptrcast.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::cell::RefCell;
22
use std::cmp::max;
3-
use std::collections::{hash_map::Entry, HashMap};
3+
use std::collections::hash_map::Entry;
44

55
use rand::Rng;
66

7+
use rustc_data_structures::fx::FxHashMap;
78
use rustc::ty::layout::HasDataLayout;
89
use rustc_mir::interpret::{AllocCheck, AllocId, InterpResult, Memory, Machine, Pointer, PointerArithmetic};
910
use rustc_target::abi::Size;
@@ -21,7 +22,7 @@ pub struct GlobalState {
2122
/// `AllocExtra` because function pointers also have a base address, and
2223
/// they do not have an `AllocExtra`.
2324
/// This is the inverse of `int_to_ptr_map`.
24-
pub base_addr: HashMap<AllocId, u64>,
25+
pub base_addr: FxHashMap<AllocId, u64>,
2526
/// This is used as a memory address when a new pointer is casted to an integer. It
2627
/// is always larger than any address that was previously made part of a block.
2728
pub next_base_addr: u64,
@@ -31,7 +32,7 @@ impl Default for GlobalState {
3132
fn default() -> Self {
3233
GlobalState {
3334
int_to_ptr_map: Vec::default(),
34-
base_addr: HashMap::default(),
35+
base_addr: FxHashMap::default(),
3536
next_base_addr: STACK_ADDR,
3637
}
3738
}

src/machine.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
44
use std::borrow::Cow;
55
use std::cell::RefCell;
6-
use std::collections::HashMap;
76
use std::num::NonZeroU64;
87
use std::rc::Rc;
98

109
use rand::rngs::StdRng;
1110

11+
use rustc_data_structures::fx::FxHashMap;
1212
use rustc::mir;
1313
use rustc::ty::{
1414
self,
1515
layout::{LayoutOf, Size},
1616
Ty,
1717
};
1818
use rustc_ast::attr;
19-
use rustc_span::{source_map::Span, symbol::sym};
19+
use rustc_span::{source_map::Span, symbol::{sym, Symbol}};
2020

2121
use crate::*;
2222

@@ -75,7 +75,7 @@ pub struct MemoryExtra {
7575
pub intptrcast: intptrcast::MemoryExtra,
7676

7777
/// Mapping extern static names to their canonical allocation.
78-
pub(crate) extern_statics: HashMap<&'static str, AllocId>,
78+
pub(crate) extern_statics: FxHashMap<Symbol, AllocId>,
7979

8080
/// The random number generator used for resolving non-determinism.
8181
/// Needs to be queried by ptr_to_int, hence needs interior mutability.
@@ -92,7 +92,7 @@ impl MemoryExtra {
9292
MemoryExtra {
9393
stacked_borrows,
9494
intptrcast: Default::default(),
95-
extern_statics: HashMap::default(),
95+
extern_statics: FxHashMap::default(),
9696
rng: RefCell::new(rng),
9797
}
9898
}
@@ -111,7 +111,7 @@ impl MemoryExtra {
111111
this.memory
112112
.extra
113113
.extern_statics
114-
.insert("__cxa_thread_atexit_impl", place.ptr.assert_ptr().alloc_id)
114+
.insert(Symbol::intern("__cxa_thread_atexit_impl"), place.ptr.assert_ptr().alloc_id)
115115
.unwrap_none();
116116
}
117117
_ => {} // No "extern statics" supported on this platform
@@ -310,11 +310,11 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'tcx> {
310310
};
311311
let attrs = tcx.get_attrs(def_id);
312312
let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
313-
Some(name) => name.as_str(),
314-
None => tcx.item_name(def_id).as_str(),
313+
Some(name) => name,
314+
None => tcx.item_name(def_id),
315315
};
316316
// Check if we know this one.
317-
if let Some(canonical_id) = mem.extra.extern_statics.get(&*link_name) {
317+
if let Some(canonical_id) = mem.extra.extern_statics.get(&link_name) {
318318
trace!("canonical_alloc_id: {:?} ({}) -> {:?}", id, link_name, canonical_id);
319319
*canonical_id
320320
} else {

src/mono_hash_map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! This is a "monotonic `HashMap`": A `HashMap` that, when shared, can be pushed to but not
1+
//! This is a "monotonic `FxHashMap`": A `FxHashMap` that, when shared, can be pushed to but not
22
//! otherwise mutated. We also box items in the map. This means we can safely provide
3-
//! shared references into existing items in the `HashMap`, because they will not be dropped
3+
//! shared references into existing items in the `FxHashMap`, because they will not be dropped
44
//! (from being removed) or moved (because they are boxed).
55
//! The API is is completely tailored to what `memory.rs` needs. It is still in
66
//! a separate file to minimize the amount of code that has to care about the unsafety.
@@ -21,8 +21,8 @@ impl<K: Hash + Eq, V> MonoHashMap<K, V> {
2121
/// This function exists for priroda to be able to iterate over all evaluator memory.
2222
///
2323
/// The function is somewhat roundabout with the closure argument because internally the
24-
/// `MonoHashMap` uses a `RefCell`. When iterating over the `HashMap` inside the `RefCell`,
25-
/// we need to keep a borrow to the `HashMap` inside the iterator. The borrow is only alive
24+
/// `MonoHashMap` uses a `RefCell`. When iterating over the `FxHashMap` inside the `RefCell`,
25+
/// we need to keep a borrow to the `FxHashMap` inside the iterator. The borrow is only alive
2626
/// as long as the `Ref` returned by `RefCell::borrow()` is alive. So we can't return the
2727
/// iterator, as that would drop the `Ref`. We can't return both, as it's not possible in Rust
2828
/// to have a struct/tuple with a field that refers to another field.

src/shims/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
use std::collections::HashMap;
21
use std::ffi::{OsString, OsStr};
32
use std::env;
43

54
use crate::stacked_borrows::Tag;
65
use crate::*;
76

7+
use rustc_data_structures::fx::FxHashMap;
88
use rustc::ty::layout::Size;
99
use rustc_mir::interpret::Pointer;
1010

1111
#[derive(Default)]
1212
pub struct EnvVars {
1313
/// Stores pointers to the environment variables. These variables must be stored as
1414
/// null-terminated C strings with the `"{name}={value}"` format.
15-
map: HashMap<OsString, Pointer<Tag>>,
15+
map: FxHashMap<OsString, Pointer<Tag>>,
1616
}
1717

1818
impl EnvVars {

src/shims/fs.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::collections::BTreeMap;
2-
use std::collections::HashMap;
32
use std::convert::{TryFrom, TryInto};
43
use std::fs::{read_dir, remove_dir, remove_file, rename, DirBuilder, File, FileType, OpenOptions, ReadDir};
54
use std::io::{Read, Seek, SeekFrom, Write};
65
use std::path::PathBuf;
76
use std::time::SystemTime;
87

8+
use rustc_data_structures::fx::FxHashMap;
99
use rustc::ty::layout::{Align, LayoutOf, Size};
1010

1111
use crate::stacked_borrows::Tag;
@@ -212,10 +212,10 @@ pub struct DirHandler {
212212
/// When opendir is called, a directory iterator is created on the host for the target
213213
/// directory, and an entry is stored in this hash map, indexed by an ID which represents
214214
/// the directory stream. When readdir is called, the directory stream ID is used to look up
215-
/// the corresponding ReadDir iterator from this HashMap, and information from the next
215+
/// the corresponding ReadDir iterator from this map, and information from the next
216216
/// directory entry is returned. When closedir is called, the ReadDir iterator is removed from
217-
/// this HashMap.
218-
streams: HashMap<u64, ReadDir>,
217+
/// the map.
218+
streams: FxHashMap<u64, ReadDir>,
219219
/// ID number to be used by the next call to opendir
220220
next_id: u64,
221221
}
@@ -232,7 +232,7 @@ impl DirHandler {
232232
impl Default for DirHandler {
233233
fn default() -> DirHandler {
234234
DirHandler {
235-
streams: HashMap::new(),
235+
streams: FxHashMap::default(),
236236
// Skip 0 as an ID, because it looks like a null pointer to libc
237237
next_id: 1,
238238
}

src/stacked_borrows.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
//! for further information.
33
44
use std::cell::RefCell;
5-
use std::collections::{HashMap, HashSet};
65
use std::fmt;
76
use std::num::NonZeroU64;
87
use std::rc::Rc;
98

9+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc::mir::RetagKind;
1111
use rustc::ty::{self, layout::Size};
1212
use rustc_hir::Mutability;
@@ -96,11 +96,11 @@ pub struct GlobalState {
9696
/// Table storing the "base" tag for each allocation.
9797
/// The base tag is the one used for the initial pointer.
9898
/// We need this in a separate table to handle cyclic statics.
99-
base_ptr_ids: HashMap<AllocId, Tag>,
99+
base_ptr_ids: FxHashMap<AllocId, Tag>,
100100
/// Next unused call ID (for protectors).
101101
next_call_id: CallId,
102102
/// Those call IDs corresponding to functions that are still running.
103-
active_calls: HashSet<CallId>,
103+
active_calls: FxHashSet<CallId>,
104104
/// The id to trace in this execution run
105105
tracked_pointer_tag: Option<PtrId>,
106106
}
@@ -153,9 +153,9 @@ impl GlobalState {
153153
pub fn new(tracked_pointer_tag: Option<PtrId>) -> Self {
154154
GlobalState {
155155
next_ptr_id: NonZeroU64::new(1).unwrap(),
156-
base_ptr_ids: HashMap::default(),
156+
base_ptr_ids: FxHashMap::default(),
157157
next_call_id: NonZeroU64::new(1).unwrap(),
158-
active_calls: HashSet::default(),
158+
active_calls: FxHashSet::default(),
159159
tracked_pointer_tag,
160160
}
161161
}

0 commit comments

Comments
 (0)