Skip to content

Commit 57d6ddd

Browse files
incr.comp.: Hide concrete hash algorithm used for ICH
1 parent e2bd2d8 commit 57d6ddd

File tree

4 files changed

+72
-8
lines changed

4 files changed

+72
-8
lines changed

src/librustc/session/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ pub struct PerfStats {
118118
pub incr_comp_hashes_time: Cell<Duration>,
119119
// The number of incr. comp. hash computations performed
120120
pub incr_comp_hashes_count: Cell<u64>,
121+
// The number of bytes hashed when computing ICH values
122+
pub incr_comp_bytes_hashed: Cell<u64>,
121123
// The accumulated time spent on computing symbol hashes
122124
pub symbol_hash_time: Cell<Duration>,
123125
}
@@ -439,6 +441,11 @@ impl Session {
439441
duration_to_secs_str(self.perf_stats.incr_comp_hashes_time.get()));
440442
println!("Total number of incr. comp. hashes computed: {}",
441443
self.perf_stats.incr_comp_hashes_count.get());
444+
println!("Total number of bytes hashed for incr. comp.: {}",
445+
self.perf_stats.incr_comp_bytes_hashed.get());
446+
println!("Average bytes hashed per incr. comp. HIR node: {}",
447+
self.perf_stats.incr_comp_bytes_hashed.get() /
448+
self.perf_stats.incr_comp_hashes_count.get());
442449
println!("Total time spent computing symbol hashes: {}",
443450
duration_to_secs_str(self.perf_stats.symbol_hash_time.get()));
444451
}
@@ -571,6 +578,7 @@ pub fn build_session_(sopts: config::Options,
571578
svh_time: Cell::new(Duration::from_secs(0)),
572579
incr_comp_hashes_time: Cell::new(Duration::from_secs(0)),
573580
incr_comp_hashes_count: Cell::new(0),
581+
incr_comp_bytes_hashed: Cell::new(0),
574582
symbol_hash_time: Cell::new(Duration::from_secs(0)),
575583
}
576584
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::hash::Hasher;
12+
use std::collections::hash_map::DefaultHasher;
13+
14+
#[derive(Debug)]
15+
pub struct IchHasher {
16+
// FIXME: this should use SHA1, not DefaultHasher. DefaultHasher is not
17+
// built to avoid collisions.
18+
state: DefaultHasher,
19+
bytes_hashed: u64,
20+
}
21+
22+
impl IchHasher {
23+
pub fn new() -> IchHasher {
24+
IchHasher {
25+
state: DefaultHasher::new(),
26+
bytes_hashed: 0
27+
}
28+
}
29+
30+
pub fn bytes_hashed(&self) -> u64 {
31+
self.bytes_hashed
32+
}
33+
}
34+
35+
impl Hasher for IchHasher {
36+
#[inline]
37+
fn finish(&self) -> u64 {
38+
self.state.finish()
39+
}
40+
41+
#[inline]
42+
fn write(&mut self, bytes: &[u8]) {
43+
self.state.write(bytes);
44+
self.bytes_hashed += bytes.len() as u64;
45+
}
46+
}

src/librustc_incremental/calculate_svh/mod.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
use syntax::ast;
3131
use std::cell::RefCell;
3232
use std::hash::{Hash, Hasher};
33-
use std::collections::hash_map::DefaultHasher;
3433
use rustc::dep_graph::DepNode;
3534
use rustc::hir;
3635
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
@@ -43,10 +42,12 @@ use rustc::session::config::DebugInfoLevel::NoDebugInfo;
4342
use self::def_path_hash::DefPathHashes;
4443
use self::svh_visitor::StrictVersionHashVisitor;
4544
use self::caching_codemap_view::CachingCodemapView;
45+
use self::hasher::IchHasher;
4646

4747
mod def_path_hash;
4848
mod svh_visitor;
4949
mod caching_codemap_view;
50+
mod hasher;
5051

