Skip to content

Commit 43308d4

Browse files
committed
Auto merge of #45282 - stjepang:experiment-ordermap, r=<try>
[Experiment] Replace HashMap with OrderMap Do not merge. This is just a simple experiment where I modified `FxHashMap` to use `OrderMap` instead of `HashMap`, as explained in #45273. Don't expect the code to look good. :) cc @Mark-Simulacrum - Can we please run performance tests to see how this PR impacts compile times? cc @bluss @kennytm @eddyb said on IRC that we shouldn't blindly swap the implementation just yet - let's investigate a bit further. If changing the hash map implementation affects performance a lot, then we can probably gain even more by using a different data structure.
2 parents af7de7b + 5dbe2e4 commit 43308d4

File tree

40 files changed

+114
-30
lines changed

40 files changed

+114
-30
lines changed

src/Cargo.lock

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ rustc_errors = { path = "../librustc_errors" }
2323
serialize = { path = "../libserialize" }
2424
syntax = { path = "../libsyntax" }
2525
syntax_pos = { path = "../libsyntax_pos" }
26+
ordermap = "0.3.0"
2627

2728
# Note that these dependencies are a lie, they're just here to get linkage to
2829
# work.

src/librustc/dep_graph/query.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl DepGraphQuery {
4141
}
4242

4343
pub fn contains_node(&self, node: &DepNode) -> bool {
44-
self.indices.contains_key(&node)
44+
self.indices.contains_key(node)
4545
}
4646

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

