Unverified Commit c6ac0b9f authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

SingleModuleGraph: yield edge weights during traversal (#74620)

This adds the edge weight (the `ChunkingType`) of the edge during
`traverse_edges_from_entry` and `traverse_edges_from_entry_topological`,
rather than only providing only the originating node.

Test Plan: Added a log to verify edge types were provided to these
visitors.
parent d737cefc
...@@ -174,9 +174,9 @@ impl NextDynamicGraph { ...@@ -174,9 +174,9 @@ impl NextDynamicGraph {
// module -> the client reference entry (if any) // module -> the client reference entry (if any)
let mut state_map = HashMap::new(); let mut state_map = HashMap::new();
graph.traverse_edges_from_entry(entry, |(parent_node, node)| { graph.traverse_edges_from_entry(entry, |parent_info, node| {
let module = node.module; let module = node.module;
let Some(parent_node) = parent_node else { let Some((parent_node, _)) = parent_info else {
state_map.insert(module, VisitState::Entry); state_map.insert(module, VisitState::Entry);
return GraphTraversalAction::Continue; return GraphTraversalAction::Continue;
}; };
...@@ -362,9 +362,9 @@ impl ClientReferencesGraph { ...@@ -362,9 +362,9 @@ impl ClientReferencesGraph {
entry, entry,
// state_map is `module -> Option< the current so parent server component >` // state_map is `module -> Option< the current so parent server component >`
&mut HashMap::new(), &mut HashMap::new(),
|(parent_node, node), state_map| { |parent_info, node, state_map| {
let module = node.module; let module = node.module;
let Some(parent_node) = parent_node else { let Some((parent_node, _)) = parent_info else {
state_map.insert(module, None); state_map.insert(module, None);
return GraphTraversalAction::Continue; return GraphTraversalAction::Continue;
}; };
...@@ -390,8 +390,8 @@ impl ClientReferencesGraph { ...@@ -390,8 +390,8 @@ impl ClientReferencesGraph {
_ => GraphTraversalAction::Continue, _ => GraphTraversalAction::Continue,
} }
}, },
|(parent_node, node), state_map| { |parent_info, node, state_map| {
let Some(parent_node) = parent_node else { let Some((parent_node, _)) = parent_info else {
return; return;
}; };
let parent_module = parent_node.module; let parent_module = parent_node.module;
......
...@@ -6,7 +6,7 @@ use std::{ ...@@ -6,7 +6,7 @@ use std::{
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use petgraph::{ use petgraph::{
graph::{DiGraph, NodeIndex}, graph::{DiGraph, EdgeIndex, NodeIndex},
visit::{Dfs, VisitMap, Visitable}, visit::{Dfs, VisitMap, Visitable},
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
...@@ -212,11 +212,18 @@ impl SingleModuleGraph { ...@@ -212,11 +212,18 @@ impl SingleModuleGraph {
/// target. /// target.
/// ///
/// This means that target nodes can be revisited (once per incoming edge). /// This means that target nodes can be revisited (once per incoming edge).
///
/// * `entry` - The entry module to start the traversal from
/// * `visitor` - Called before visiting the children of a node.
/// - Receives (originating &SingleModuleGraphNode, edge &ChunkingType), target
/// &SingleModuleGraphNode, state &S
/// - Can return [GraphTraversalAction]s to control the traversal
pub fn traverse_edges_from_entry<'a>( pub fn traverse_edges_from_entry<'a>(
&'a self, &'a self,
entry: ResolvedVc<Box<dyn Module>>, entry: ResolvedVc<Box<dyn Module>>,
mut visitor: impl FnMut( mut visitor: impl FnMut(
(Option<&'a SingleModuleGraphNode>, &'a SingleModuleGraphNode), Option<(&'a SingleModuleGraphNode, &'a ChunkingType)>,
&'a SingleModuleGraphNode,
) -> GraphTraversalAction, ) -> GraphTraversalAction,
) -> Result<()> { ) -> Result<()> {
let graph = &self.graph; let graph = &self.graph;
...@@ -226,14 +233,24 @@ impl SingleModuleGraph { ...@@ -226,14 +233,24 @@ impl SingleModuleGraph {
let mut discovered = graph.visit_map(); let mut discovered = graph.visit_map();
let entry_weight = graph.node_weight(entry_node).unwrap(); let entry_weight = graph.node_weight(entry_node).unwrap();
entry_weight.emit_issues(); entry_weight.emit_issues();
visitor((None, entry_weight)); visitor(None, entry_weight);
while let Some(node) = stack.pop() { while let Some(node) = stack.pop() {
let node_weight = graph.node_weight(node).unwrap(); let node_weight = graph.node_weight(node).unwrap();
if discovered.visit(node) { if discovered.visit(node) {
for succ in graph.neighbors(node).collect::<Vec<_>>() { let neighbors = {
let mut neighbors = vec![];
let mut walker = graph.neighbors(node).detach();
while let Some((edge, succ)) = walker.next(graph) {
neighbors.push((edge, succ));
}
neighbors
};
for (edge, succ) in neighbors {
let succ_weight = graph.node_weight(succ).unwrap(); let succ_weight = graph.node_weight(succ).unwrap();
let action = visitor((Some(node_weight), succ_weight)); let edge_weight = graph.edge_weight(edge).unwrap();
let action = visitor(Some((node_weight, edge_weight)), succ_weight);
if !discovered.is_visited(&succ) && action == GraphTraversalAction::Continue { if !discovered.is_visited(&succ) && action == GraphTraversalAction::Continue {
stack.push(succ); stack.push(succ);
} }
...@@ -251,16 +268,29 @@ impl SingleModuleGraph { ...@@ -251,16 +268,29 @@ impl SingleModuleGraph {
/// ///
/// Target nodes can be revisited (once per incoming edge). /// Target nodes can be revisited (once per incoming edge).
/// Edges are traversed in normal order, so should correspond to reference order. /// Edges are traversed in normal order, so should correspond to reference order.
///
/// * `entry` - The entry module to start the traversal from
/// * `state` - The state to be passed to the visitors
/// * `visit_preorder` - Called before visiting the children of a node.
/// - Receives: (originating &SingleModuleGraphNode, edge &ChunkingType), target
/// &SingleModuleGraphNode, state &S
/// - Can return [GraphTraversalAction]s to control the traversal
/// * `visit_postorder` - Called after visiting the children of a node. Return
/// - Receives: (originating &SingleModuleGraphNode, edge &ChunkingType), target
/// &SingleModuleGraphNode, state &S
/// - Can return [GraphTraversalAction]s to control the traversal
pub fn traverse_edges_from_entry_topological<'a, S>( pub fn traverse_edges_from_entry_topological<'a, S>(
&'a self, &'a self,
entry: ResolvedVc<Box<dyn Module>>, entry: ResolvedVc<Box<dyn Module>>,
state: &mut S, state: &mut S,
mut visit_preorder: impl FnMut( mut visit_preorder: impl FnMut(
(Option<&'a SingleModuleGraphNode>, &'a SingleModuleGraphNode), Option<(&'a SingleModuleGraphNode, &'a ChunkingType)>,
&'a SingleModuleGraphNode,
&mut S, &mut S,
) -> GraphTraversalAction, ) -> GraphTraversalAction,
mut visit_postorder: impl FnMut( mut visit_postorder: impl FnMut(
(Option<&'a SingleModuleGraphNode>, &'a SingleModuleGraphNode), Option<(&'a SingleModuleGraphNode, &'a ChunkingType)>,
&'a SingleModuleGraphNode,
&mut S, &mut S,
), ),
) -> Result<()> { ) -> Result<()> {
...@@ -272,39 +302,47 @@ impl SingleModuleGraph { ...@@ -272,39 +302,47 @@ impl SingleModuleGraph {
ExpandAndVisit, ExpandAndVisit,
} }
let mut stack: Vec<(ReverseTopologicalPass, Option<NodeIndex>, NodeIndex)> = #[allow(clippy::type_complexity)] // This is a temporary internal structure
vec![(ReverseTopologicalPass::ExpandAndVisit, None, entry_node)]; let mut stack: Vec<(
ReverseTopologicalPass,
Option<(NodeIndex, EdgeIndex)>,
NodeIndex,
)> = vec![(ReverseTopologicalPass::ExpandAndVisit, None, entry_node)];
let mut expanded = HashSet::new(); let mut expanded = HashSet::new();
while let Some((pass, parent, current)) = stack.pop() { while let Some((pass, parent, current)) = stack.pop() {
match pass { match pass {
ReverseTopologicalPass::Visit => { ReverseTopologicalPass::Visit => {
visit_postorder( visit_postorder(
( parent.map(|parent| {
parent.map(|parent| graph.node_weight(parent).unwrap()), (
graph.node_weight(current).unwrap(), graph.node_weight(parent.0).unwrap(),
), graph.edge_weight(parent.1).unwrap(),
)
}),
graph.node_weight(current).unwrap(),
state, state,
); );
} }
ReverseTopologicalPass::ExpandAndVisit => { ReverseTopologicalPass::ExpandAndVisit => {
let action = visit_preorder( let action = visit_preorder(
( parent.map(|parent| {
parent.map(|parent| graph.node_weight(parent).unwrap()), (
graph.node_weight(current).unwrap(), graph.node_weight(parent.0).unwrap(),
), graph.edge_weight(parent.1).unwrap(),
)
}),
graph.node_weight(current).unwrap(),
state, state,
); );
stack.push((ReverseTopologicalPass::Visit, parent, current)); stack.push((ReverseTopologicalPass::Visit, parent, current));
if expanded.insert(current) && action == GraphTraversalAction::Continue { if expanded.insert(current) && action == GraphTraversalAction::Continue {
stack.extend( stack.extend(iter_neighbors(graph, current).map(|(edge, child)| {
graph (
.neighbors(current) ReverseTopologicalPass::ExpandAndVisit,
// .collect::<Vec<_>>() Some((current, edge)),
// .rev() child,
.map(|child| { )
(ReverseTopologicalPass::ExpandAndVisit, Some(current), child) }));
}),
);
} }
} }
} }
...@@ -533,3 +571,11 @@ impl Visit<SingleModuleGraphBuilderNode> for SingleModuleGraphBuilder { ...@@ -533,3 +571,11 @@ impl Visit<SingleModuleGraphBuilderNode> for SingleModuleGraphBuilder {
} }
} }
} }
fn iter_neighbors<N, E>(
graph: &DiGraph<N, E>,
node: NodeIndex,
) -> impl Iterator<Item = (EdgeIndex, NodeIndex)> + '_ {
let mut walker = graph.neighbors(node).detach();
std::iter::from_fn(move || walker.next(graph))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment