Skip to content
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

Some preparatory refactorings for hash-based DepNodes #42504

Merged
merged 4 commits into from
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 14 additions & 3 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
// except according to those terms.

use hir::def_id::CrateNum;
use ich::Fingerprint;
use rustc_data_structures::stable_hasher::StableHasher;
use std::fmt::Debug;
use std::sync::Arc;
use std::hash::Hash;

macro_rules! try_opt {
($e:expr) => (
Expand Down Expand Up @@ -56,7 +58,7 @@ pub enum DepNode<D: Clone + Debug> {

/// Represents some artifact that we save to disk. Note that these
/// do not have a def-id as part of their identifier.
WorkProduct(Arc<WorkProductId>),
WorkProduct(WorkProductId),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this make DepNode be Copy? That would be great!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(What is Fingerprint anyway?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, DepNode will be Copy.
A Fingerprint is a hash value with a "virtual uniqueness" property, i.e. there will be collisions (like with any hash) but the probability is so low that it doesn't matter. Because of this property, a Fingerprint can be used as an identifier. In terms of rustc I now use the type Fingerprint for anything that fulfills this property but sometimes I like to introduce newtypes for it, for example DefPathHash.


// Represents different phases in the compiler.
RegionMaps(D),
Expand Down Expand Up @@ -318,7 +320,16 @@ impl<D: Clone + Debug> DepNode<D> {
/// the need to be mapped or unmapped. (This ensures we can serialize
/// them even in the absence of a tcx.)
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct WorkProductId(pub String);
pub struct WorkProductId(pub Fingerprint);

impl WorkProductId {
pub fn from_cgu_name(cgu_name: &str) -> WorkProductId {
let mut hasher = StableHasher::new();
cgu_name.len().hash(&mut hasher);
cgu_name.hash(&mut hasher);
WorkProductId(hasher.finish())
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub enum GlobalMetaDataKind {
Expand Down
15 changes: 7 additions & 8 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use rustc_data_structures::fx::FxHashMap;
use session::config::OutputType;
use std::cell::{Ref, RefCell};
use std::rc::Rc;
use std::sync::Arc;

use super::dep_node::{DepNode, WorkProductId};
use super::query::DepGraphQuery;
Expand All @@ -35,10 +34,10 @@ struct DepGraphData {
/// things available to us. If we find that they are not dirty, we
/// load the path to the file storing those work-products here into
/// this map. We can later look for and extract that data.
previous_work_products: RefCell<FxHashMap<Arc<WorkProductId>, WorkProduct>>,
previous_work_products: RefCell<FxHashMap<WorkProductId, WorkProduct>>,

/// Work-products that we generate in this run.
work_products: RefCell<FxHashMap<Arc<WorkProductId>, WorkProduct>>,
work_products: RefCell<FxHashMap<WorkProductId, WorkProduct>>,
}

impl DepGraph {
Expand Down Expand Up @@ -120,7 +119,7 @@ impl DepGraph {
/// Indicates that a previous work product exists for `v`. This is
/// invoked during initial start-up based on what nodes are clean
/// (and what files exist in the incr. directory).
pub fn insert_previous_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) {
pub fn insert_previous_work_product(&self, v: &WorkProductId, data: WorkProduct) {
debug!("insert_previous_work_product({:?}, {:?})", v, data);
self.data.previous_work_products.borrow_mut()
.insert(v.clone(), data);
Expand All @@ -129,29 +128,29 @@ impl DepGraph {
/// Indicates that we created the given work-product in this run
/// for `v`. This record will be preserved and loaded in the next
/// run.
pub fn insert_work_product(&self, v: &Arc<WorkProductId>, data: WorkProduct) {
pub fn insert_work_product(&self, v: &WorkProductId, data: WorkProduct) {
debug!("insert_work_product({:?}, {:?})", v, data);
self.data.work_products.borrow_mut()
.insert(v.clone(), data);
}

/// Check whether a previous work product exists for `v` and, if
/// so, return the path that leads to it. Used to skip doing work.
pub fn previous_work_product(&self, v: &Arc<WorkProductId>) -> Option<WorkProduct> {
pub fn previous_work_product(&self, v: &WorkProductId) -> Option<WorkProduct> {
self.data.previous_work_products.borrow()
.get(v)
.cloned()
}

/// Access the map of work-products created during this run. Only
/// used during saving of the dep-graph.
pub fn work_products(&self) -> Ref<FxHashMap<Arc<WorkProductId>, WorkProduct>> {
pub fn work_products(&self) -> Ref<FxHashMap<WorkProductId, WorkProduct>> {
self.data.work_products.borrow()
}

/// Access the map of work-products created during the cached run. Only
/// used during saving of the dep-graph.
pub fn previous_work_products(&self) -> Ref<FxHashMap<Arc<WorkProductId>, WorkProduct>> {
pub fn previous_work_products(&self) -> Ref<FxHashMap<WorkProductId, WorkProduct>> {
self.data.previous_work_products.borrow()
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_incremental/persist/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use rustc::hir::def_id::DefIndex;
use rustc::hir::map::DefPathHash;
use rustc::ich::Fingerprint;
use rustc::middle::cstore::EncodedMetadataHash;
use std::sync::Arc;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::indexed_vec::{IndexVec, Idx};

Expand Down Expand Up @@ -98,7 +97,7 @@ pub struct SerializedHash {
#[derive(Debug, RustcEncodable, RustcDecodable)]
pub struct SerializedWorkProduct {
/// node that produced the work-product
pub id: Arc<WorkProductId>,
pub id: WorkProductId,

/// work-product data itself
pub work_product: WorkProduct,
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_incremental/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use rustc_serialize::Decodable as RustcDecodable;
use rustc_serialize::opaque::Decoder;
use std::default::Default;
use std::path::{Path};
use std::sync::Arc;

use IncrementalHashesMap;
use super::data::*;
Expand Down Expand Up @@ -327,7 +326,7 @@ fn transitive_dirty_nodes(edge_map: &FxHashMap<DepNode<DefPathHash>, Vec<DepNode
/// otherwise no longer applicable.
fn reconcile_work_products<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
work_products: Vec<SerializedWorkProduct>,
clean_work_products: &FxHashSet<Arc<WorkProductId>>) {
clean_work_products: &FxHashSet<WorkProductId>) {
debug!("reconcile_work_products({:?})", work_products);
for swp in work_products {
if !clean_work_products.contains(&swp.id) {
Expand Down Expand Up @@ -424,8 +423,8 @@ fn process_edges<'a, 'tcx, 'edges>(
target: &'edges DepNode<DefPathHash>,
edges: &'edges FxHashMap<DepNode<DefPathHash>, Vec<DepNode<DefPathHash>>>,
dirty_raw_nodes: &DirtyNodes,
clean_work_products: &mut FxHashSet<Arc<WorkProductId>>,
dirty_work_products: &mut FxHashSet<Arc<WorkProductId>>,
clean_work_products: &mut FxHashSet<WorkProductId>,
dirty_work_products: &mut FxHashSet<WorkProductId>,
extra_edges: &mut Vec<(&'edges DepNode<DefPathHash>, &'edges DepNode<DefPathHash>)>)
{
// If the target is dirty, skip the edge. If this is an edge
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_incremental/persist/work_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use rustc::session::Session;
use rustc::session::config::OutputType;
use rustc::util::fs::link_or_copy;
use std::path::PathBuf;
use std::sync::Arc;
use std::fs as std_fs;

pub fn save_trans_partition(sess: &Session,
Expand All @@ -30,7 +29,7 @@ pub fn save_trans_partition(sess: &Session,
if sess.opts.incremental.is_none() {
return;
}
let work_product_id = Arc::new(WorkProductId(cgu_name.to_string()));
let work_product_id = WorkProductId::from_cgu_name(cgu_name);

let saved_files: Option<Vec<_>> =
files.iter()
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_trans/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ use rustc::ty::{self, TyCtxt};
use rustc::ty::item_path::characteristic_def_id_of_type;
use rustc_incremental::IchHasher;
use std::hash::Hash;
use std::sync::Arc;
use syntax::ast::NodeId;
use syntax::symbol::{Symbol, InternedString};
use trans_item::{TransItem, InstantiationMode};
Expand Down Expand Up @@ -164,8 +163,8 @@ impl<'tcx> CodegenUnit<'tcx> {
&self.items
}

pub fn work_product_id(&self) -> Arc<WorkProductId> {
Arc::new(WorkProductId(self.name().to_string()))
pub fn work_product_id(&self) -> WorkProductId {
WorkProductId::from_cgu_name(self.name())
}

pub fn work_product_dep_node(&self) -> DepNode<DefId> {
Expand Down