diff --git a/src/librustc/dep_graph/query.rs b/src/librustc/dep_graph/query.rs index 283da1050aedc..ea83a4f8b3104 100644 --- a/src/librustc/dep_graph/query.rs +++ b/src/librustc/dep_graph/query.rs @@ -22,11 +22,10 @@ impl DepGraphQuery { pub fn new(nodes: &[DepNode], edges: &[(DepNode, DepNode)]) -> DepGraphQuery { - let mut graph = Graph::new(); + let mut graph = Graph::with_capacity(nodes.len(), edges.len()); let mut indices = FxHashMap(); for node in nodes { - indices.insert(node.clone(), graph.next_node_index()); - graph.add_node(node.clone()); + indices.insert(node.clone(), graph.add_node(node.clone())); } for &(ref source, ref target) in edges { diff --git a/src/librustc_data_structures/graph/mod.rs b/src/librustc_data_structures/graph/mod.rs index a5f83ce05f5e5..474622f366913 100644 --- a/src/librustc_data_structures/graph/mod.rs +++ b/src/librustc_data_structures/graph/mod.rs @@ -114,6 +114,13 @@ impl Graph { } } + pub fn with_capacity(nodes: usize, edges: usize) -> Graph { + Graph { + nodes: SnapshotVec::with_capacity(nodes), + edges: SnapshotVec::with_capacity(edges), + } + } + // # Simple accessors #[inline] diff --git a/src/librustc_data_structures/snapshot_vec.rs b/src/librustc_data_structures/snapshot_vec.rs index dac074ab91e1b..2da91918288ba 100644 --- a/src/librustc_data_structures/snapshot_vec.rs +++ b/src/librustc_data_structures/snapshot_vec.rs @@ -66,6 +66,13 @@ impl SnapshotVec { } } + pub fn with_capacity(n: usize) -> SnapshotVec { + SnapshotVec { + values: Vec::with_capacity(n), + undo_log: Vec::new(), + } + } + fn in_snapshot(&self) -> bool { !self.undo_log.is_empty() }