Skip to content

Commit dc8ac26

Browse files
authoredNov 9, 2016
Rollup merge of rust-lang#37229 - nnethercote:FxHasher, r=nikomatsakis
Replace FNV with a faster hash function. Hash table lookups are very hot in rustc profiles and the time taken within `FnvHash` itself is a big part of that. Although FNV is a simple hash, it processes its input one byte at a time. In contrast, Firefox has a homespun hash function that is also simple but works on multiple bytes at a time. So I tried it out and the results are compelling: ``` futures-rs-test 4.326s vs 4.212s --> 1.027x faster (variance: 1.001x, 1.007x) helloworld 0.233s vs 0.232s --> 1.004x faster (variance: 1.037x, 1.016x) html5ever-2016- 5.397s vs 5.210s --> 1.036x faster (variance: 1.009x, 1.006x) hyper.0.5.0 5.018s vs 4.905s --> 1.023x faster (variance: 1.007x, 1.006x) inflate-0.1.0 4.889s vs 4.872s --> 1.004x faster (variance: 1.012x, 1.007x) issue-32062-equ 0.347s vs 0.335s --> 1.035x faster (variance: 1.033x, 1.019x) issue-32278-big 1.717s vs 1.622s --> 1.059x faster (variance: 1.027x, 1.028x) jld-day15-parse 1.537s vs 1.459s --> 1.054x faster (variance: 1.005x, 1.003x) piston-image-0. 11.863s vs 11.482s --> 1.033x faster (variance: 1.060x, 1.002x) regex.0.1.30 2.517s vs 2.453s --> 1.026x faster (variance: 1.011x, 1.013x) rust-encoding-0 2.080s vs 2.047s --> 1.016x faster (variance: 1.005x, 1.005x) syntex-0.42.2 32.268s vs 31.275s --> 1.032x faster (variance: 1.014x, 1.022x) syntex-0.42.2-i 17.629s vs 16.559s --> 1.065x faster (variance: 1.013x, 1.021x) ``` (That's a stage1 compiler doing debug builds. Results for a stage2 compiler are similar.) The attached commit is not in a state suitable for landing because I changed the implementation of FnvHasher without changing its name (because that would have required touching many lines in the compiler). Nonetheless, it is a good place to start discussions. Profiles show very clearly that this new hash function is a lot faster to compute than FNV. The quality of the new hash function is less clear -- it seems to do better in some cases and worse in others (judging by the number of instructions executed in `Hash{Map,Set}::get`). CC @brson, @arthurprs
2 parents 2321d11 + 00e48af commit dc8ac26

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+703
-588
lines changed
 

‎src/librustc/dep_graph/dep_tracking_map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use hir::def_id::DefId;
12-
use rustc_data_structures::fnv::FnvHashMap;
12+
use rustc_data_structures::fx::FxHashMap;
1313
use std::cell::RefCell;
1414
use std::ops::Index;
1515
use std::hash::Hash;
@@ -24,7 +24,7 @@ use super::{DepNode, DepGraph};
2424
pub struct DepTrackingMap<M: DepTrackingMapConfig> {
2525
phantom: PhantomData<M>,
2626
graph: DepGraph,
27-
map: FnvHashMap<M::Key, M::Value>,
27+
map: FxHashMap<M::Key, M::Value>,
2828
}
2929

3030
pub trait DepTrackingMapConfig {
@@ -38,7 +38,7 @@ impl<M: DepTrackingMapConfig> DepTrackingMap<M> {
3838
DepTrackingMap {
3939
phantom: PhantomData,
4040
graph: graph,
41-
map: FnvHashMap()
41+
map: FxHashMap()
4242
}
4343
}
4444

‎src/librustc/dep_graph/edges.rs

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

11-
use rustc_data_structures::fnv::{FnvHashMap, FnvHashSet};
11+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1212
use std::fmt::Debug;
1313
use std::hash::Hash;
1414
use super::{DepGraphQuery, DepNode};
1515

1616
pub struct DepGraphEdges<D: Clone + Debug + Eq + Hash> {
1717
nodes: Vec<DepNode<D>>,
18-
indices: FnvHashMap<DepNode<D>, IdIndex>,
19-
edges: FnvHashSet<(IdIndex, IdIndex)>,
18+
indices: FxHashMap<DepNode<D>, IdIndex>,
19+
edges: FxHashSet<(IdIndex, IdIndex)>,
2020
open_nodes: Vec<OpenNode>,
2121
}
2222

@@ -46,8 +46,8 @@ impl<D: Clone + Debug + Eq + Hash> DepGraphEdges<D> {
4646
pub fn new() -> DepGraphEdges<D> {
4747
DepGraphEdges {
4848
nodes: vec![],
49-
indices: FnvHashMap(),
50-
edges: FnvHashSet(),
49+
indices: FxHashMap(),
50+
edges: FxHashSet(),
5151
open_nodes: Vec::new()
5252
}
5353
}

0 commit comments

Comments
 (0)