8484
/// Just the outgoing edges from `node`.
8585
pub fn immediate_successors(&self, node: &DepNode) -> Vec<&DepNode> {
86-
if let Some(&index) = self.indices.get(&node) {
86+
if let Some(&index) = self.indices.get(node) {
8787
self.graph.successor_nodes(index)
8888
.map(|s| self.graph.node_data(s))
8989
.collect()

src/librustc/ich/hcx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use session::Session;
2121
use std::cmp::Ord;
2222
use std::hash as std_hash;
2323
use std::cell::RefCell;
24-
use std::collections::HashMap;
24+
use ordermap::OrderMap;
2525

2626
use syntax::ast;
2727
use syntax::attr;
@@ -426,7 +426,7 @@ pub fn hash_stable_trait_impls<'gcx, W, R>(
426426
hcx: &mut StableHashingContext<'gcx>,
427427
hasher: &mut StableHasher<W>,
428428
blanket_impls: &Vec<DefId>,
429-
non_blanket_impls: &HashMap<fast_reject::SimplifiedType, Vec<DefId>, R>)
429+
non_blanket_impls: &OrderMap<fast_reject::SimplifiedType, Vec<DefId>, R>)
430430
where W: StableHasherResult,
431431
R: std_hash::BuildHasher,
432432
{

src/librustc/infer/freshen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use ty::subst::Substs;
4747
use util::nodemap::FxHashMap;
4848
use hir::def_id::DefId;
4949

50-
use std::collections::hash_map::Entry;
50+
use ordermap::Entry;
5151

5252
use super::InferCtxt;
5353
use super::unify_key::ToType;

src/librustc/infer/region_inference/graphviz.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use infer::region_inference::RegionVarBindings;
2828
use util::nodemap::{FxHashMap, FxHashSet};
2929

3030
use std::borrow::Cow;
31-
use std::collections::hash_map::Entry::Vacant;
31+
use ordermap::Entry::Vacant;
3232
use std::env;
3333
use std::fs::File;
3434
use std::io;

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
#![recursion_limit="256"]
6464

65+
extern crate ordermap;
6566
extern crate arena;
6667
#[macro_use] extern crate bitflags;
6768
extern crate core;

src/librustc/traits/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
12631263
let trait_ref = &cache_fresh_trait_pred.0.trait_ref;
12641264
if self.can_use_global_caches(param_env) {
12651265
let cache = tcx.selection_cache.hashmap.borrow();
1266-
if let Some(cached) = cache.get(&trait_ref) {
1266+
if let Some(cached) = cache.get(trait_ref) {
12671267
return Some(cached.get(tcx));
12681268
}
12691269
}

src/librustc/ty/context.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,16 @@ use rustc_data_structures::accumulate_vec::AccumulateVec;
5353
use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
5454
StableHasher, StableHasherResult,
5555
StableVec};
56+
use rustc_data_structures;
5657
use arena::{TypedArena, DroplessArena};
5758
use rustc_const_math::{ConstInt, ConstUsize};
5859
use rustc_data_structures::indexed_vec::IndexVec;
5960
use std::any::Any;
6061
use std::borrow::Borrow;
6162
use std::cell::{Cell, RefCell};
6263
use std::cmp::Ordering;
63-
use std::collections::hash_map::{self, Entry};
64-
use std::hash::{Hash, Hasher};
64+
use ordermap::{self, Entry};
65+
use std::hash::{BuildHasherDefault, Hash, Hasher};
6566
use std::mem;
6667
use std::ops::Deref;
6768
use std::iter;
@@ -275,7 +276,7 @@ impl<'a, V> LocalTableInContext<'a, V> {
275276
self.data.get(&id.local_id)
276277
}
277278

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

302-
pub fn entry(&mut self, id: hir::HirId) -> Entry<hir::ItemLocalId, V> {
303+
pub fn entry(
304+
&mut self,
305+
id: hir::HirId
306+
) -> Entry<hir::ItemLocalId, V, BuildHasherDefault<rustc_data_structures::fx::FxHasher>> {
303307
validate_hir_id_for_typeck_tables(self.local_id_root, id, true);
304308
self.data.entry(id.local_id)
305309
}

src/librustc_data_structures/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ crate-type = ["dylib"]
1111
[dependencies]
1212
log = "0.3"
1313
serialize = { path = "../libserialize" }
14+
ordermap = "0.3.0"

src/librustc_data_structures/fx.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::collections::{HashMap, HashSet};
11+
use std::collections::HashSet;
1212
use std::default::Default;
1313
use std::hash::{Hasher, Hash, BuildHasherDefault};
1414
use std::ops::BitXor;
15+
use ordermap::OrderMap;
1516

16-
pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
17+
// pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
1718
pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;
1819

20+
pub type FxHashMap<K, V> = OrderMap<K, V, BuildHasherDefault<FxHasher>>;
21+
1922
#[allow(non_snake_case)]
2023
pub fn FxHashMap<K: Hash + Eq, V>() -> FxHashMap<K, V> {
21-
HashMap::default()
24+
Default::default()
2225
}
2326

2427
#[allow(non_snake_case)]

src/librustc_data_structures/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extern crate log;
4040
extern crate serialize as rustc_serialize; // used by deriving
4141
#[cfg(unix)]
4242
extern crate libc;
43+
extern crate ordermap;
4344

4445
pub use rustc_serialize::hex::ToHex;
4546

src/librustc_data_structures/obligation_forest/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use fx::{FxHashMap, FxHashSet};
1919

2020
use std::cell::Cell;
21-
use std::collections::hash_map::Entry;
21+
use super::ordermap::Entry;
2222
use std::fmt::Debug;
2323
use std::hash;
2424
use std::marker::PhantomData;

src/librustc_data_structures/stable_hasher.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::marker::PhantomData;
1313
use std::mem;
1414
use blake2b::Blake2bHasher;
1515
use rustc_serialize::leb128;
16+
use ordermap::OrderMap;
1617

1718
fn write_unsigned_leb128_to_buf(buf: &mut [u8; 16], value: u64) -> usize {
1819
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>
482483
impl_stable_hash_via_hash!(::std::path::Path);
483484
impl_stable_hash_via_hash!(::std::path::PathBuf);
484485

485-
impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R>
486+
impl<K, V, R, HCX> HashStable<HCX> for OrderMap<K, V, R>
486487
where K: ToStableHashKey<HCX> + Eq + Hash,
487488
V: HashStable<HCX>,
488489
R: BuildHasher,
@@ -542,7 +543,7 @@ impl<K, HCX> HashStable<HCX> for ::std::collections::BTreeSet<K>
542543
pub fn hash_stable_hashmap<HCX, K, V, R, SK, F, W>(
543544
hcx: &mut HCX,
544545
hasher: &mut StableHasher<W>,
545-
map: &::std::collections::HashMap<K, V, R>,
546+
map: &OrderMap<K, V, R>,
546547
to_stable_hash_key: F)
547548
where K: Eq + Hash,
548549
V: HashStable<HCX>,

src/librustc_lint/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ rustc_back = { path = "../librustc_back" }
1616
rustc_const_eval = { path = "../librustc_const_eval" }
1717
syntax = { path = "../libsyntax" }
1818
syntax_pos = { path = "../libsyntax_pos" }
19+
ordermap = "0.3.0"

src/librustc_lint/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern crate log;
4141
extern crate rustc_back;
4242
extern crate rustc_const_eval;
4343
extern crate syntax_pos;
44+
extern crate ordermap;
4445

4546
use rustc::lint;
4647
use rustc::middle;

src/librustc_lint/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use util::nodemap::FxHashMap;
1515
use lint::{LateContext, EarlyContext, LintContext, LintArray};
1616
use lint::{LintPass, EarlyLintPass, LateLintPass};
1717

18-
use std::collections::hash_map::Entry::{Occupied, Vacant};
18+
use ordermap::Entry::{Occupied, Vacant};
1919

2020
use syntax::ast;
2121
use syntax::attr;

src/librustc_metadata/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ serialize = { path = "../libserialize" }
2121
syntax = { path = "../libsyntax" }
2222
syntax_ext = { path = "../libsyntax_ext" }
2323
syntax_pos = { path = "../libsyntax_pos" }
24+
ordermap = "0.3.0"

src/librustc_metadata/cstore_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
290290
// external item to be parents).
291291
visible_parent_map: |tcx, cnum| {
292292
use std::collections::vec_deque::VecDeque;
293-
use std::collections::hash_map::Entry;
293+
use ordermap::Entry;
294294

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

315315
match visible_parent_map.entry(child) {
316-
Entry::Occupied(mut entry) => {
316+
Entry::Occupied(entry) => {
317317
// If `child` is defined in crate `cnum`, ensure
318318
// that it is mapped to a parent in `cnum`.
319319
if child.krate == cnum && entry.get().krate != cnum {

src/librustc_metadata/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern crate proc_macro;
3939
extern crate rustc;
4040
extern crate rustc_back;
4141
extern crate rustc_data_structures;
42+
extern crate ordermap;
4243

4344
mod diagnostics;
4445

src/librustc_mir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ rustc_errors = { path = "../librustc_errors" }
2020
serialize = { path = "../libserialize" }
2121
syntax = { path = "../libsyntax" }
2222
syntax_pos = { path = "../libsyntax_pos" }
23+
ordermap = "0.3.0"

src/librustc_mir/dataflow/impls/borrows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<'a, 'tcx> Borrows<'a, 'tcx> {
117117
}
118118

119119
pub fn region_span(&self, region: &Region) -> Span {
120-
let opt_span = self.region_span_map.get(region);
120+
let opt_span = self.region_span_map.get(*region);
121121
assert!(opt_span.is_some(), "end region not found for {:?}", region);
122122
*opt_span.unwrap()
123123
}

src/librustc_mir/dataflow/move_paths/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_data_structures::indexed_vec::{IndexVec};
1616

1717
use syntax::codemap::DUMMY_SP;
1818

19-
use std::collections::hash_map::Entry;
19+
use ordermap::Entry;
2020
use std::mem;
2121

2222
use super::abs_domain::Lift;

src/librustc_mir/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern crate syntax_pos;
4141
extern crate rustc_const_math;
4242
extern crate rustc_const_eval;
4343
extern crate core; // for NonZero
44+
extern crate ordermap;
4445

4546
mod diagnostics;
4647

src/librustc_passes/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ rustc_const_math = { path = "../librustc_const_math" }
1616
syntax = { path = "../libsyntax" }
1717
syntax_pos = { path = "../libsyntax_pos" }
1818
rustc_errors = { path = "../librustc_errors" }
19+
ordermap = "0.3.0"

src/librustc_passes/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use syntax::ast;
5050
use syntax_pos::{Span, DUMMY_SP};
5151
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
5252

53-
use std::collections::hash_map::Entry;
53+
use ordermap::Entry;
5454
use std::cmp::Ordering;
5555

5656
struct CheckCrateVisitor<'a, 'tcx: 'a> {

src/librustc_passes/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern crate log;
3232
extern crate syntax;
3333
extern crate syntax_pos;
3434
extern crate rustc_errors as errors;
35+
extern crate ordermap;
3536

3637
mod diagnostics;
3738

src/librustc_trans/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rustc_trans_utils = { path = "../librustc_trans_utils" }
3030
serialize = { path = "../libserialize" }
3131
syntax = { path = "../libsyntax" }
3232
syntax_pos = { path = "../libsyntax_pos" }
33+
ordermap = "0.3.0"
3334

3435
[target."cfg(windows)".dependencies]
3536
cc = "1.0"

0 commit comments

Comments
 (0)