perf: reach each sibling once when nesting waiting units
Searching the graph per sibling pair cost O(n^2) searches under a target with many dependencies. One reachable set per sibling makes the check a lookup. Release, 200 units: model projection 2.64 ms -> 0.75 ms, whole frame 3.02 ms -> 1.09 ms.
This commit is contained in:
1 parent
d3ac753fe2
commit
ba6e934412
1 file changed
+19
-14
@@ -2,9 +2,8 @@ use std::{collections::HashSet, time::Duration};
|
|||||||
|
|
||||||
use petgraph::{
|
use petgraph::{
|
||||||
Direction::Incoming,
|
Direction::Incoming,
|
||||||
algo::{DfsSpace, has_path_connecting},
|
|
||||||
graphmap::DiGraphMap,
|
graphmap::DiGraphMap,
|
||||||
visit::Visitable,
|
visit::{Dfs, Walker},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@@ -13,7 +12,11 @@ use super::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
type WaitingGraph<'a> = DiGraphMap<&'a str, ()>;
|
type WaitingGraph<'a> = DiGraphMap<&'a str, ()>;
|
||||||
type WaitingSearch<'a> = DfsSpace<&'a str, <WaitingGraph<'a> as Visitable>::Map>;
|
|
||||||
|
struct ReachableSibling<'a> {
|
||||||
|
unit: &'a str,
|
||||||
|
reaches: HashSet<&'a str>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum RowStatus {
|
pub enum RowStatus {
|
||||||
@@ -130,7 +133,6 @@ impl BootState {
|
|||||||
unblocked.clear();
|
unblocked.clear();
|
||||||
leftovers.clear();
|
leftovers.clear();
|
||||||
}
|
}
|
||||||
let mut space = DfsSpace::new(&graph);
|
|
||||||
let mut visited = HashSet::new();
|
let mut visited = HashSet::new();
|
||||||
let mut forest = Vec::new();
|
let mut forest = Vec::new();
|
||||||
stages
|
stages
|
||||||
@@ -142,7 +144,7 @@ impl BootState {
|
|||||||
if visited.contains(top) {
|
if visited.contains(top) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let stage = self.grow(top, &graph, &mut space, &mut visited);
|
let stage = self.grow(top, &graph, &mut visited);
|
||||||
if stage.row.status != RowStatus::Done || !stage.children.is_empty() {
|
if stage.row.status != RowStatus::Done || !stage.children.is_empty() {
|
||||||
forest.push(stage);
|
forest.push(stage);
|
||||||
}
|
}
|
||||||
@@ -154,7 +156,6 @@ impl BootState {
|
|||||||
&'a self,
|
&'a self,
|
||||||
unit: &'a str,
|
unit: &'a str,
|
||||||
graph: &WaitingGraph<'a>,
|
graph: &WaitingGraph<'a>,
|
||||||
space: &mut WaitingSearch<'a>,
|
|
||||||
visited: &mut HashSet<&'a str>,
|
visited: &mut HashSet<&'a str>,
|
||||||
) -> TreeNode {
|
) -> TreeNode {
|
||||||
visited.insert(unit);
|
visited.insert(unit);
|
||||||
@@ -167,20 +168,24 @@ impl BootState {
|
|||||||
status: record.row_status(),
|
status: record.row_status(),
|
||||||
chain: Vec::new(),
|
chain: Vec::new(),
|
||||||
};
|
};
|
||||||
let candidates: Vec<&str> = graph
|
let siblings: Vec<ReachableSibling> = graph
|
||||||
.neighbors(unit)
|
.neighbors(unit)
|
||||||
.filter(|dependency| !visited.contains(dependency) && !self.is_milestone(dependency))
|
.filter(|dependency| !visited.contains(dependency) && !self.is_milestone(dependency))
|
||||||
|
.map(|sibling| ReachableSibling {
|
||||||
|
unit: sibling,
|
||||||
|
reaches: Dfs::new(graph, sibling).iter(graph).collect(),
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let mut waits_on =
|
let mut children: Vec<&str> = siblings
|
||||||
|waiter, dependency| has_path_connecting(graph, waiter, dependency, Some(&mut *space));
|
|
||||||
let mut children: Vec<&str> = candidates
|
|
||||||
.iter()
|
.iter()
|
||||||
.copied()
|
|
||||||
.filter(|child| {
|
.filter(|child| {
|
||||||
!candidates.iter().any(|sibling| {
|
!siblings.iter().any(|sibling| {
|
||||||
sibling != child && waits_on(*sibling, *child) && !waits_on(*child, *sibling)
|
sibling.unit != child.unit
|
||||||
|
&& sibling.reaches.contains(child.unit)
|
||||||
|
&& !child.reaches.contains(sibling.unit)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
.map(|child| child.unit)
|
||||||
.collect();
|
.collect();
|
||||||
self.sort_siblings(&mut children);
|
self.sort_siblings(&mut children);
|
||||||
visited.extend(children.iter().copied());
|
visited.extend(children.iter().copied());
|
||||||
@@ -190,7 +195,7 @@ impl BootState {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|child| {
|
.map(|child| {
|
||||||
visited.remove(child);
|
visited.remove(child);
|
||||||
self.grow(child, graph, space, visited)
|
self.grow(child, graph, visited)
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user