Commit dfcc363b authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

fix async cycle

parent 36f50c5a
use anyhow::Result;
use rustc_hash::FxHashSet;
use rustc_hash::{FxHashMap, FxHashSet};
use smallvec::SmallVec;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use crate::{
......@@ -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
// modules in the SCC is async.
let mut async_modules = self_async_modules;
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.entry_modules(),
&mut (),
......@@ -85,17 +108,16 @@ async fn compute_async_module_info_single(
};
if ref_data.chunking_type.is_inherit_async() && async_modules.contains(&module) {
async_modules.insert(parent_module);
}
Ok(())
},
)?;
graph_ref.traverse_cycles(
|ref_data| ref_data.chunking_type.is_inherit_async(),
|cycle| {
if cycle.iter().any(|node| async_modules.contains(node)) {
async_modules.extend(cycle.iter().map(|n| **n));
let newly_async = async_modules.insert(parent_module);
if newly_async {
for cycle in cycle_entries
.get(&parent_module)
.iter()
.flat_map(|v| v.iter())
{
async_modules.extend(cycles[*cycle].iter().copied());
}
}
}
Ok(())
},
......
......@@ -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
// (or 1.2% of all modules). So with this analysis we could potentially drop 80% of
// the cycle breaker modules.
circuit_breakers.extend(cycle.iter().map(|n| **n));
circuit_breakers.extend(cycle.iter().copied());
Ok(())
},
)?;
......
......@@ -414,7 +414,7 @@ impl SingleModuleGraph {
fn traverse_cycles<'l>(
&'l self,
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<()> {
// see https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
// but iteratively instead of recursively
......@@ -491,7 +491,7 @@ impl SingleModuleGraph {
if let SingleModuleGraphNode::Module(module) =
self.graph.node_weight(poppped).unwrap()
{
scc.push(module);
scc.push(*module);
}
if poppped == node {
break;
......@@ -1182,7 +1182,7 @@ impl ModuleGraphRef {
pub fn traverse_cycles(
&self,
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<()> {
for graph in &self.graphs {
graph.traverse_cycles(&edge_filter, &mut visit_cycle)?;
......
......@@ -392,9 +392,7 @@ pub async fn compute_module_batches(
.iter()
.any(|node| pre_batches.boundary_modules.contains(node))
{
pre_batches
.boundary_modules
.extend(cycle.iter().map(|node| **node));
pre_batches.boundary_modules.extend(cycle.iter().copied());
}
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