Skip to content

Commit

Permalink
avoid hash in client code
Browse files Browse the repository at this point in the history
  • Loading branch information
SparrowLii committed Jan 5, 2023
1 parent 4e5256e commit a4b1663
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
39 changes: 21 additions & 18 deletions compiler/rustc_query_system/src/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,24 +378,16 @@ impl<K: DepKind> DepGraph<K> {
/// Executes something within an "anonymous" task, that is, a task the
/// `DepNode` of which is determined by the list of inputs it read from.
#[inline]
pub fn with_anon_task<Tcx: DepContext<DepKind = K>, OP, R>( &self,
pub fn with_anon_task<Tcx: DepContext<DepKind = K>, OP, R>(
&self,
cx: Tcx,
dep_kind: K,
op: OP,
) -> (R, DepNodeIndex)
where
OP: FnOnce() -> R,
{
self.with_hash_task(cx, dep_kind, op, |task_deps| {
// The dep node indices are hashed here instead of hashing the dep nodes of the
// dependencies. These indices may refer to different nodes per session, but this isn't
// a problem here because we that ensure the final dep node hash is per session only by
// combining it with the per session random number `anon_id_seed`. This hash only need
// to map the dependencies to a single value on a per session basis.
let mut hasher = StableHasher::new();
task_deps.reads.hash(&mut hasher);
hasher.finish()
})
self.with_hash_task(cx, dep_kind, op, None::<()>)
}

/// Executes something within an "anonymous" task. The `hash` is used for
Expand All @@ -405,20 +397,21 @@ impl<K: DepKind> DepGraph<K> {
cx: Ctxt,
dep_kind: K,
op: OP,
hash: H,
hash: Option<H>,
) -> (R, DepNodeIndex)
where
OP: FnOnce() -> R,
H: FnOnce(&TaskDeps<K>) -> Fingerprint,
H: Hash,
{
debug_assert!(!cx.is_eval_always(dep_kind));

if let Some(ref data) = self.data {
let task_deps = Lock::new(TaskDeps::default());
let result = K::with_deps(TaskDepsRef::Allow(&task_deps), op);
let task_deps = task_deps.into_inner();
let task_deps = task_deps.reads;

let dep_node_index = match task_deps.reads.len() {
let dep_node_index = match task_deps.len() {
0 => {
// Because the dep-node id of anon nodes is computed from the sets of its
// dependencies we already know what the ID of this dependency-less node is
Expand All @@ -429,23 +422,33 @@ impl<K: DepKind> DepGraph<K> {
}
1 => {
// When there is only one dependency, don't bother creating a node.
task_deps.reads[0]
task_deps[0]
}
_ => {
let hash_result = hash(&task_deps);
let mut hasher = StableHasher::new();
if let Some(hash) = hash {
hash.hash(&mut hasher);
} else {
// The dep node indices are hashed here instead of hashing the dep nodes of the
// dependencies. These indices may refer to different nodes per session, but this isn't
// a problem here because we that ensure the final dep node hash is per session only by
// combining it with the per session random number `anon_id_seed`. This hash only need
// to map the dependencies to a single value on a per session basis.
task_deps.hash(&mut hasher);
}

let target_dep_node = DepNode {
kind: dep_kind,
// Fingerprint::combine() is faster than sending Fingerprint
// through the StableHasher (at least as long as StableHasher
// is so slow).
hash: data.current.anon_id_seed.combine(hash_result).into(),
hash: data.current.anon_id_seed.combine(hasher.finish()).into(),
};

data.current.intern_new_node(
cx.profiler(),
target_dep_node,
task_deps.reads,
task_deps,
Fingerprint::ZERO,
)
}
Expand Down
22 changes: 4 additions & 18 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ use rustc_middle::ty::{self, EarlyBinder, PolyProjectionPredicate, ToPolyTraitRe
use rustc_middle::ty::{Ty, TyCtxt, TypeFoldable, TypeVisitable};
use rustc_span::symbol::sym;

use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::StableHasher;
use std::cell::{Cell, RefCell};
use std::cmp;
use std::fmt::{self, Display};
Expand All @@ -59,8 +57,6 @@ use std::iter;

pub use rustc_middle::traits::select::*;
use rustc_middle::ty::print::with_no_trimmed_paths;
use rustc_query_system::dep_graph::TaskDeps;

mod candidate_assembly;
mod confirmation;

Expand Down Expand Up @@ -1021,18 +1017,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
return Ok(cycle_result);
}

let (result, dep_node) = if cfg!(parallel_compiler) {
self.in_task_with_hash(
|this| this.evaluate_stack(&stack),
|_| {
let mut hasher = StableHasher::new();
(param_env, fresh_trait_pred).hash(&mut hasher);
hasher.finish()
},
)
} else {
self.in_task(|this| this.evaluate_stack(&stack))
};
let (result, dep_node) = self
.in_task_with_hash(|this| this.evaluate_stack(&stack), &(param_env, fresh_trait_pred));

let result = result?;

Expand Down Expand Up @@ -1359,13 +1345,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
fn in_task_with_hash<OP, R, H>(&mut self, op: OP, hash: H) -> (R, DepNodeIndex)
where
OP: FnOnce(&mut Self) -> R,
H: FnOnce(&TaskDeps<DepKind>) -> Fingerprint,
H: Hash,
{
let (result, dep_node) = self.tcx().dep_graph.with_hash_task(
self.tcx(),
DepKind::TraitSelect,
|| op(self),
hash,
Some(hash),
);
self.tcx().dep_graph.read_index(dep_node);
(result, dep_node)
Expand Down

0 comments on commit a4b1663

Please sign in to comment.