Skip to content

Commit

Permalink
Skip verification on DAG::set_validate(false)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
lucasvr committed Feb 26, 2024
1 parent 08f2327 commit b2cce8e
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions layout/src/adt/dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b2cce8e

Please sign in to comment.