Skip to content

Commit

Permalink
Rollup merge of rust-lang#36831 - michaelwoerister:ich-updates, r=nik…
Browse files Browse the repository at this point in the history
…omatsakis

incr.comp.: Minor refactoring and update to struct ICH test case

r? @nikomatsakis
  • Loading branch information
alexcrichton authored Oct 12, 2016
2 parents 76fb6e7 + 954d89b commit 091b547
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 8 deletions.
8 changes: 8 additions & 0 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ pub struct PerfStats {
pub incr_comp_hashes_time: Cell<Duration>,
// The number of incr. comp. hash computations performed
pub incr_comp_hashes_count: Cell<u64>,
// The number of bytes hashed when computing ICH values
pub incr_comp_bytes_hashed: Cell<u64>,
// The accumulated time spent on computing symbol hashes
pub symbol_hash_time: Cell<Duration>,
}
Expand Down Expand Up @@ -439,6 +441,11 @@ impl Session {
duration_to_secs_str(self.perf_stats.incr_comp_hashes_time.get()));
println!("Total number of incr. comp. hashes computed: {}",
self.perf_stats.incr_comp_hashes_count.get());
println!("Total number of bytes hashed for incr. comp.: {}",
self.perf_stats.incr_comp_bytes_hashed.get());
println!("Average bytes hashed per incr. comp. HIR node: {}",
self.perf_stats.incr_comp_bytes_hashed.get() /
self.perf_stats.incr_comp_hashes_count.get());
println!("Total time spent computing symbol hashes: {}",
duration_to_secs_str(self.perf_stats.symbol_hash_time.get()));
}
Expand Down Expand Up @@ -571,6 +578,7 @@ pub fn build_session_(sopts: config::Options,
svh_time: Cell::new(Duration::from_secs(0)),
incr_comp_hashes_time: Cell::new(Duration::from_secs(0)),
incr_comp_hashes_count: Cell::new(0),
incr_comp_bytes_hashed: Cell::new(0),
symbol_hash_time: Cell::new(Duration::from_secs(0)),
}
};
Expand Down
46 changes: 46 additions & 0 deletions src/librustc_incremental/calculate_svh/hasher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::hash::Hasher;
use std::collections::hash_map::DefaultHasher;

#[derive(Debug)]
pub struct IchHasher {
// FIXME: this should use SHA1, not DefaultHasher. DefaultHasher is not
// built to avoid collisions.
state: DefaultHasher,
bytes_hashed: u64,
}

impl IchHasher {
pub fn new() -> IchHasher {
IchHasher {
state: DefaultHasher::new(),
bytes_hashed: 0
}
}

pub fn bytes_hashed(&self) -> u64 {
self.bytes_hashed
}
}

impl Hasher for IchHasher {
#[inline]
fn finish(&self) -> u64 {
self.state.finish()
}

#[inline]
fn write(&mut self, bytes: &[u8]) {
self.state.write(bytes);
self.bytes_hashed += bytes.len() as u64;
}
}
20 changes: 15 additions & 5 deletions src/librustc_incremental/calculate_svh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use syntax::ast;
use std::cell::RefCell;
use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;
use rustc::dep_graph::DepNode;
use rustc::hir;
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
Expand All @@ -43,10 +42,12 @@ use rustc::session::config::DebugInfoLevel::NoDebugInfo;
use self::def_path_hash::DefPathHashes;
use self::svh_visitor::StrictVersionHashVisitor;
use self::caching_codemap_view::CachingCodemapView;
use self::hasher::IchHasher;

mod def_path_hash;
mod svh_visitor;
mod caching_codemap_view;
mod hasher;

pub struct IncrementalHashesMap {
hashes: FnvHashMap<DepNode<DefId>, u64>,
Expand Down Expand Up @@ -74,6 +75,10 @@ impl IncrementalHashesMap {
pub fn iter<'a>(&'a self) -> ::std::collections::hash_map::Iter<'a, DepNode<DefId>, u64> {
self.hashes.iter()
}

pub fn len(&self) -> usize {
self.hashes.len()
}
}

impl<'a> ::std::ops::Index<&'a DepNode<DefId>> for IncrementalHashesMap {
Expand Down Expand Up @@ -102,6 +107,9 @@ pub fn compute_incremental_hashes_map<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
|v| visit::walk_crate(v, krate));
krate.visit_all_items(&mut visitor);
});

tcx.sess.perf_stats.incr_comp_hashes_count.set(visitor.hashes.len() as u64);

