Unverified Commit 27a271bd authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Rename `traverse_edges_from_entries_topological` to…

[turbopack] Rename `traverse_edges_from_entries_topological` to `traverse_edges_from_entries_dfs` (#81481)

## Rename `traverse_edges_from_entries_topological` to `traverse_edges_from_entries_dfs`

This PR renames the `traverse_edges_from_entries_topological` method to `traverse_edges_from_entries_dfs` to better reflect its actual behavior. The method performs a depth-first search traversal rather than a topological sort.

The PR also:
- Updates the method documentation to clarify that it performs a DFS traversal
- Makes `ClientReferenceMapType` derive `Copy` for better ergonomics
- Removes unnecessary `ResolvedVc::upcast` call in client references mapping
- Renames some variables for clarity (`actions` → `client_references`)
- Removes unused `module_graph_for_entries` function
- Adds some tests for the traversal routine.  Not very interesting for this traversal but should make it easier to test future traversals.

These changes improve code clarity and correctness by ensuring the method name accurately describes its behavior.
parent a1339ec6
...@@ -9639,10 +9639,12 @@ dependencies = [ ...@@ -9639,10 +9639,12 @@ dependencies = [
"turbo-prehash", "turbo-prehash",
"turbo-rcstr", "turbo-rcstr",
"turbo-tasks", "turbo-tasks",
"turbo-tasks-backend",
"turbo-tasks-build", "turbo-tasks-build",
"turbo-tasks-env", "turbo-tasks-env",
"turbo-tasks-fs", "turbo-tasks-fs",
"turbo-tasks-hash", "turbo-tasks-hash",
"turbo-tasks-testing",
"urlencoding", "urlencoding",
] ]
......
...@@ -13,7 +13,7 @@ use turbopack::css::chunk::CssChunkPlaceable; ...@@ -13,7 +13,7 @@ use turbopack::css::chunk::CssChunkPlaceable;
use turbopack_core::{module::Module, module_graph::SingleModuleGraph}; use turbopack_core::{module::Module, module_graph::SingleModuleGraph};
#[derive( #[derive(
Clone, Serialize, Deserialize, Eq, PartialEq, TraceRawVcs, ValueDebugFormat, NonLocalValue, Copy, Clone, Serialize, Deserialize, Eq, PartialEq, TraceRawVcs, ValueDebugFormat, NonLocalValue,
)] )]
pub enum ClientReferenceMapType { pub enum ClientReferenceMapType {
EcmascriptClientReference { EcmascriptClientReference {
...@@ -31,7 +31,7 @@ pub struct ClientReferencesSet(FxHashMap<ResolvedVc<Box<dyn Module>>, ClientRefe ...@@ -31,7 +31,7 @@ pub struct ClientReferencesSet(FxHashMap<ResolvedVc<Box<dyn Module>>, ClientRefe
pub async fn map_client_references( pub async fn map_client_references(
graph: Vc<SingleModuleGraph>, graph: Vc<SingleModuleGraph>,
) -> Result<Vc<ClientReferencesSet>> { ) -> Result<Vc<ClientReferencesSet>> {
let actions = graph let client_references = graph
.await? .await?
.iter_nodes() .iter_nodes()
.map(|node| async move { .map(|node| async move {
...@@ -52,9 +52,9 @@ pub async fn map_client_references( ...@@ -52,9 +52,9 @@ pub async fn map_client_references(
{ {
Ok(Some(( Ok(Some((
module, module,
ClientReferenceMapType::CssClientReference(ResolvedVc::upcast( ClientReferenceMapType::CssClientReference(
client_reference_module.await?.client_module, client_reference_module.await?.client_module,
)), ),
))) )))
} else if let Some(server_component) = } else if let Some(server_component) =
ResolvedVc::try_downcast_type::<NextServerComponentModule>(module) ResolvedVc::try_downcast_type::<NextServerComponentModule>(module)
...@@ -69,5 +69,5 @@ pub async fn map_client_references( ...@@ -69,5 +69,5 @@ pub async fn map_client_references(
}) })
.try_flat_join() .try_flat_join()
.await?; .await?;
Ok(Vc::cell(actions.into_iter().collect())) Ok(Vc::cell(client_references.into_iter().collect()))
} }
...@@ -291,7 +291,7 @@ impl ClientReferencesGraph { ...@@ -291,7 +291,7 @@ impl ClientReferencesGraph {
let mut client_references_by_server_component = let mut client_references_by_server_component =
FxIndexMap::from_iter([(None, Vec::new())]); FxIndexMap::from_iter([(None, Vec::new())]);
graph.traverse_edges_from_entries_topological( graph.traverse_edges_from_entries_dfs(
entries, entries,
// state_map is `module -> Option< the current so parent server component >` // state_map is `module -> Option< the current so parent server component >`
&mut FxHashMap::default(), &mut FxHashMap::default(),
......
...@@ -960,18 +960,6 @@ impl Project { ...@@ -960,18 +960,6 @@ impl Project {
}) })
} }
#[turbo_tasks::function]
pub async fn module_graph_for_entries(
self: Vc<Self>,
entries: Vc<GraphEntries>,
) -> Result<Vc<ModuleGraph>> {
Ok(if *self.per_page_module_graph().await? {
ModuleGraph::from_modules(entries, self.next_mode().await?.is_production())
} else {
*self.whole_app_module_graphs().await?.full
})
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn whole_app_module_graphs(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraphs>> { pub async fn whole_app_module_graphs(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraphs>> {
async move { async move {
......
...@@ -50,6 +50,8 @@ turbo-tasks-build = { workspace = true } ...@@ -50,6 +50,8 @@ turbo-tasks-build = { workspace = true }
[dev-dependencies] [dev-dependencies]
rstest = { workspace = true } rstest = { workspace = true }
tokio = { workspace = true } tokio = { workspace = true }
turbo-tasks-testing = { workspace = true }
turbo-tasks-backend = { workspace = true }
[features] [features]
default = [] default = []
......
...@@ -220,7 +220,7 @@ pub async fn chunk_group_content( ...@@ -220,7 +220,7 @@ pub async fn chunk_group_content(
entries.push(module_batches_graph.get_entry_index(entry).await?); entries.push(module_batches_graph.get_entry_index(entry).await?);
} }
module_batches_graph.traverse_edges_from_entries_topological( module_batches_graph.traverse_edges_from_entries_dfs(
entries, entries,
&mut state, &mut state,
|parent_info, &node, state| { |parent_info, &node, state| {
......
...@@ -73,7 +73,7 @@ async fn compute_async_module_info_single( ...@@ -73,7 +73,7 @@ async fn compute_async_module_info_single(
// modules in the SCC is async. // modules in the SCC is async.
let mut async_modules = self_async_modules; let mut async_modules = self_async_modules;
graph.traverse_edges_from_entries_topological( graph.traverse_edges_from_entries_dfs(
graph.entry_modules(), graph.entry_modules(),
&mut (), &mut (),
|_, _, _| Ok(GraphTraversalAction::Continue), |_, _, _| Ok(GraphTraversalAction::Continue),
......
...@@ -251,7 +251,7 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc< ...@@ -251,7 +251,7 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
let mut visited = FxHashSet::default(); let mut visited = FxHashSet::default();
module_graph module_graph
.traverse_edges_from_entries_topological( .traverse_edges_from_entries_dfs(
chunk_group.entries(), chunk_group.entries(),
&mut (), &mut (),
|parent_info, node, _| { |parent_info, node, _| {
...@@ -332,7 +332,7 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc< ...@@ -332,7 +332,7 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
FxHashSet::with_capacity_and_hasher(module_merged_groups.len(), Default::default()); FxHashSet::with_capacity_and_hasher(module_merged_groups.len(), Default::default());
module_graph module_graph
.traverse_edges_from_entries_topological( .traverse_edges_from_entries_dfs(
entries, entries,
&mut (), &mut (),
|_, _, _| Ok(GraphTraversalAction::Continue), |_, _, _| Ok(GraphTraversalAction::Continue),
......
...@@ -123,7 +123,7 @@ impl ModuleBatchesGraph { ...@@ -123,7 +123,7 @@ impl ModuleBatchesGraph {
// Clippy complains but there's a type error without the bound // Clippy complains but there's a type error without the bound
#[allow(clippy::implied_bounds_in_impls)] #[allow(clippy::implied_bounds_in_impls)]
/// Traverses all reachable edges in topological order. The preorder visitor can be used to /// Traverses all reachable edges in dfs order. The preorder visitor can be used to
/// forward state down the graph, and to skip subgraphs /// forward state down the graph, and to skip subgraphs
/// ///
/// Use this to collect batches/modules in evaluation order. /// Use this to collect batches/modules in evaluation order.
...@@ -131,7 +131,7 @@ impl ModuleBatchesGraph { ...@@ -131,7 +131,7 @@ impl ModuleBatchesGraph {
/// 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 /// * `entries` - The entry modules to start the traversal from
/// * `state` - The state to be passed to the visitors /// * `state` - The state to be passed to the visitors
/// * `visit_preorder` - Called before visiting the children of a node. /// * `visit_preorder` - Called before visiting the children of a node.
/// - Receives: (originating &ModuleBatchesGraphNode, edge &ChunkingType), target /// - Receives: (originating &ModuleBatchesGraphNode, edge &ChunkingType), target
...@@ -140,7 +140,7 @@ impl ModuleBatchesGraph { ...@@ -140,7 +140,7 @@ impl ModuleBatchesGraph {
/// * `visit_postorder` - Called after visiting the children of a node. Return /// * `visit_postorder` - Called after visiting the children of a node. Return
/// - Receives: (originating &ModuleBatchesGraphNode, edge &ChunkingType), target /// - Receives: (originating &ModuleBatchesGraphNode, edge &ChunkingType), target
/// &ModuleBatchesGraphNode, state &S /// &ModuleBatchesGraphNode, state &S
pub fn traverse_edges_from_entries_topological<'a, S>( pub fn traverse_edges_from_entries_dfs<'a, S>(
&'a self, &'a self,
entries: impl IntoIterator< entries: impl IntoIterator<
Item = NodeIndex, Item = NodeIndex,
...@@ -160,20 +160,16 @@ impl ModuleBatchesGraph { ...@@ -160,20 +160,16 @@ impl ModuleBatchesGraph {
) -> Result<()> { ) -> Result<()> {
let graph = &self.graph; let graph = &self.graph;
enum ReverseTopologicalPass { enum ReverseDFSPass {
Visit, Visit,
ExpandAndVisit, ExpandAndVisit,
} }
let entries = entries.into_iter(); let entries = entries.into_iter();
#[allow(clippy::type_complexity)] // This is a temporary internal structure #[allow(clippy::type_complexity)] // This is a temporary internal structure
let mut stack: Vec<( let mut stack: Vec<(ReverseDFSPass, Option<(NodeIndex, EdgeIndex)>, NodeIndex)> = entries
ReverseTopologicalPass,
Option<(NodeIndex, EdgeIndex)>,
NodeIndex,
)> = entries
.rev() .rev()
.map(|e| (ReverseTopologicalPass::ExpandAndVisit, None, e)) .map(|e| (ReverseDFSPass::ExpandAndVisit, None, e))
.collect(); .collect();
let mut expanded = FxHashSet::default(); let mut expanded = FxHashSet::default();
while let Some((pass, parent, current)) = stack.pop() { while let Some((pass, parent, current)) = stack.pop() {
...@@ -184,24 +180,20 @@ impl ModuleBatchesGraph { ...@@ -184,24 +180,20 @@ impl ModuleBatchesGraph {
) )
}); });
match pass { match pass {
ReverseTopologicalPass::Visit => { ReverseDFSPass::Visit => {
let current_node = graph.node_weight(current).unwrap(); let current_node = graph.node_weight(current).unwrap();
visit_postorder(parent_arg, current_node, state); visit_postorder(parent_arg, current_node, state);
} }
ReverseTopologicalPass::ExpandAndVisit => { ReverseDFSPass::ExpandAndVisit => {
let current_node = graph.node_weight(current).unwrap(); let current_node = graph.node_weight(current).unwrap();
let action = visit_preorder(parent_arg, current_node, state)?; let action = visit_preorder(parent_arg, current_node, state)?;
if action == GraphTraversalAction::Exclude { if action == GraphTraversalAction::Exclude {
continue; continue;
} }
stack.push((ReverseTopologicalPass::Visit, parent, current)); stack.push((ReverseDFSPass::Visit, parent, current));
if action == GraphTraversalAction::Continue && expanded.insert(current) { if action == GraphTraversalAction::Continue && expanded.insert(current) {
stack.extend(iter_neighbors_rev(graph, current).map(|(edge, child)| { stack.extend(iter_neighbors_rev(graph, current).map(|(edge, child)| {
( (ReverseDFSPass::ExpandAndVisit, Some((current, edge)), child)
ReverseTopologicalPass::ExpandAndVisit,
Some((current, edge)),
child,
)
})); }));
} }
} }
...@@ -293,7 +285,7 @@ impl PreBatches { ...@@ -293,7 +285,7 @@ impl PreBatches {
}; };
let mut visited = FxHashSet::default(); let mut visited = FxHashSet::default();
module_graph module_graph
.traverse_edges_from_entries_topological( .traverse_edges_from_entries_dfs(
std::iter::once(ResolvedVc::upcast(entry)), std::iter::once(ResolvedVc::upcast(entry)),
&mut state, &mut state,
|parent_info, node, state| { |parent_info, node, state| {
......
...@@ -93,7 +93,7 @@ pub async fn compute_style_groups( ...@@ -93,7 +93,7 @@ pub async fn compute_style_groups(
} }
let mut visited = FxHashSet::default(); let mut visited = FxHashSet::default();
let mut items_in_postorder = FxIndexSet::default(); let mut items_in_postorder = FxIndexSet::default();
batches_graph.traverse_edges_from_entries_topological( batches_graph.traverse_edges_from_entries_dfs(
entries.iter().copied(), entries.iter().copied(),
&mut (), &mut (),
|parent_info, module, _| { |parent_info, module, _| {
......
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