Skip to content

Commit a7817dd

Browse files
committed
rustc: Preallocate when building the dep graph
This commit alters the `query` function in the dep graph module to preallocate memory using `with_capacity` instead of relying on automatic growth. Discovered in #44576 it was found that for the syntex_syntax clean incremental benchmark the peak memory usage was found when the dep graph was being saved, particularly the `DepGraphQuery` data structure itself. PRs like #44142 which add more queries end up just making this much larger! I didn't see an immediately obvious way to reduce the size of the `DepGraphQuery` object, but it turns out that `with_capacity` helps quite a bit! Locally 831 MB was used [before] this commit, and 770 MB is in use at the peak of the compiler [after] this commit. That's a nice 7.5% improvement! This won't quite make up for the losses in #44142 but I figured it's a good start. [before]: https://gist.github.com/alexcrichton/2d2b9c7a65503761925c5a0bcfeb0d1e [before]: https://gist.github.com/alexcrichton/6da51f2a6184bfb81694cc44f06deb5b
1 parent 2d288a5 commit a7817dd

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/librustc/dep_graph/query.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ impl DepGraphQuery {
2222
pub fn new(nodes: &[DepNode],
2323
edges: &[(DepNode, DepNode)])
2424
-> DepGraphQuery {
25-
let mut graph = Graph::new();
25+
let mut graph = Graph::with_capacity(nodes.len(), edges.len());
2626
let mut indices = FxHashMap();
2727
for node in nodes {
28-
indices.insert(node.clone(), graph.next_node_index());
29-
graph.add_node(node.clone());
28+
indices.insert(node.clone(), graph.add_node(node.clone()));
3029
}
3130

3231
for &(ref source, ref target) in edges {

src/librustc_data_structures/graph/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ impl<N: Debug, E: Debug> Graph<N, E> {
114114
}
115115
}
116116

117+
pub fn with_capacity(nodes: usize, edges: usize) -> Graph<N, E> {
118+
Graph {
119+
nodes: SnapshotVec::with_capacity(nodes),
120+
edges: SnapshotVec::with_capacity(edges),
121+
}
122+
}
123+
117124
// # Simple accessors
118125

119126
#[inline]

src/librustc_data_structures/snapshot_vec.rs

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ impl<D: SnapshotVecDelegate> SnapshotVec<D> {
6666
}
6767
}
6868

69+
pub fn with_capacity(n: usize) -> SnapshotVec<D> {
70+
SnapshotVec {
71+
values: Vec::with_capacity(n),
72+
undo_log: Vec::new(),
73+
}
74+
}
75+
6976
fn in_snapshot(&self) -> bool {
7077
!self.undo_log.is_empty()
7178
}

0 commit comments

Comments
 (0)