Add profiling metrics for, and speed up, Graphene graph compilation (#1924)

* Add compile time benchmark for the demo artwork

* Implement node input replacement batching

isometric-fountain      time:   [7.4307 ms 7.5072 ms 7.5974 ms]
                        change: [-19.302% -18.136% -16.903%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  6 (6.00%) high mild
  4 (4.00%) high severe

painted-dreams          time:   [1.8108 ms 1.8223 ms 1.8350 ms]
                        change: [-12.422% -11.524% -10.650%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 13 outliers among 100 measurements (13.00%)
  5 (5.00%) high mild
  8 (8.00%) high severe

procedural-string-lights
                        time:   [551.65 µs 560.58 µs 571.13 µs]
                        change: [-5.7783% -2.5770% +1.3136%] (p = 0.20 > 0.05)
                        No change in performance detected.
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) high mild
  5 (5.00%) high severe

red-dress               time:   [9.7951 ms 9.9006 ms 10.016 ms]
                        change: [-18.812% -17.558% -16.292%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  5 (5.00%) high mild

valley-of-spires        time:   [4.7294 ms 4.7837 ms 4.8442 ms]
                        change: [-16.889% -15.712% -14.615%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 16 outliers among 100 measurements (16.00%)
  11 (11.00%) high mild
  5 (5.00%) high severe

* Implement node dependency tracking

isometric-fountain      time:   [6.5695 ms 6.6418 ms 6.7227 ms]
                        change: [-13.426% -12.112% -10.681%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
  5 (5.00%) high mild
  5 (5.00%) high severe

painted-dreams          time:   [1.7406 ms 1.7566 ms 1.7742 ms]
                        change: [-4.7386% -3.5484% -2.3707%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 11 outliers among 100 measurements (11.00%)
  10 (10.00%) high mild
  1 (1.00%) high severe

procedural-string-lights
                        time:   [585.40 µs 590.98 µs 596.82 µs]
                        change: [-3.7739% +0.7591% +4.6293%] (p = 0.76 > 0.05)
                        No change in performance detected.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high mild

red-dress               time:   [8.4314 ms 8.5072 ms 8.5899 ms]
                        change: [-26.797% -25.374% -23.958%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 11 outliers among 100 measurements (11.00%)
  5 (5.00%) high mild
  6 (6.00%) high severe

valley-of-spires        time:   [4.3700 ms 4.4118 ms 4.4579 ms]
                        change: [-11.470% -10.198% -8.9826%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
  7 (7.00%) high mild
  5 (5.00%) high severe

* Remove clone and use rustc hash for storing nodes

isometric-fountain      time:   [5.9220 ms 5.9854 ms 6.0570 ms]
                        change: [-11.974% -10.539% -9.2291%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
  4 (4.00%) high mild
  8 (8.00%) high severe

painted-dreams          time:   [1.5337 ms 1.5470 ms 1.5618 ms]
                        change: [-11.901% -10.907% -9.9075%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  5 (5.00%) high mild
  4 (4.00%) high severe

procedural-string-lights
                        time:   [496.44 µs 501.44 µs 506.33 µs]
                        change: [-20.002% -15.633% -12.213%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  3 (3.00%) high mild
  1 (1.00%) high severe

red-dress               time:   [7.7037 ms 7.7871 ms 7.8774 ms]
                        change: [-11.906% -10.576% -9.2560%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  7 (7.00%) high mild
  1 (1.00%) high severe

valley-of-spires        time:   [3.9182 ms 3.9501 ms 3.9851 ms]
                        change: [-14.615% -13.075% -11.500%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high severe

* Fix test depending on stable node order

* Simplify flattening

* Remove unused dependant vec size safeguards

* Improve topological sort and make assert debug only

isometric-fountain      time:   [2.9515 ms 2.9971 ms 3.0459 ms]
                        change: [-61.270% -60.533% -59.747%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 11 outliers among 100 measurements (11.00%)
  7 (7.00%) high mild
  4 (4.00%) high severe

Benchmarking painted-dreams: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 8.1s, enable flat sampling, or reduce sample count to 50.
painted-dreams          time:   [1.1134 ms 1.1322 ms 1.1494 ms]
                        change: [-40.991% -37.660% -34.129%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high severe

procedural-string-lights
                        time:   [391.88 µs 397.80 µs 403.84 µs]
                        change: [-19.953% -17.762% -14.930%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  3 (3.00%) high mild
  3 (3.00%) high severe

red-dress               time:   [3.0722 ms 3.1179 ms 3.1707 ms]
                        change: [-59.878% -59.168% -58.422%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
  5 (5.00%) high mild
  7 (7.00%) high severe

valley-of-spires        time:   [2.0234 ms 2.0470 ms 2.0737 ms]
                        change: [-48.994% -47.910% -46.907%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  2 (2.00%) high mild
  7 (7.00%) high severe

* Improve id remapping logic by reusing id mapping

isometric-fountain      time:   [2.2464 ms 2.2841 ms 2.3260 ms]
                        change: [-25.693% -23.790% -22.024%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
  5 (5.00%) high mild
  1 (1.00%) high severe

Benchmarking painted-dreams: Warming up for 3.0000 s
Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 7.3s, enable flat sampling, or reduce sample count to 50.
painted-dreams          time:   [869.96 µs 886.99 µs 902.36 µs]
                        change: [-26.928% -19.590% -8.0737%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high severe

procedural-string-lights
                        time:   [291.60 µs 296.60 µs 302.45 µs]
                        change: [-28.175% -25.168% -22.078%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  2 (2.00%) high mild
  6 (6.00%) high severe

red-dress               time:   [2.7946 ms 2.8356 ms 2.8800 ms]
                        change: [-10.991% -9.0546% -6.9757%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  7 (7.00%) high mild

valley-of-spires        time:   [1.5583 ms 1.5801 ms 1.6039 ms]
                        change: [-24.165% -22.811% -21.213%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  7 (7.00%) high mild

* Remove flame graph file
This commit is contained in:
Dennis Kobert
2024-08-12 12:07:36 +02:00
committed by GitHub
parent e28e841e3b
commit da13f21486
14 changed files with 699 additions and 304 deletions

View File

@@ -3,6 +3,7 @@ use crate::document::{NodeId, OriginalLocation};
use dyn_any::DynAny;
use graphene_core::*;
use rustc_hash::{FxHashMap, FxHashSet};
#[cfg(feature = "serde")]
use std::borrow::Cow;
@@ -341,9 +342,16 @@ impl ProtoNode {
}
}
#[derive(Clone, Copy, PartialEq)]
enum NodeState {
Unvisited,
Visiting,
Visited,
}
impl ProtoNetwork {
fn check_ref(&self, ref_id: &NodeId, id: &NodeId) {
assert!(
debug_assert!(
self.nodes.iter().any(|(check_id, _)| check_id == ref_id),
"Node id:{id} has a reference which uses node id:{ref_id} which doesn't exist in network {self:#?}"
);
@@ -398,6 +406,7 @@ impl ProtoNetwork {
}
}
// TODO: Remsove
/// Create a hashmap with the list of nodes this proto network depends on/uses as inputs.
pub fn collect_inwards_edges(&self) -> HashMap<NodeId, Vec<NodeId>> {
let mut edges: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
@@ -420,6 +429,35 @@ impl ProtoNetwork {
edges
}
fn collect_inwards_edges_with_mapping(&self) -> (Vec<Vec<usize>>, FxHashMap<NodeId, usize>) {
let mut id_map = FxHashMap::with_capacity_and_hasher(self.nodes.len(), Default::default());
// Create dense mapping
id_map = self.nodes.iter().enumerate().map(|(idx, (id, _))| (*id, idx)).collect();
// Collect inwards edges using dense indices
let mut inwards_edges = vec![Vec::new(); self.nodes.len()];
for (node_id, node) in &self.nodes {
let node_index = id_map[node_id];
match &node.input {
ProtoNodeInput::Node(ref_id) | ProtoNodeInput::NodeLambda(ref_id) => {
self.check_ref(ref_id, &NodeId(node_index as u64));
inwards_edges[node_index].push(id_map[ref_id]);
}
_ => {}
}
if let ConstructionArgs::Nodes(ref_nodes) = &node.construction_args {
for (ref_id, _) in ref_nodes {
self.check_ref(ref_id, &NodeId(node_index as u64));
inwards_edges[node_index].push(id_map[ref_id]);
}
}
}
(inwards_edges, id_map)
}
/// Inserts a [`graphene_core::structural::ComposeNode`] for each node that has a [`ProtoNodeInput::Node`]. The compose node evaluates the first node, and then sends the result into the second node.
pub fn resolve_inputs(&mut self) -> Result<(), String> {
// Perform topological sort once
@@ -487,35 +525,43 @@ impl ProtoNetwork {
}
});
}
// Based on https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
// This approach excludes nodes that are not connected
pub fn topological_sort(&self) -> Result<Vec<NodeId>, String> {
let mut sorted = Vec::new();
let inwards_edges = self.collect_inwards_edges();
fn visit(node_id: NodeId, temp_marks: &mut HashSet<NodeId>, sorted: &mut Vec<NodeId>, inwards_edges: &HashMap<NodeId, Vec<NodeId>>, network: &ProtoNetwork) -> Result<(), String> {
if sorted.contains(&node_id) {
return Ok(());
};
if temp_marks.contains(&node_id) {
return Err(format!("Cycle detected {inwards_edges:#?}, {network:#?}"));
}
pub fn topological_sort(&self) -> Result<(Vec<NodeId>, FxHashMap<NodeId, usize>), String> {
let (inwards_edges, id_map) = self.collect_inwards_edges_with_mapping();
let mut sorted = Vec::with_capacity(self.nodes.len());
let mut stack = vec![id_map[&self.output]];
let mut state = vec![NodeState::Unvisited; self.nodes.len()];
if let Some(dependencies) = inwards_edges.get(&node_id) {
temp_marks.insert(node_id);
for &dependant in dependencies {
visit(dependant, temp_marks, sorted, inwards_edges, network)?;
while let Some(&node_index) = stack.last() {
match state[node_index] {
NodeState::Unvisited => {
state[node_index] = NodeState::Visiting;
for &dep_index in inwards_edges[node_index].iter().rev() {
match state[dep_index] {
NodeState::Visiting => {
return Err(format!("Cycle detected involving node {}", self.nodes[dep_index].0));
}
NodeState::Unvisited => {
stack.push(dep_index);
}
NodeState::Visited => {}
}
}
}
NodeState::Visiting => {
stack.pop();
state[node_index] = NodeState::Visited;
sorted.push(NodeId(node_index as u64));
}
NodeState::Visited => {
stack.pop();
}
temp_marks.remove(&node_id);
}
sorted.push(node_id);
Ok(())
}
if !self.nodes.iter().any(|(id, _)| *id == self.output) {
return Err(format!("Output id {} does not exist", self.output));
}
visit(self.output, &mut HashSet::new(), &mut sorted, &inwards_edges, self)?;
Ok(sorted)
Ok((sorted, id_map))
}
fn is_topologically_sorted(&self) -> bool {
@@ -536,57 +582,36 @@ impl ProtoNetwork {
true
}
/*// Based on https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm
pub fn topological_sort(&self) -> Vec<NodeId> {
let mut sorted = Vec::new();
let outwards_edges = self.collect_outwards_edges();
let mut inwards_edges = self.collect_inwards_edges();
let mut no_incoming_edges: Vec<_> = self.nodes.iter().map(|entry| entry.0).filter(|id| !inwards_edges.contains_key(id)).collect();
assert_ne!(no_incoming_edges.len(), 0, "Acyclic graphs must have at least one node with no incoming edge");
while let Some(node_id) = no_incoming_edges.pop() {
sorted.push(node_id);
if let Some(outwards_edges) = outwards_edges.get(&node_id) {
for &ref_id in outwards_edges {
let dependencies = inwards_edges.get_mut(&ref_id).unwrap();
dependencies.retain(|&id| id != node_id);
if dependencies.is_empty() {
no_incoming_edges.push(ref_id)
}
}
}
}
sorted
}*/
/// Sort the nodes vec so it is in a topological order. This ensures that no node takes an input from a node that is found later in the list.
fn reorder_ids(&mut self) -> Result<(), String> {
let order = self.topological_sort()?;
let (order, id_map) = self.topological_sort()?;
// Map of node ids to their current index in the nodes vector
let current_positions: HashMap<_, _> = self.nodes.iter().enumerate().map(|(pos, (id, _))| (*id, pos)).collect();
// // Map of node ids to their current index in the nodes vector
// let current_positions: FxHashMap<_, _> = self.nodes.iter().enumerate().map(|(pos, (id, _))| (*id, pos)).collect();
// Map of node ids to their new index based on topological order
let new_positions: HashMap<_, _> = order.iter().enumerate().map(|(pos, id)| (*id, NodeId(pos as u64))).collect();
// // Map of node ids to their new index based on topological order
let new_positions: FxHashMap<_, _> = order.iter().enumerate().map(|(pos, id)| (self.nodes[id.0 as usize].0, pos)).collect();
// assert_eq!(id_map, current_positions);
// Create a new nodes vector based on the topological order
let mut new_nodes = Vec::with_capacity(order.len());
for (index, &id) in order.iter().enumerate() {
let current_pos = *current_positions.get(&id).unwrap();
new_nodes.push((NodeId(index as u64), self.nodes[current_pos].1.clone()));
let mut node = std::mem::take(&mut self.nodes[id.0 as usize].1);
// Update node references to reflect the new order
node.map_ids(|id| NodeId(*new_positions.get(&id).expect("node not found in lookup table") as u64), false);
new_nodes.push((NodeId(index as u64), node));
}
// Update node references to reflect the new order
new_nodes.iter_mut().for_each(|(_, node)| {
node.map_ids(|id| *new_positions.get(&id).expect("node not found in lookup table"), false);
});
// new_nodes.iter_mut().for_each(|(_, node)| {
// node.map_ids(|id| *new_positions.get(&id).expect("node not found in lookup table"), false);
// });
// Update the nodes vector and other references
self.nodes = new_nodes;
self.inputs = self.inputs.iter().filter_map(|id| new_positions.get(id).copied()).collect();
self.output = *new_positions.get(&self.output).unwrap();
self.inputs = self.inputs.iter().filter_map(|id| new_positions.get(id).map(|x| NodeId(*x as u64))).collect();
self.output = NodeId(*new_positions.get(&self.output).unwrap() as u64);
assert_eq!(order.len(), self.nodes.len());
Ok(())
@@ -890,7 +915,8 @@ mod test {
#[test]
fn topological_sort() {
let construction_network = test_network();
let sorted = construction_network.topological_sort().expect("Error when calling 'topological_sort' on 'construction_network.");
let (sorted, _) = construction_network.topological_sort().expect("Error when calling 'topological_sort' on 'construction_network.");
let sorted: Vec<_> = sorted.iter().map(|x| construction_network.nodes[x.0 as usize].0).collect();
println!("{sorted:#?}");
assert_eq!(sorted, vec![NodeId(14), NodeId(10), NodeId(11), NodeId(1)]);
}
@@ -907,7 +933,8 @@ mod test {
fn id_reordering() {
let mut construction_network = test_network();
construction_network.reorder_ids().expect("Error when calling 'reorder_ids' on 'construction_network.");
let sorted = construction_network.topological_sort().expect("Error when calling 'topological_sort' on 'construction_network.");
let (sorted, _) = construction_network.topological_sort().expect("Error when calling 'topological_sort' on 'construction_network.");
let sorted: Vec<_> = sorted.iter().map(|x| construction_network.nodes[x.0 as usize].0).collect();
println!("nodes: {:#?}", construction_network.nodes);
assert_eq!(sorted, vec![NodeId(0), NodeId(1), NodeId(2), NodeId(3)]);
let ids: Vec<_> = construction_network.nodes.iter().map(|(id, _)| *id).collect();
@@ -922,7 +949,7 @@ mod test {
let mut construction_network = test_network();
construction_network.reorder_ids().expect("Error when calling 'reorder_ids' on 'construction_network.");
construction_network.reorder_ids().expect("Error when calling 'reorder_ids' on 'construction_network.");
let sorted = construction_network.topological_sort().expect("Error when calling 'topological_sort' on 'construction_network.");
let (sorted, _) = construction_network.topological_sort().expect("Error when calling 'topological_sort' on 'construction_network.");
assert_eq!(sorted, vec![NodeId(0), NodeId(1), NodeId(2), NodeId(3)]);
let ids: Vec<_> = construction_network.nodes.iter().map(|(id, _)| *id).collect();
println!("{ids:#?}");