Unverified Commit 28c9eefa authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: iter_reachable_modules (#93315)

Add `iter_reachable_modules` and `iter_reachable_nodes`. They actually respect unused references
parent 6d2cb42f
...@@ -31,9 +31,9 @@ pub struct ClientReferenceData(FxHashMap<ResolvedVc<Box<dyn Module>>, ClientMani ...@@ -31,9 +31,9 @@ pub struct ClientReferenceData(FxHashMap<ResolvedVc<Box<dyn Module>>, ClientMani
pub async fn map_client_references( pub async fn map_client_references(
graph: ResolvedVc<ModuleGraphLayer>, graph: ResolvedVc<ModuleGraphLayer>,
) -> Result<Vc<ClientReferenceData>> { ) -> Result<Vc<ClientReferenceData>> {
let graph = graph.await?;
let manifest = graph let manifest = graph
.iter_nodes() .await?
.iter_reachable_modules()?
.map(|module| async move { .map(|module| async move {
if let Some(client_reference_module) = if let Some(client_reference_module) =
ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(module) ResolvedVc::try_downcast_type::<EcmascriptClientReferenceModule>(module)
......
...@@ -123,7 +123,7 @@ pub async fn map_next_dynamic( ...@@ -123,7 +123,7 @@ pub async fn map_next_dynamic(
let actions = let actions =
graph graph
.await? .await?
.iter_nodes() .iter_reachable_modules()?
.map(|module| async move { .map(|module| async move {
if let Some(dynamic_entry_module) = if let Some(dynamic_entry_module) =
ResolvedVc::try_downcast_type::<NextDynamicEntryModule>(module) ResolvedVc::try_downcast_type::<NextDynamicEntryModule>(module)
......
...@@ -527,7 +527,7 @@ pub async fn map_server_actions( ...@@ -527,7 +527,7 @@ pub async fn map_server_actions(
) -> Result<Vc<AllModuleActions>> { ) -> Result<Vc<AllModuleActions>> {
let actions = graph let actions = graph
.await? .await?
.iter_nodes() .iter_reachable_modules()?
.map(async |module| { .map(async |module| {
// TODO: compare module contexts instead? // TODO: compare module contexts instead?
let layer = match module.ident().await?.layer.as_ref() { let layer = match module.ident().await?.layer.as_ref() {
......
...@@ -4,7 +4,7 @@ use turbo_tasks::{OperationVc, ResolvedVc, TryFlatJoinIterExt, Vc}; ...@@ -4,7 +4,7 @@ use turbo_tasks::{OperationVc, ResolvedVc, TryFlatJoinIterExt, Vc};
use crate::{ use crate::{
module::Module, module::Module,
module_graph::{GraphTraversalAction, ModuleGraph, ModuleGraphLayer}, module_graph::{GraphTraversalAction, ModuleGraph, ModuleGraphLayer, SingleModuleGraphNode},
}; };
/// This lists all the modules that are async (self or transitively because they reference another /// This lists all the modules that are async (self or transitively because they reference another
...@@ -44,13 +44,11 @@ async fn compute_async_module_info_single( ...@@ -44,13 +44,11 @@ async fn compute_async_module_info_single(
}; };
let graph = graph.read_strongly_consistent().await?; let graph = graph.read_strongly_consistent().await?;
let self_async_modules = graph let self_async_modules = graph
.enumerate_nodes() .iter_reachable_nodes()?
.map(async |(_, node)| { .map(async |node| {
Ok(match node { Ok(match node {
super::SingleModuleGraphNode::Module(node) => { SingleModuleGraphNode::Module(node) => node.is_self_async().await?.then_some(*node),
node.is_self_async().await?.then_some(*node) SingleModuleGraphNode::VisitedModule { idx: _, module } => {
}
super::SingleModuleGraphNode::VisitedModule { idx: _, module } => {
// If a module is async in the parent then we need to mark reverse dependencies // If a module is async in the parent then we need to mark reverse dependencies
// async in this graph as well. // async in this graph as well.
parent_async_modules parent_async_modules
......
...@@ -140,7 +140,7 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc< ...@@ -140,7 +140,7 @@ pub async fn compute_merged_modules(module_graph: Vc<ModuleGraph>) -> Result<Vc<
let inner_span = tracing::info_span!("collect mergeable modules"); let inner_span = tracing::info_span!("collect mergeable modules");
let mergeable = module_graph let mergeable = module_graph
.iter_nodes() .iter_reachable_modules()?
.map(async |module| { .map(async |module| {
if let Some(mergeable) = if let Some(mergeable) =
ResolvedVc::try_downcast::<Box<dyn MergeableModule>>(module) ResolvedVc::try_downcast::<Box<dyn MergeableModule>>(module)
......
...@@ -4,7 +4,7 @@ use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc}; ...@@ -4,7 +4,7 @@ use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc};
use crate::{ use crate::{
module::{Module, ModuleSideEffects}, module::{Module, ModuleSideEffects},
module_graph::{GraphTraversalAction, ModuleGraph, ModuleGraphLayer}, module_graph::{GraphTraversalAction, ModuleGraph, ModuleGraphLayer, SingleModuleGraphNode},
}; };
/// This lists all the modules that are side effect free /// This lists all the modules that are side effect free
...@@ -43,16 +43,16 @@ async fn compute_side_effect_free_module_info_single( ...@@ -43,16 +43,16 @@ async fn compute_side_effect_free_module_info_single(
let parent_side_effect_free_modules = parent_side_effect_free_modules.await?; let parent_side_effect_free_modules = parent_side_effect_free_modules.await?;
let graph = graph.await?; let graph = graph.await?;
let module_side_effects = graph let module_side_effects = graph
.enumerate_nodes() .iter_reachable_nodes()?
.map(async |(_, node)| { .map(async |node| {
Ok(match node { Ok(match node {
super::SingleModuleGraphNode::Module(module) => { SingleModuleGraphNode::Module(module) => {
// This turbo task always has a cache hit since it is called when building the // This turbo task always has a cache hit since it is called when building the
// module graph. we could consider moving this information // module graph. we could consider moving this information
// into to the module graph, but then changes would invalidate the whole graph. // into to the module graph, but then changes would invalidate the whole graph.
(*module, *module.side_effects().await?) (*module, *module.side_effects().await?)
} }
super::SingleModuleGraphNode::VisitedModule { idx: _, module } => ( SingleModuleGraphNode::VisitedModule { idx: _, module } => (
*module, *module,
if parent_side_effect_free_modules.contains(module) { if parent_side_effect_free_modules.contains(module) {
ModuleSideEffects::SideEffectFree ModuleSideEffects::SideEffectFree
......
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