From 92a28f8d8f139d0b6b0f8efa009fee0e72103d1e Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 2 Mar 2020 22:36:15 +0100 Subject: [PATCH] HashMap -> FxHashMap --- src/intptrcast.rs | 7 ++++--- src/machine.rs | 6 +++--- src/mono_hash_map.rs | 8 ++++---- src/shims/env.rs | 4 ++-- src/shims/fs.rs | 10 +++++----- src/stacked_borrows.rs | 10 +++++----- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/intptrcast.rs b/src/intptrcast.rs index 375ebf09967d..91d9f4e84ee3 100644 --- a/src/intptrcast.rs +++ b/src/intptrcast.rs @@ -1,9 +1,10 @@ use std::cell::RefCell; use std::cmp::max; -use std::collections::{hash_map::Entry, HashMap}; +use std::collections::hash_map::Entry; use rand::Rng; +use rustc_data_structures::fx::FxHashMap; use rustc::ty::layout::HasDataLayout; use rustc_mir::interpret::{AllocCheck, AllocId, InterpResult, Memory, Machine, Pointer, PointerArithmetic}; use rustc_target::abi::Size; @@ -21,7 +22,7 @@ pub struct GlobalState { /// `AllocExtra` because function pointers also have a base address, and /// they do not have an `AllocExtra`. /// This is the inverse of `int_to_ptr_map`. - pub base_addr: HashMap, + pub base_addr: FxHashMap, /// This is used as a memory address when a new pointer is casted to an integer. It /// is always larger than any address that was previously made part of a block. pub next_base_addr: u64, @@ -31,7 +32,7 @@ impl Default for GlobalState { fn default() -> Self { GlobalState { int_to_ptr_map: Vec::default(), - base_addr: HashMap::default(), + base_addr: FxHashMap::default(), next_base_addr: STACK_ADDR, } } diff --git a/src/machine.rs b/src/machine.rs index c8705eb29e8c..0df567a2fa51 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -3,12 +3,12 @@ use std::borrow::Cow; use std::cell::RefCell; -use std::collections::HashMap; use std::num::NonZeroU64; use std::rc::Rc; use rand::rngs::StdRng; +use rustc_data_structures::fx::FxHashMap; use rustc::mir; use rustc::ty::{ self, @@ -75,7 +75,7 @@ pub struct MemoryExtra { pub intptrcast: intptrcast::MemoryExtra, /// Mapping extern static names to their canonical allocation. - pub(crate) extern_statics: HashMap, + pub(crate) extern_statics: FxHashMap, /// The random number generator used for resolving non-determinism. /// Needs to be queried by ptr_to_int, hence needs interior mutability. @@ -92,7 +92,7 @@ impl MemoryExtra { MemoryExtra { stacked_borrows, intptrcast: Default::default(), - extern_statics: HashMap::default(), + extern_statics: FxHashMap::default(), rng: RefCell::new(rng), } } diff --git a/src/mono_hash_map.rs b/src/mono_hash_map.rs index 124ae6876026..fc33126aa2e7 100644 --- a/src/mono_hash_map.rs +++ b/src/mono_hash_map.rs @@ -1,6 +1,6 @@ -//! This is a "monotonic `HashMap`": A `HashMap` that, when shared, can be pushed to but not +//! This is a "monotonic `FxHashMap`": A `FxHashMap` that, when shared, can be pushed to but not //! otherwise mutated. We also box items in the map. This means we can safely provide -//! shared references into existing items in the `HashMap`, because they will not be dropped +//! shared references into existing items in the `FxHashMap`, because they will not be dropped //! (from being removed) or moved (because they are boxed). //! The API is is completely tailored to what `memory.rs` needs. It is still in //! a separate file to minimize the amount of code that has to care about the unsafety. @@ -21,8 +21,8 @@ impl MonoHashMap { /// This function exists for priroda to be able to iterate over all evaluator memory. /// /// The function is somewhat roundabout with the closure argument because internally the - /// `MonoHashMap` uses a `RefCell`. When iterating over the `HashMap` inside the `RefCell`, - /// we need to keep a borrow to the `HashMap` inside the iterator. The borrow is only alive + /// `MonoHashMap` uses a `RefCell`. When iterating over the `FxHashMap` inside the `RefCell`, + /// we need to keep a borrow to the `FxHashMap` inside the iterator. The borrow is only alive /// as long as the `Ref` returned by `RefCell::borrow()` is alive. So we can't return the /// iterator, as that would drop the `Ref`. We can't return both, as it's not possible in Rust /// to have a struct/tuple with a field that refers to another field. diff --git a/src/shims/env.rs b/src/shims/env.rs index 10f749216541..79f0c5b3d902 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -1,10 +1,10 @@ -use std::collections::HashMap; use std::ffi::{OsString, OsStr}; use std::env; use crate::stacked_borrows::Tag; use crate::*; +use rustc_data_structures::fx::FxHashMap; use rustc::ty::layout::Size; use rustc_mir::interpret::Pointer; @@ -12,7 +12,7 @@ use rustc_mir::interpret::Pointer; pub struct EnvVars { /// Stores pointers to the environment variables. These variables must be stored as /// null-terminated C strings with the `"{name}={value}"` format. - map: HashMap>, + map: FxHashMap>, } impl EnvVars { diff --git a/src/shims/fs.rs b/src/shims/fs.rs index cc8f18af7d61..8a48518fa9bc 100644 --- a/src/shims/fs.rs +++ b/src/shims/fs.rs @@ -1,11 +1,11 @@ use std::collections::BTreeMap; -use std::collections::HashMap; use std::convert::{TryFrom, TryInto}; use std::fs::{read_dir, remove_dir, remove_file, rename, DirBuilder, File, FileType, OpenOptions, ReadDir}; use std::io::{Read, Seek, SeekFrom, Write}; use std::path::PathBuf; use std::time::SystemTime; +use rustc_data_structures::fx::FxHashMap; use rustc::ty::layout::{Align, LayoutOf, Size}; use crate::stacked_borrows::Tag; @@ -212,10 +212,10 @@ pub struct DirHandler { /// When opendir is called, a directory iterator is created on the host for the target /// directory, and an entry is stored in this hash map, indexed by an ID which represents /// the directory stream. When readdir is called, the directory stream ID is used to look up - /// the corresponding ReadDir iterator from this HashMap, and information from the next + /// the corresponding ReadDir iterator from this map, and information from the next /// directory entry is returned. When closedir is called, the ReadDir iterator is removed from - /// this HashMap. - streams: HashMap, + /// the map. + streams: FxHashMap, /// ID number to be used by the next call to opendir next_id: u64, } @@ -232,7 +232,7 @@ impl DirHandler { impl Default for DirHandler { fn default() -> DirHandler { DirHandler { - streams: HashMap::new(), + streams: FxHashMap::default(), // Skip 0 as an ID, because it looks like a null pointer to libc next_id: 1, } diff --git a/src/stacked_borrows.rs b/src/stacked_borrows.rs index 9a511aaed577..6188d05780cb 100644 --- a/src/stacked_borrows.rs +++ b/src/stacked_borrows.rs @@ -2,11 +2,11 @@ //! for further information. use std::cell::RefCell; -use std::collections::{HashMap, HashSet}; use std::fmt; use std::num::NonZeroU64; use std::rc::Rc; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc::mir::RetagKind; use rustc::ty::{self, layout::Size}; use rustc_hir::Mutability; @@ -96,11 +96,11 @@ pub struct GlobalState { /// Table storing the "base" tag for each allocation. /// The base tag is the one used for the initial pointer. /// We need this in a separate table to handle cyclic statics. - base_ptr_ids: HashMap, + base_ptr_ids: FxHashMap, /// Next unused call ID (for protectors). next_call_id: CallId, /// Those call IDs corresponding to functions that are still running. - active_calls: HashSet, + active_calls: FxHashSet, /// The id to trace in this execution run tracked_pointer_tag: Option, } @@ -153,9 +153,9 @@ impl GlobalState { pub fn new(tracked_pointer_tag: Option) -> Self { GlobalState { next_ptr_id: NonZeroU64::new(1).unwrap(), - base_ptr_ids: HashMap::default(), + base_ptr_ids: FxHashMap::default(), next_call_id: NonZeroU64::new(1).unwrap(), - active_calls: HashSet::default(), + active_calls: FxHashSet::default(), tracked_pointer_tag, } }