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 = [
"turbo-prehash",
"turbo-rcstr",
"turbo-tasks",
"turbo-tasks-backend",
"turbo-tasks-build",
"turbo-tasks-env",
"turbo-tasks-fs",
"turbo-tasks-hash",
"turbo-tasks-testing",
"urlencoding",
]
......
......@@ -13,7 +13,7 @@ use turbopack::css::chunk::CssChunkPlaceable;
use turbopack_core::{module::Module, module_graph::SingleModuleGraph};
#[derive(
Clone, Serialize, Deserialize, Eq, PartialEq, TraceRawVcs, ValueDebugFormat, NonLocalValue,
Copy, Clone, Serialize, Deserialize, Eq, PartialEq, TraceRawVcs, ValueDebugFormat, NonLocalValue,
)]
pub enum ClientReferenceMapType {
EcmascriptClientReference {
......@@ -31,7 +31,7 @@ pub struct ClientReferencesSet(FxHashMap<ResolvedVc<Box<dyn Module>>, ClientRefe
pub async fn map_client_references(
graph: Vc<SingleModuleGraph>,
) -> Result<Vc<ClientReferencesSet>> {
let actions = graph
let client_references = graph
.await?
.iter_nodes()
.map(|node| async move {
......@@ -52,9 +52,9 @@ pub async fn map_client_references(
{
Ok(Some((
module,
ClientReferenceMapType::CssClientReference(ResolvedVc::upcast(
ClientReferenceMapType::CssClientReference(
client_reference_module.await?.client_module,
)),
),
)))
} else if let Some(server_component) =
ResolvedVc::try_downcast_type::<NextServerComponentModule>(module)
......@@ -69,5 +69,5 @@ pub async fn map_client_references(
})
.try_flat_join()
.await?;
Ok(Vc::cell(actions.into_iter().collect()))
Ok(Vc::cell(client_references.into_iter().collect()))
}
......@@ -291,7 +291,7 @@ impl ClientReferencesGraph {
let mut client_references_by_server_component =
FxIndexMap::from_iter([(None, Vec::new())]);
graph.traverse_edges_from_entries_topological(
graph.traverse_edges_from_entries_dfs(
entries,
// state_map is `module -> Option< the current so parent server component >`
&mut FxHashMap::default(),
......
......@@ -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]
pub async fn whole_app_module_graphs(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraphs>> {
async move {
......
......@@ -50,6 +50,8 @@ turbo-tasks-build = { workspace = true }
[dev-dependencies]
rstest = { workspace = true }
tokio = { workspace = true }
turbo-tasks-testing = { workspace = true }
turbo-tasks-backend = { workspace = true }
[features]
default = []
......
......@@ -220,7 +220,7 @@ pub async fn chunk_group_content(
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,
&mut state,
|parent_info, &node, state| {
......
......@@ -73,7 +73,7 @@ async fn compute_async_module_info_single(
// modules in the SCC is async.
let mut async_modules = self_async_modules;
graph.traverse_edges_from_entries_topological(
graph.traverse_edges_from_entries_dfs(
graph.entry_modules(),
&mut (),
|_, _, _| Ok(GraphTraversalAction::Continue),
......
......@@ -251,7 +251,7 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
let mut visited = FxHashSet::default();
module_graph
.traverse_edges_from_entries_topological(
.traverse_edges_from_entries_dfs(
chunk_group.entries(),
&mut (),
|parent_info, node, _| {
......@@ -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());
module_graph
.traverse_edges_from_entries_topological(
.traverse_edges_from_entries_dfs(
entries,
&mut (),
|_, _, _| Ok(GraphTraversalAction::Continue),
......
......@@ -123,7 +123,7 @@ impl ModuleBatchesGraph {
// Clippy complains but there's a type error without the bound
#[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
///
/// Use this to collect batches/modules in evaluation order.
......@@ -131,7 +131,7 @@ impl ModuleBatchesGraph {
/// Target nodes can be revisited (once per incoming edge).
/// 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
/// * `visit_preorder` - Called before visiting the children of a node.
/// - Receives: (originating &ModuleBatchesGraphNode, edge &ChunkingType), target
......@@ -140,7 +140,7 @@ impl ModuleBatchesGraph {
/// * `visit_postorder` - Called after visiting the children of a node. Return
/// - Receives: (originating &ModuleBatchesGraphNode, edge &ChunkingType), target
/// &ModuleBatchesGraphNode, state &S
pub fn traverse_edges_from_entries_topological<'a, S>(
pub fn traverse_edges_from_entries_dfs<'a, S>(
&'a self,
entries: impl IntoIterator<
Item = NodeIndex,
......@@ -160,20 +160,16 @@ impl ModuleBatchesGraph {
) -> Result<()> {
let graph = &self.graph;
enum ReverseTopologicalPass {
enum ReverseDFSPass {
Visit,
ExpandAndVisit,
}
let entries = entries.into_iter();
#[allow(clippy::type_complexity)] // This is a temporary internal structure
let mut stack: Vec<(
ReverseTopologicalPass,
Option<(NodeIndex, EdgeIndex)>,
NodeIndex,
)> = entries
let mut stack: Vec<(ReverseDFSPass, Option<(NodeIndex, EdgeIndex)>, NodeIndex)> = entries
.rev()
.map(|e| (ReverseTopologicalPass::ExpandAndVisit, None, e))
.map(|e| (ReverseDFSPass::ExpandAndVisit, None, e))
.collect();
let mut expanded = FxHashSet::default();
while let Some((pass, parent, current)) = stack.pop() {
......@@ -184,24 +180,20 @@ impl ModuleBatchesGraph {
)
});
match pass {
ReverseTopologicalPass::Visit => {
ReverseDFSPass::Visit => {
let current_node = graph.node_weight(current).unwrap();
visit_postorder(parent_arg, current_node, state);
}
ReverseTopologicalPass::ExpandAndVisit => {
ReverseDFSPass::ExpandAndVisit => {
let current_node = graph.node_weight(current).unwrap();
let action = visit_preorder(parent_arg, current_node, state)?;
if action == GraphTraversalAction::Exclude {
continue;
}
stack.push((ReverseTopologicalPass::Visit, parent, current));
stack.push((ReverseDFSPass::Visit, parent, current));
if action == GraphTraversalAction::Continue && expanded.insert(current) {
stack.extend(iter_neighbors_rev(graph, current).map(|(edge, child)| {
(
ReverseTopologicalPass::ExpandAndVisit,
Some((current, edge)),
child,
)
(ReverseDFSPass::ExpandAndVisit, Some((current, edge)), child)
}));
}
}
......@@ -293,7 +285,7 @@ impl PreBatches {
};
let mut visited = FxHashSet::default();
module_graph
.traverse_edges_from_entries_topological(
.traverse_edges_from_entries_dfs(
std::iter::once(ResolvedVc::upcast(entry)),
&mut state,
|parent_info, node, state| {
......
......@@ -93,7 +93,7 @@ pub async fn compute_style_groups(
}
let mut visited = FxHashSet::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(),
&mut (),
|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