Skip to content

[Experiment] Replace HashMap with OrderMap #45282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/librustc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ rustc_errors = { path = "../librustc_errors" }
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
ordermap = "0.3.0"

# Note that these dependencies are a lie, they're just here to get linkage to
# work.
4 changes: 2 additions & 2 deletions src/librustc/dep_graph/query.rs
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ impl DepGraphQuery {
}

pub fn contains_node(&self, node: &DepNode) -> bool {
self.indices.contains_key(&node)
self.indices.contains_key(node)
}

pub fn nodes(&self) -> Vec<&DepNode> {
@@ -83,7 +83,7 @@ impl DepGraphQuery {

/// Just the outgoing edges from `node`.
pub fn immediate_successors(&self, node: &DepNode) -> Vec<&DepNode> {
if let Some(&index) = self.indices.get(&node) {
if let Some(&index) = self.indices.get(node) {
self.graph.successor_nodes(index)
.map(|s| self.graph.node_data(s))
.collect()
4 changes: 2 additions & 2 deletions src/librustc/ich/hcx.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ use session::Session;
use std::cmp::Ord;
use std::hash as std_hash;
use std::cell::RefCell;
use std::collections::HashMap;
use ordermap::OrderMap;

use syntax::ast;
use syntax::attr;
@@ -426,7 +426,7 @@ pub fn hash_stable_trait_impls<'gcx, W, R>(
hcx: &mut StableHashingContext<'gcx>,
hasher: &mut StableHasher<W>,
blanket_impls: &Vec<DefId>,
non_blanket_impls: &HashMap<fast_reject::SimplifiedType, Vec<DefId>, R>)
non_blanket_impls: &OrderMap<fast_reject::SimplifiedType, Vec<DefId>, R>)
where W: StableHasherResult,
R: std_hash::BuildHasher,
{
2 changes: 1 addition & 1 deletion src/librustc/infer/freshen.rs
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ use ty::subst::Substs;
use util::nodemap::FxHashMap;
use hir::def_id::DefId;

use std::collections::hash_map::Entry;
use ordermap::Entry;

use super::InferCtxt;
use super::unify_key::ToType;
2 changes: 1 addition & 1 deletion src/librustc/infer/region_inference/graphviz.rs
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ use infer::region_inference::RegionVarBindings;
use util::nodemap::{FxHashMap, FxHashSet};

use std::borrow::Cow;
use std::collections::hash_map::Entry::Vacant;
use ordermap::Entry::Vacant;
use std::env;
use std::fs::File;
use std::io;
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
@@ -62,6 +62,7 @@

#![recursion_limit="256"]

extern crate ordermap;
extern crate arena;
#[macro_use] extern crate bitflags;
extern crate core;
2 changes: 1 addition & 1 deletion src/librustc/traits/select.rs
Original file line number Diff line number Diff line change
@@ -1263,7 +1263,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
let trait_ref = &cache_fresh_trait_pred.0.trait_ref;
if self.can_use_global_caches(param_env) {
let cache = tcx.selection_cache.hashmap.borrow();
if let Some(cached) = cache.get(&trait_ref) {
if let Some(cached) = cache.get(trait_ref) {
return Some(cached.get(tcx));
}
}
12 changes: 8 additions & 4 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
@@ -53,15 +53,16 @@ use rustc_data_structures::accumulate_vec::AccumulateVec;
use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
StableHasher, StableHasherResult,
StableVec};
use rustc_data_structures;
use arena::{TypedArena, DroplessArena};
use rustc_const_math::{ConstInt, ConstUsize};
use rustc_data_structures::indexed_vec::IndexVec;
use std::any::Any;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use std::cmp::Ordering;
use std::collections::hash_map::{self, Entry};
use std::hash::{Hash, Hasher};
use ordermap::{self, Entry};
use std::hash::{BuildHasherDefault, Hash, Hasher};
use std::mem;
use std::ops::Deref;
use std::iter;
@@ -275,7 +276,7 @@ impl<'a, V> LocalTableInContext<'a, V> {
self.data.get(&id.local_id)
}

pub fn iter(&self) -> hash_map::Iter<hir::ItemLocalId, V> {
pub fn iter(&self) -> ordermap::Iter<hir::ItemLocalId, V> {
self.data.iter()
}
}
@@ -299,7 +300,10 @@ impl<'a, V> LocalTableInContextMut<'a, V> {
self.data.get_mut(&id.local_id)
}

pub fn entry(&mut self, id: hir::HirId) -> Entry<hir::ItemLocalId, V> {
pub fn entry(
&mut self,
id: hir::HirId
) -> Entry<hir::ItemLocalId, V, BuildHasherDefault<rustc_data_structures::fx::FxHasher>> {
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
self.data.entry(id.local_id)
}
1 change: 1 addition & 0 deletions src/librustc_data_structures/Cargo.toml
Original file line number Diff line number Diff line change
@@ -11,3 +11,4 @@ crate-type = ["dylib"]
[dependencies]
log = "0.3"
serialize = { path = "../libserialize" }
ordermap = "0.3.0"
9 changes: 6 additions & 3 deletions src/librustc_data_structures/fx.rs
Original file line number Diff line number Diff line change
@@ -8,17 +8,20 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::default::Default;
use std::hash::{Hasher, Hash, BuildHasherDefault};
use std::ops::BitXor;
use ordermap::OrderMap;

pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
// pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;

pub type FxHashMap<K, V> = OrderMap<K, V, BuildHasherDefault<FxHasher>>;

#[allow(non_snake_case)]
pub fn FxHashMap<K: Hash + Eq, V>() -> FxHashMap<K, V> {
HashMap::default()
Default::default()
}

#[allow(non_snake_case)]
1 change: 1 addition & 0 deletions src/librustc_data_structures/lib.rs
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ extern crate log;
extern crate serialize as rustc_serialize; // used by deriving
#[cfg(unix)]
extern crate libc;
extern crate ordermap;

pub use rustc_serialize::hex::ToHex;

2 changes: 1 addition & 1 deletion src/librustc_data_structures/obligation_forest/mod.rs
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
use fx::{FxHashMap, FxHashSet};

use std::cell::Cell;
use std::collections::hash_map::Entry;
use super::ordermap::Entry;
use std::fmt::Debug;
use std::hash;
use std::marker::PhantomData;
5 changes: 3 additions & 2 deletions src/librustc_data_structures/stable_hasher.rs
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ use std::marker::PhantomData;
use std::mem;
use blake2b::Blake2bHasher;
use rustc_serialize::leb128;
use ordermap::OrderMap;

fn write_unsigned_leb128_to_buf(buf: &mut [u8; 16], value: u64) -> usize {
leb128::write_unsigned_leb128_to(value as u128, |i, v| buf[i] = v)
@@ -482,7 +483,7 @@ impl<I: ::indexed_vec::Idx, CTX> HashStable<CTX> for ::indexed_set::IdxSetBuf<I>
impl_stable_hash_via_hash!(::std::path::Path);
impl_stable_hash_via_hash!(::std::path::PathBuf);

impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R>
impl<K, V, R, HCX> HashStable<HCX> for OrderMap<K, V, R>
where K: ToStableHashKey<HCX> + Eq + Hash,
V: HashStable<HCX>,
R: BuildHasher,
@@ -542,7 +543,7 @@ impl<K, HCX> HashStable<HCX> for ::std::collections::BTreeSet<K>
pub fn hash_stable_hashmap<HCX, K, V, R, SK, F, W>(
hcx: &mut HCX,
hasher: &mut StableHasher<W>,
map: &::std::collections::HashMap<K, V, R>,
map: &OrderMap<K, V, R>,
to_stable_hash_key: F)
where K: Eq + Hash,
V: HashStable<HCX>,
1 change: 1 addition & 0 deletions src/librustc_lint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -16,3 +16,4 @@ rustc_back = { path = "../librustc_back" }
rustc_const_eval = { path = "../librustc_const_eval" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
ordermap = "0.3.0"
1 change: 1 addition & 0 deletions src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ extern crate log;
extern crate rustc_back;
extern crate rustc_const_eval;
extern crate syntax_pos;
extern crate ordermap;

use rustc::lint;
use rustc::middle;
2 changes: 1 addition & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ use util::nodemap::FxHashMap;
use lint::{LateContext, EarlyContext, LintContext, LintArray};
use lint::{LintPass, EarlyLintPass, LateLintPass};

use std::collections::hash_map::Entry::{Occupied, Vacant};
use ordermap::Entry::{Occupied, Vacant};

use syntax::ast;
use syntax::attr;
1 change: 1 addition & 0 deletions src/librustc_metadata/Cargo.toml
Original file line number Diff line number Diff line change
@@ -21,3 +21,4 @@ serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_ext = { path = "../libsyntax_ext" }
syntax_pos = { path = "../libsyntax_pos" }
ordermap = "0.3.0"
4 changes: 2 additions & 2 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
@@ -290,7 +290,7 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
// external item to be parents).
visible_parent_map: |tcx, cnum| {
use std::collections::vec_deque::VecDeque;
use std::collections::hash_map::Entry;
use ordermap::Entry;

assert_eq!(cnum, LOCAL_CRATE);
let mut visible_parent_map: DefIdMap<DefId> = DefIdMap();
@@ -313,7 +313,7 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
}

match visible_parent_map.entry(child) {
Entry::Occupied(mut entry) => {
Entry::Occupied(entry) => {
// If `child` is defined in crate `cnum`, ensure
// that it is mapped to a parent in `cnum`.
if child.krate == cnum && entry.get().krate != cnum {
1 change: 1 addition & 0 deletions src/librustc_metadata/lib.rs
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ extern crate proc_macro;
extern crate rustc;
extern crate rustc_back;
extern crate rustc_data_structures;
extern crate ordermap;

mod diagnostics;

1 change: 1 addition & 0 deletions src/librustc_mir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -20,3 +20,4 @@ rustc_errors = { path = "../librustc_errors" }
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
ordermap = "0.3.0"
2 changes: 1 addition & 1 deletion src/librustc_mir/dataflow/impls/borrows.rs
Original file line number Diff line number Diff line change
@@ -117,7 +117,7 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
}

pub fn region_span(&self, region: &Region) -> Span {
let opt_span = self.region_span_map.get(region);
let opt_span = self.region_span_map.get(*region);
assert!(opt_span.is_some(), "end region not found for {:?}", region);
*opt_span.unwrap()
}
2 changes: 1 addition & 1 deletion src/librustc_mir/dataflow/move_paths/builder.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ use rustc_data_structures::indexed_vec::{IndexVec};

use syntax::codemap::DUMMY_SP;

use std::collections::hash_map::Entry;
use ordermap::Entry;
use std::mem;

use super::abs_domain::Lift;
1 change: 1 addition & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@ extern crate syntax_pos;
extern crate rustc_const_math;
extern crate rustc_const_eval;
extern crate core; // for NonZero
extern crate ordermap;

mod diagnostics;

1 change: 1 addition & 0 deletions src/librustc_passes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -16,3 +16,4 @@ rustc_const_math = { path = "../librustc_const_math" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
rustc_errors = { path = "../librustc_errors" }
ordermap = "0.3.0"
2 changes: 1 addition & 1 deletion src/librustc_passes/consts.rs
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ use syntax::ast;
use syntax_pos::{Span, DUMMY_SP};
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};

use std::collections::hash_map::Entry;
use ordermap::Entry;
use std::cmp::Ordering;

struct CheckCrateVisitor<'a, 'tcx: 'a> {
1 change: 1 addition & 0 deletions src/librustc_passes/lib.rs
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ extern crate log;
extern crate syntax;
extern crate syntax_pos;
extern crate rustc_errors as errors;
extern crate ordermap;

mod diagnostics;

1 change: 1 addition & 0 deletions src/librustc_trans/Cargo.toml
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ rustc_trans_utils = { path = "../librustc_trans_utils" }
serialize = { path = "../libserialize" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
ordermap = "0.3.0"

[target."cfg(windows)".dependencies]
cc = "1.0"
1 change: 1 addition & 0 deletions src/librustc_trans/lib.rs
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ extern crate rustc_trans_utils;
extern crate rustc_demangle;
extern crate jobserver;
extern crate num_cpus;
extern crate ordermap;

#[macro_use] extern crate log;
#[macro_use] extern crate syntax;
4 changes: 2 additions & 2 deletions src/librustc_trans/partitioning.rs
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ use rustc::middle::trans::{Linkage, Visibility};
use rustc::ty::{self, TyCtxt, InstanceDef};
use rustc::ty::item_path::characteristic_def_id_of_type;
use rustc::util::nodemap::{FxHashMap, FxHashSet};
use std::collections::hash_map::Entry;
use ordermap::Entry;
use syntax::ast::NodeId;
use syntax::symbol::{Symbol, InternedString};
use trans_item::{TransItem, TransItemExt, InstantiationMode};
@@ -398,7 +398,7 @@ fn merge_codegen_units<'tcx>(initial_partitioning: &mut PreInliningPartitioning<
let mut smallest = codegen_units.pop().unwrap();
let second_smallest = codegen_units.last_mut().unwrap();

for (k, v) in smallest.items_mut().drain() {
for (k, v) in smallest.items_mut().drain(..) {
second_smallest.items_mut().insert(k, v);
}
}
1 change: 1 addition & 0 deletions src/librustc_typeck/Cargo.toml
Original file line number Diff line number Diff line change
@@ -21,3 +21,4 @@ rustc_data_structures = { path = "../librustc_data_structures" }
rustc_platform_intrinsics = { path = "../librustc_platform_intrinsics" }
syntax_pos = { path = "../libsyntax_pos" }
rustc_errors = { path = "../librustc_errors" }
ordermap = "0.3.0"
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ use check::{FnCtxt, Expectation, Diverges};
use check::coercion::CoerceMany;
use util::nodemap::FxHashMap;

use std::collections::hash_map::Entry::{Occupied, Vacant};
use ordermap::Entry::{Occupied, Vacant};
use std::cmp;
use syntax::ast;
use syntax::codemap::Spanned;
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/generator_interior.rs
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ pub fn resolve_interior<'a, 'gcx, 'tcx>(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
let region_expr_count = visitor.region_scope_tree.body_expr_count(body_id).unwrap();
assert_eq!(region_expr_count, visitor.expr_count);

let mut types: Vec<_> = visitor.types.drain().collect();
let mut types: Vec<_> = visitor.types.drain(..).collect();

// Sort types by insertion order
types.sort_by_key(|t| t.1);
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
@@ -108,7 +108,7 @@ use util::common::{ErrorReported, indenter};
use util::nodemap::{DefIdMap, FxHashMap, NodeMap};

use std::cell::{Cell, RefCell, Ref, RefMut};
use std::collections::hash_map::Entry;
use ordermap::Entry;
use std::cmp;
use std::fmt::Display;
use std::mem::replace;
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/upvar.rs
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ use rustc::hir::def_id::DefIndex;
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
use rustc::util::nodemap::FxHashMap;

use std::collections::hash_map::Entry;
use ordermap::Entry;

impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
pub fn closure_analyze(&self, body: &'gcx hir::Body) {
2 changes: 1 addition & 1 deletion src/librustc_typeck/impl_wf_check.rs
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ use rustc::hir::itemlikevisit::ItemLikeVisitor;
use rustc::hir::def_id::DefId;
use rustc::ty::{self, TyCtxt};
use rustc::util::nodemap::{FxHashMap, FxHashSet};
use std::collections::hash_map::Entry::{Occupied, Vacant};
use ordermap::Entry::{Occupied, Vacant};

use syntax_pos::Span;

1 change: 1 addition & 0 deletions src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
@@ -90,6 +90,7 @@ extern crate rustc_back;
extern crate rustc_const_math;
extern crate rustc_data_structures;
extern crate rustc_errors as errors;
extern crate ordermap;

use rustc::hir;
use rustc::lint;
3 changes: 3 additions & 0 deletions src/libserialize/Cargo.toml
Original file line number Diff line number Diff line change
@@ -7,3 +7,6 @@ version = "0.0.0"
name = "serialize"
path = "lib.rs"
crate-type = ["dylib", "rlib"]

[dependencies]
ordermap = "0.3.0"
38 changes: 38 additions & 0 deletions src/libserialize/collection_impls.rs
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ use std::hash::{Hash, BuildHasher};

use {Decodable, Encodable, Decoder, Encoder};
use std::collections::{LinkedList, VecDeque, BTreeMap, BTreeSet, HashMap, HashSet};
use ordermap::OrderMap;

impl<
T: Encodable
@@ -163,6 +164,43 @@ impl<K, V, S> Decodable for HashMap<K, V, S>
}
}

impl<K, V, S> Encodable for OrderMap<K, V, S>
where K: Encodable + Hash + Eq,
V: Encodable,
S: BuildHasher,
{
fn encode<E: Encoder>(&self, e: &mut E) -> Result<(), E::Error> {
e.emit_map(self.len(), |e| {
let mut i = 0;
for (key, val) in self {
e.emit_map_elt_key(i, |e| key.encode(e))?;
e.emit_map_elt_val(i, |e| val.encode(e))?;
i += 1;
}
Ok(())
})
}
}

impl<K, V, S> Decodable for OrderMap<K, V, S>
where K: Decodable + Hash + Eq,
V: Decodable,
S: BuildHasher + Default,
{
fn decode<D: Decoder>(d: &mut D) -> Result<OrderMap<K, V, S>, D::Error> {
d.read_map(|d, len| {
let state = Default::default();
let mut map = OrderMap::with_capacity_and_hasher(len, state);
for i in 0..len {
let key = d.read_map_elt_key(i, |d| Decodable::decode(d))?;
let val = d.read_map_elt_val(i, |d| Decodable::decode(d))?;
map.insert(key, val);
}
Ok(map)
})
}
}

impl<T, S> Encodable for HashSet<T, S>
where T: Encodable + Hash + Eq,
S: BuildHasher,
2 changes: 2 additions & 0 deletions src/libserialize/lib.rs
Original file line number Diff line number Diff line change
@@ -27,6 +27,8 @@ Core encoding and decoding interfaces.
#![feature(specialization)]
#![cfg_attr(test, feature(test))]

extern crate ordermap;

pub use self::serialize::{Decoder, Encoder, Decodable, Encodable};

pub use self::serialize::{SpecializationError, SpecializedEncoder, SpecializedDecoder};