From daf152462276069275334c70819db1ebcdd77e8b Mon Sep 17 00:00:00 2001 From: Carter Canedy Date: Sat, 18 Jan 2025 09:58:15 -0800 Subject: [PATCH] optimize subgraph collection --- crates/project/src/graph.rs | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/crates/project/src/graph.rs b/crates/project/src/graph.rs index 94b96eb13b1fb..5fad8f9110092 100644 --- a/crates/project/src/graph.rs +++ b/crates/project/src/graph.rs @@ -88,33 +88,24 @@ impl Graph { return graph; }; - let mut nodes = vec![]; let mut graph = Self::new(); - self.collect_subgraph_nodes(start_node, &mut nodes); - - for node in nodes { - graph.add_node(node); - if let Some(neighbors) = self.adjacencies.get(&node) { - for neighbor in neighbors { - graph.add_edge(node, *neighbor); - } - } - } + self.collect_subgraph_nodes(&mut graph, start_node); graph } - fn collect_subgraph_nodes(&self, node: u32, nodes: &mut Vec) { - if !nodes.contains(&node) { - nodes.push(node); + fn collect_subgraph_nodes(&self, other: &mut Self, node: u32) { + if !other.adjacencies.contains_key(&node) { + other.add_node(node); let Some(neighbors) = self.adjacencies.get(&node) else { return; }; for neighbor in neighbors { - self.collect_subgraph_nodes(*neighbor, nodes); + self.collect_subgraph_nodes(other, *neighbor); + other.add_edge(node, *neighbor); } } } @@ -207,9 +198,9 @@ mod graph_test { [2, 3], [2, 1], [4, 1], - [3, 4], - [4, 5], - [2, 5] + [2, 4], + [5, 4], + [5, 1] ]; let mut graph = Graph::new(); @@ -218,6 +209,6 @@ mod graph_test { graph.add_edge(*src, *dst); } - assert_eq!(graph.topo_sort().unwrap(), vec![2, 3, 4, 5, 1]); + assert_eq!(graph.topo_sort().unwrap(), vec![2, 3, 5, 4, 1]); } }