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

perf(dep_graph): Avoid allocating a set on when the number reads are … #69778

Merged
merged 2 commits into from
Mar 23, 2020
Merged
Changes from all commits
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
35 changes: 24 additions & 11 deletions src/librustc/dep_graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,7 @@ impl DepGraph {
{
if let Some(ref data) = self.data {
let (result, task_deps) = ty::tls::with_context(|icx| {
let task_deps = Lock::new(TaskDeps {
#[cfg(debug_assertions)]
node: None,
reads: SmallVec::new(),
read_set: Default::default(),
});
let task_deps = Lock::new(TaskDeps::default());

let r = {
let icx = ty::tls::ImplicitCtxt { task_deps: Some(&task_deps), ..icx.clone() };
Expand Down Expand Up @@ -968,7 +963,7 @@ pub enum WorkProductFileKind {
#[derive(Clone)]
struct DepNodeData {
node: DepNode,
edges: SmallVec<[DepNodeIndex; 8]>,
edges: EdgesVec,
fingerprint: Fingerprint,
}

Expand Down Expand Up @@ -1093,7 +1088,7 @@ impl CurrentDepGraph {
fn alloc_node(
&self,
dep_node: DepNode,
edges: SmallVec<[DepNodeIndex; 8]>,
edges: EdgesVec,
fingerprint: Fingerprint,
) -> DepNodeIndex {
debug_assert!(
Expand All @@ -1105,7 +1100,7 @@ impl CurrentDepGraph {
fn intern_node(
&self,
dep_node: DepNode,
edges: SmallVec<[DepNodeIndex; 8]>,
edges: EdgesVec,
fingerprint: Fingerprint,
) -> DepNodeIndex {
match self.node_to_node_index.get_shard_by_value(&dep_node).lock().entry(dep_node) {
Expand All @@ -1128,11 +1123,25 @@ impl DepGraphData {
let icx = if let Some(icx) = icx { icx } else { return };
if let Some(task_deps) = icx.task_deps {
let mut task_deps = task_deps.lock();
let task_deps = &mut *task_deps;
if cfg!(debug_assertions) {
self.current.total_read_count.fetch_add(1, Relaxed);
}
if task_deps.read_set.insert(source) {

// As long as we only have a low number of reads we can avoid doing a hash
// insert and potentially allocating/reallocating the hashmap
let new_read = if task_deps.reads.len() < TASK_DEPS_READS_CAP {
task_deps.reads.iter().all(|other| *other != source)
} else {
task_deps.read_set.insert(source)
};
if new_read {
task_deps.reads.push(source);
if task_deps.reads.len() == TASK_DEPS_READS_CAP {
// Fill `read_set` with what we have so far so we can use the hashset next
// time
task_deps.read_set.extend(task_deps.reads.iter().copied());
}

#[cfg(debug_assertions)]
{
Expand All @@ -1154,10 +1163,14 @@ impl DepGraphData {
}
}

/// The capacity of the `reads` field `SmallVec`
const TASK_DEPS_READS_CAP: usize = 8;
davidtwco marked this conversation as resolved.
Show resolved Hide resolved
type EdgesVec = SmallVec<[DepNodeIndex; TASK_DEPS_READS_CAP]>;
#[derive(Default)]
pub struct TaskDeps {
#[cfg(debug_assertions)]
node: Option<DepNode>,
reads: SmallVec<[DepNodeIndex; 8]>,
reads: EdgesVec,
read_set: FxHashSet<DepNodeIndex>,
}

Expand Down