From 2259bb0f6987af3a0dbcf152d653dae10c3f8b25 Mon Sep 17 00:00:00 2001 From: Lucas Villa Real Date: Mon, 26 Feb 2024 14:58:18 -0300 Subject: [PATCH] Skip verification on DAG::set_validate(false) Introduce a new API set_validate(validate: bool) so that calls to DAG::verify() can be skipped. This has been observed to lead to time savings of more than two orders of magnitude on a graph with only 250 nodes (240 secs -> ~2 secs). --- layout/src/adt/dag.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/layout/src/adt/dag.rs b/layout/src/adt/dag.rs index c3c9b14..cd5df37 100644 --- a/layout/src/adt/dag.rs +++ b/layout/src/adt/dag.rs @@ -13,6 +13,9 @@ pub struct DAG { /// Places nodes in levels. ranks: RankType, + + /// Perform validation checks. + validate: bool, } /// Used by users to keep track of nodes that are saved in the DAG. @@ -78,9 +81,14 @@ impl DAG { DAG { nodes: Vec::new(), ranks: Vec::new(), + validate: true, } } + pub fn set_validate(&mut self, validate: bool) { + self.validate = validate; + } + pub fn clear(&mut self) { self.nodes.clear(); self.ranks.clear(); @@ -163,24 +171,26 @@ impl DAG { } pub fn verify(&self) { - // Check that the node indices are valid. - for node in &self.nodes { - for edge in &node.successors { - assert!(edge.idx < self.nodes.len()); + if self.validate { + // Check that the node indices are valid. + for node in &self.nodes { + for edge in &node.successors { + assert!(edge.idx < self.nodes.len()); + } } - } - // Check that the graph is a DAG. - for (i, node) in self.nodes.iter().enumerate() { - let from = NodeHandle::from(i); - for dest in node.successors.iter() { - let reachable = self.is_reachable(*dest, from) && from != *dest; - assert!(!reachable, "We found a cycle!"); + // Check that the graph is a DAG. + for (i, node) in self.nodes.iter().enumerate() { + let from = NodeHandle::from(i); + for dest in node.successors.iter() { + let reachable = self.is_reachable(*dest, from) && from != *dest; + assert!(!reachable, "We found a cycle!"); + } } - } - // Make sure that all of the nodes are in ranks. - assert_eq!(self.count_nodes_in_ranks(), self.len()); + // Make sure that all of the nodes are in ranks. + assert_eq!(self.count_nodes_in_ranks(), self.len()); + } } pub fn len(&self) -> usize {