record_time(&tcx.sess.perf_stats.svh_time, || visitor.compute_crate_hash());
visitor.hashes
}
Expand All @@ -127,9 +135,7 @@ impl<'a, 'tcx> HashItemsVisitor<'a, 'tcx> {
{
assert!(def_id.is_local());
debug!("HashItemsVisitor::calculate(def_id={:?})", def_id);
// FIXME: this should use SHA1, not DefaultHasher. DefaultHasher is not
// built to avoid collisions.
let mut state = DefaultHasher::new();
let mut state = IchHasher::new();
walk_op(&mut StrictVersionHashVisitor::new(&mut state,
self.tcx,
&mut self.def_path_hashes,
Expand All @@ -138,12 +144,16 @@ impl<'a, 'tcx> HashItemsVisitor<'a, 'tcx> {
let item_hash = state.finish();
self.hashes.insert(DepNode::Hir(def_id), item_hash);
debug!("calculate_item_hash: def_id={:?} hash={:?}", def_id, item_hash);

let bytes_hashed = self.tcx.sess.perf_stats.incr_comp_bytes_hashed.get() +
state.bytes_hashed();
self.tcx.sess.perf_stats.incr_comp_bytes_hashed.set(bytes_hashed);
}

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

let mut crate_state = DefaultHasher::new();
let mut crate_state = IchHasher::new();

let crate_disambiguator = self.tcx.sess.local_crate_disambiguator();
"crate_disambiguator".hash(&mut crate_state);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_incremental/calculate_svh/svh_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ use rustc::hir::intravisit as visit;
use rustc::ty::TyCtxt;
use rustc_data_structures::fnv;
use std::hash::Hash;
use std::collections::hash_map::DefaultHasher;

use super::def_path_hash::DefPathHashes;
use super::caching_codemap_view::CachingCodemapView;
use super::hasher::IchHasher;

const IGNORED_ATTRIBUTES: &'static [&'static str] = &[
"cfg",
Expand All @@ -48,15 +48,15 @@ const IGNORED_ATTRIBUTES: &'static [&'static str] = &[

pub struct StrictVersionHashVisitor<'a, 'hash: 'a, 'tcx: 'hash> {
pub tcx: TyCtxt<'hash, 'tcx, 'tcx>,
pub st: &'a mut DefaultHasher,
pub st: &'a mut IchHasher,
// collect a deterministic hash of def-ids that we have seen
def_path_hashes: &'a mut DefPathHashes<'hash, 'tcx>,
hash_spans: bool,
codemap: &'a mut CachingCodemapView<'tcx>,
}

impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
pub fn new(st: &'a mut DefaultHasher,
pub fn new(st: &'a mut IchHasher,
tcx: TyCtxt<'hash, 'tcx, 'tcx>,
def_path_hashes: &'a mut DefPathHashes<'hash, 'tcx>,
codemap: &'a mut CachingCodemapView<'tcx>,
Expand Down
71 changes: 71 additions & 0 deletions src/test/incremental/hashes/struct_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,74 @@ struct Visibility;
#[rustc_clean(label="Hir", cfg="cfail3")]
#[rustc_metadata_clean(cfg="cfail3")]
pub struct Visibility;




struct ReferencedType1;
struct ReferencedType2;

// Tuple Struct Change Field Type Indirectly -----------------------------------
mod tuple_struct_change_field_type_indirectly {
#[cfg(cfail1)]
use super::ReferencedType1 as FieldType;
#[cfg(not(cfail1))]
use super::ReferencedType2 as FieldType;

#[rustc_dirty(label="Hir", cfg="cfail2")]
#[rustc_clean(label="Hir", cfg="cfail3")]
#[rustc_metadata_dirty(cfg="cfail2")]
#[rustc_metadata_clean(cfg="cfail3")]
struct TupleStruct(FieldType);
}


// Record Struct Change Field Type Indirectly -----------------------------------
mod record_struct_change_field_type_indirectly {
#[cfg(cfail1)]
use super::ReferencedType1 as FieldType;
#[cfg(not(cfail1))]
use super::ReferencedType2 as FieldType;

#[rustc_dirty(label="Hir", cfg="cfail2")]
#[rustc_clean(label="Hir", cfg="cfail3")]
#[rustc_metadata_dirty(cfg="cfail2")]
#[rustc_metadata_clean(cfg="cfail3")]
struct RecordStruct {
_x: FieldType
}
}




trait ReferencedTrait1 {}
trait ReferencedTrait2 {}

// Change Trait Bound Indirectly -----------------------------------------------
mod change_trait_bound_indirectly {
#[cfg(cfail1)]
use super::ReferencedTrait1 as Trait;
#[cfg(not(cfail1))]
use super::ReferencedTrait2 as Trait;

#[rustc_dirty(label="Hir", cfg="cfail2")]
#[rustc_clean(label="Hir", cfg="cfail3")]
#[rustc_metadata_dirty(cfg="cfail2")]
#[rustc_metadata_clean(cfg="cfail3")]
struct Struct<T: Trait>(T);
}

// Change Trait Bound Indirectly In Where Clause -------------------------------
mod change_trait_bound_indirectly_in_where_clause {
#[cfg(cfail1)]
use super::ReferencedTrait1 as Trait;
#[cfg(not(cfail1))]
use super::ReferencedTrait2 as Trait;

#[rustc_dirty(label="Hir", cfg="cfail2")]
#[rustc_clean(label="Hir", cfg="cfail3")]
#[rustc_metadata_dirty(cfg="cfail2")]
#[rustc_metadata_clean(cfg="cfail3")]
struct Struct<T>(T) where T : Trait;
}

0 comments on commit 091b547

Please sign in to comment.