5152
pub struct IncrementalHashesMap {
5253
hashes: FnvHashMap<DepNode<DefId>, u64>,
@@ -74,6 +75,10 @@ impl IncrementalHashesMap {
7475
pub fn iter<'a>(&'a self) -> ::std::collections::hash_map::Iter<'a, DepNode<DefId>, u64> {
7576
self.hashes.iter()
7677
}
78+
79+
pub fn len(&self) -> usize {
80+
self.hashes.len()
81+
}
7782
}
7883

7984
impl<'a> ::std::ops::Index<&'a DepNode<DefId>> for IncrementalHashesMap {
@@ -102,6 +107,9 @@ pub fn compute_incremental_hashes_map<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
102107
|v| visit::walk_crate(v, krate));
103108
krate.visit_all_items(&mut visitor);
104109
});
110+
111+
tcx.sess.perf_stats.incr_comp_hashes_count.set(visitor.hashes.len() as u64);
112+
105113
record_time(&tcx.sess.perf_stats.svh_time, || visitor.compute_crate_hash());
106114
visitor.hashes
107115
}
@@ -127,9 +135,7 @@ impl<'a, 'tcx> HashItemsVisitor<'a, 'tcx> {
127135
{
128136
assert!(def_id.is_local());
129137
debug!("HashItemsVisitor::calculate(def_id={:?})", def_id);
130-
// FIXME: this should use SHA1, not DefaultHasher. DefaultHasher is not
131-
// built to avoid collisions.
132-
let mut state = DefaultHasher::new();
138+
let mut state = IchHasher::new();
133139
walk_op(&mut StrictVersionHashVisitor::new(&mut state,
134140
self.tcx,
135141
&mut self.def_path_hashes,
@@ -138,12 +144,16 @@ impl<'a, 'tcx> HashItemsVisitor<'a, 'tcx> {
138144
let item_hash = state.finish();
139145
self.hashes.insert(DepNode::Hir(def_id), item_hash);
140146
debug!("calculate_item_hash: def_id={:?} hash={:?}", def_id, item_hash);
147+
148+
let bytes_hashed = self.tcx.sess.perf_stats.incr_comp_bytes_hashed.get() +
149+
state.bytes_hashed();
150+
self.tcx.sess.perf_stats.incr_comp_bytes_hashed.set(bytes_hashed);
141151
}
142152

143153
fn compute_crate_hash(&mut self) {
144154
let krate = self.tcx.map.krate();
145155

146-
let mut crate_state = DefaultHasher::new();
156+
let mut crate_state = IchHasher::new();
147157

148158
let crate_disambiguator = self.tcx.sess.local_crate_disambiguator();
149159
"crate_disambiguator".hash(&mut crate_state);

src/librustc_incremental/calculate_svh/svh_visitor.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ use rustc::hir::intravisit as visit;
3131
use rustc::ty::TyCtxt;
3232
use rustc_data_structures::fnv;
3333
use std::hash::Hash;
34-
use std::collections::hash_map::DefaultHasher;
3534

3635
use super::def_path_hash::DefPathHashes;
3736
use super::caching_codemap_view::CachingCodemapView;
37+
use super::hasher::IchHasher;
3838

3939
const IGNORED_ATTRIBUTES: &'static [&'static str] = &[
4040
"cfg",
@@ -48,15 +48,15 @@ const IGNORED_ATTRIBUTES: &'static [&'static str] = &[
4848

4949
pub struct StrictVersionHashVisitor<'a, 'hash: 'a, 'tcx: 'hash> {
5050
pub tcx: TyCtxt<'hash, 'tcx, 'tcx>,
51-
pub st: &'a mut DefaultHasher,
51+
pub st: &'a mut IchHasher,
5252
// collect a deterministic hash of def-ids that we have seen
5353
def_path_hashes: &'a mut DefPathHashes<'hash, 'tcx>,
5454
hash_spans: bool,
5555
codemap: &'a mut CachingCodemapView<'tcx>,
5656
}
5757

5858
impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
59-
pub fn new(st: &'a mut DefaultHasher,
59+
pub fn new(st: &'a mut IchHasher,
6060
tcx: TyCtxt<'hash, 'tcx, 'tcx>,
6161
def_path_hashes: &'a mut DefPathHashes<'hash, 'tcx>,
6262
codemap: &'a mut CachingCodemapView<'tcx>,

0 commit comments

Comments
 (0)