Skip to content

Commit 78262e7

Browse files
committed
Use SmallVec for DepNodeIndex within dep_graph.
This avoids a decent number of allocations, enough to speed up incremental runs of many rustc-benchmarks, the best by 2%.
1 parent f9bfe84 commit 78262e7

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/librustc/dep_graph/graph.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use errors::DiagnosticBuilder;
1212
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1313
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1414
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
15+
use rustc_data_structures::small_vec::SmallVec;
1516
use rustc_data_structures::sync::{Lrc, RwLock, ReadGuard, Lock};
1617
use std::env;
1718
use std::hash::Hash;
@@ -131,7 +132,7 @@ impl DepGraph {
131132
let mut edges = Vec::new();
132133
for (index, edge_targets) in current_dep_graph.edges.iter_enumerated() {
133134
let from = current_dep_graph.nodes[index];
134-
for &edge_target in edge_targets {
135+
for &edge_target in edge_targets.iter() {
135136
let to = current_dep_graph.nodes[edge_target];
136137
edges.push((from, to));
137138
}
@@ -209,7 +210,7 @@ impl DepGraph {
209210
self.with_task_impl(key, cx, arg, false, task,
210211
|key| OpenTask::Regular(Lock::new(RegularOpenTask {
211212
node: key,
212-
reads: Vec::new(),
213+
reads: SmallVec::new(),
213214
read_set: FxHashSet(),
214215
})),
215216
|data, key, task| data.borrow_mut().complete_task(key, task))
@@ -230,7 +231,7 @@ impl DepGraph {
230231

231232
self.with_task_impl(key, cx, input, true, identity_fn,
232233
|_| OpenTask::Ignore,
233-
|data, key, _| data.borrow_mut().alloc_node(key, Vec::new()))
234+
|data, key, _| data.borrow_mut().alloc_node(key, SmallVec::new()))
234235
}
235236

236237
fn with_task_impl<'gcx, C, A, R>(
@@ -353,7 +354,7 @@ impl DepGraph {
353354
if let Some(ref data) = self.data {
354355
let (result, open_task) = ty::tls::with_context(|icx| {
355356
let task = OpenTask::Anon(Lock::new(AnonOpenTask {
356-
reads: Vec::new(),
357+
reads: SmallVec::new(),
357358
read_set: FxHashSet(),
358359
}));
359360

@@ -626,7 +627,7 @@ impl DepGraph {
626627

627628
debug_assert!(data.colors.borrow().get(prev_dep_node_index).is_none());
628629

629-
let mut current_deps = Vec::new();
630+
let mut current_deps = SmallVec::new();
630631

631632
for &dep_dep_node_index in prev_deps {
632633
let dep_dep_node_color = data.colors.borrow().get(dep_dep_node_index);
@@ -923,7 +924,7 @@ pub enum WorkProductFileKind {
923924

924925
pub(super) struct CurrentDepGraph {
925926
nodes: IndexVec<DepNodeIndex, DepNode>,
926-
edges: IndexVec<DepNodeIndex, Vec<DepNodeIndex>>,
927+
edges: IndexVec<DepNodeIndex, SmallVec<[DepNodeIndex; 8]>>,
927928
node_to_node_index: FxHashMap<DepNode, DepNodeIndex>,
928929
forbidden_edge: Option<EdgeFilter>,
929930

@@ -1061,7 +1062,7 @@ impl CurrentDepGraph {
10611062
} = task {
10621063
debug_assert_eq!(node, key);
10631064
let krate_idx = self.node_to_node_index[&DepNode::new_no_params(DepKind::Krate)];
1064-
self.alloc_node(node, vec![krate_idx])
1065+
self.alloc_node(node, SmallVec::one(krate_idx))
10651066
} else {
10661067
bug!("complete_eval_always_task() - Expected eval always task to be popped");
10671068
}
@@ -1107,7 +1108,7 @@ impl CurrentDepGraph {
11071108

11081109
fn alloc_node(&mut self,
11091110
dep_node: DepNode,
1110-
edges: Vec<DepNodeIndex>)
1111+
edges: SmallVec<[DepNodeIndex; 8]>)
11111112
-> DepNodeIndex {
11121113
debug_assert_eq!(self.edges.len(), self.nodes.len());
11131114
debug_assert_eq!(self.node_to_node_index.len(), self.nodes.len());
@@ -1122,12 +1123,12 @@ impl CurrentDepGraph {
11221123

11231124
pub struct RegularOpenTask {
11241125
node: DepNode,
1125-
reads: Vec<DepNodeIndex>,
1126+
reads: SmallVec<[DepNodeIndex; 8]>,
11261127
read_set: FxHashSet<DepNodeIndex>,
11271128
}
11281129

11291130
pub struct AnonOpenTask {
1130-
reads: Vec<DepNodeIndex>,
1131+
reads: SmallVec<[DepNodeIndex; 8]>,
11311132
read_set: FxHashSet<DepNodeIndex>,
11321133
}
11331134

0 commit comments

Comments
 (0)