Commit dfcc363b authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

fix async cycle

parent 36f50c5a
use anyhow::Result; use anyhow::Result;
use rustc_hash::FxHashSet; use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use crate::{ use crate::{
...@@ -72,8 +73,30 @@ async fn compute_async_module_info_single( ...@@ -72,8 +73,30 @@ async fn compute_async_module_info_single(
// strongly-connected components, and then marking all the whole SCC as async if one of the // strongly-connected components, and then marking all the whole SCC as async if one of the
// modules in the SCC is async. // modules in the SCC is async.
let mut async_modules = self_async_modules;
let graph_ref = graph.read(); let graph_ref = graph.read();
let mut async_modules = self_async_modules;
let mut cycles = vec![];
let mut cycle_entries: FxHashMap<ResolvedVc<_>, SmallVec<[usize; 1]>> = Default::default();
graph_ref.traverse_cycles(
|ref_data| ref_data.chunking_type.is_inherit_async(),
|cycle| {
let cycle_idx = cycles.len();
cycles.push(cycle.to_vec());
for module in cycle {
// For later when we propagate async-ness to parents
cycle_entries.entry(*module).or_default().push(cycle_idx);
}
// For the self_async_modules
if cycle.iter().any(|node| async_modules.contains(node)) {
async_modules.extend(cycle.iter().copied());
}
Ok(())
},
)?;
graph_ref.traverse_edges_from_entries_dfs( graph_ref.traverse_edges_from_entries_dfs(
graph.entry_modules(), graph.entry_modules(),
&mut (), &mut (),
...@@ -85,17 +108,16 @@ async fn compute_async_module_info_single( ...@@ -85,17 +108,16 @@ async fn compute_async_module_info_single(
}; };
if ref_data.chunking_type.is_inherit_async() && async_modules.contains(&module) { if ref_data.chunking_type.is_inherit_async() && async_modules.contains(&module) {
async_modules.insert(parent_module); let newly_async = async_modules.insert(parent_module);
} if newly_async {
Ok(()) for cycle in cycle_entries
}, .get(&parent_module)
)?; .iter()
.flat_map(|v| v.iter())
graph_ref.traverse_cycles( {
|ref_data| ref_data.chunking_type.is_inherit_async(), async_modules.extend(cycles[*cycle].iter().copied());
|cycle| { }
if cycle.iter().any(|node| async_modules.contains(node)) { }
async_modules.extend(cycle.iter().map(|n| **n));
} }
Ok(()) Ok(())
}, },
......
...@@ -40,7 +40,7 @@ pub async fn compute_export_usage_info( ...@@ -40,7 +40,7 @@ pub async fn compute_export_usage_info(
// rare. For vercel-site on 8/22/2025 there were 106 cycles covering 800 modules // rare. For vercel-site on 8/22/2025 there were 106 cycles covering 800 modules
// (or 1.2% of all modules). So with this analysis we could potentially drop 80% of // (or 1.2% of all modules). So with this analysis we could potentially drop 80% of
// the cycle breaker modules. // the cycle breaker modules.
circuit_breakers.extend(cycle.iter().map(|n| **n)); circuit_breakers.extend(cycle.iter().copied());
Ok(()) Ok(())
}, },
)?; )?;
......
...@@ -414,7 +414,7 @@ impl SingleModuleGraph { ...@@ -414,7 +414,7 @@ impl SingleModuleGraph {
fn traverse_cycles<'l>( fn traverse_cycles<'l>(
&'l self, &'l self,
edge_filter: impl Fn(&'l RefData) -> bool, edge_filter: impl Fn(&'l RefData) -> bool,
mut visit_cycle: impl FnMut(&[&'l ResolvedVc<Box<dyn Module>>]) -> Result<()>, mut visit_cycle: impl FnMut(&[ResolvedVc<Box<dyn Module>>]) -> Result<()>,
) -> Result<()> { ) -> Result<()> {
// see https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm // see https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
// but iteratively instead of recursively // but iteratively instead of recursively
...@@ -491,7 +491,7 @@ impl SingleModuleGraph { ...@@ -491,7 +491,7 @@ impl SingleModuleGraph {
if let SingleModuleGraphNode::Module(module) = if let SingleModuleGraphNode::Module(module) =
self.graph.node_weight(poppped).unwrap() self.graph.node_weight(poppped).unwrap()
{ {
scc.push(module); scc.push(*module);
} }
if poppped == node { if poppped == node {
break; break;
...@@ -1182,7 +1182,7 @@ impl ModuleGraphRef { ...@@ -1182,7 +1182,7 @@ impl ModuleGraphRef {
pub fn traverse_cycles( pub fn traverse_cycles(
&self, &self,
edge_filter: impl Fn(&RefData) -> bool, edge_filter: impl Fn(&RefData) -> bool,
mut visit_cycle: impl FnMut(&[&ResolvedVc<Box<dyn Module>>]) -> Result<()>, mut visit_cycle: impl FnMut(&[ResolvedVc<Box<dyn Module>>]) -> Result<()>,
) -> Result<()> { ) -> Result<()> {
for graph in &self.graphs { for graph in &self.graphs {
graph.traverse_cycles(&edge_filter, &mut visit_cycle)?; graph.traverse_cycles(&edge_filter, &mut visit_cycle)?;
......
...@@ -392,9 +392,7 @@ pub async fn compute_module_batches( ...@@ -392,9 +392,7 @@ pub async fn compute_module_batches(
.iter() .iter()
.any(|node| pre_batches.boundary_modules.contains(node)) .any(|node| pre_batches.boundary_modules.contains(node))
{ {
pre_batches pre_batches.boundary_modules.extend(cycle.iter().copied());
.boundary_modules
.extend(cycle.iter().map(|node| **node));
} }
Ok(()) Ok(())
}, },
......
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