-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(turbopack-ecmascript): implement acyclic SCC graph for ESM impor…
…ts (vercel/turborepo#5506)
- Loading branch information
1 parent
7f54c70
commit 67a2dfb
Showing
10 changed files
with
261 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
use std::collections::HashMap; | ||
|
||
use anyhow::{Context, Result}; | ||
use petgraph::{algo::tarjan_scc, prelude::DiGraphMap}; | ||
use turbo_tasks::{TryFlatJoinIterExt, Value, Vc}; | ||
use turbopack_core::{ | ||
chunk::{availability_info::AvailabilityInfo, available_assets::chunkable_assets_set}, | ||
module::{Module, ModulesSet}, | ||
}; | ||
|
||
use crate::{ | ||
chunk::EcmascriptChunkPlaceable, | ||
references::esm::{base::ReferencedAsset, EsmAssetReference}, | ||
EcmascriptModuleAssets, | ||
}; | ||
|
||
/// A graph representing all ESM imports in a chunk group. | ||
#[turbo_tasks::value(serialization = "none", cell = "new", eq = "manual")] | ||
pub(crate) struct EsmScope { | ||
scc_map: HashMap<Vc<Box<dyn EcmascriptChunkPlaceable>>, Vc<EsmScopeScc>>, | ||
#[turbo_tasks(trace_ignore, debug_ignore)] | ||
scc_graph: DiGraphMap<Vc<EsmScopeScc>, ()>, | ||
} | ||
|
||
#[turbo_tasks::value(transparent)] | ||
pub(crate) struct EsmScopeScc(Vec<Vc<Box<dyn EcmascriptChunkPlaceable>>>); | ||
|
||
#[turbo_tasks::value(transparent)] | ||
pub(crate) struct OptionEsmScopeScc(Option<Vc<EsmScopeScc>>); | ||
|
||
#[turbo_tasks::value(transparent)] | ||
pub(crate) struct EsmScopeSccs(Vec<Vc<EsmScopeScc>>); | ||
|
||
#[turbo_tasks::value_impl] | ||
impl EsmScope { | ||
#[turbo_tasks::function] | ||
pub(crate) async fn new(availability_info: Value<AvailabilityInfo>) -> Result<Vc<Self>> { | ||
let assets = if let Some(root) = availability_info.current_availability_root() { | ||
chunkable_assets_set(root) | ||
} else { | ||
ModulesSet::empty() | ||
}; | ||
|
||
let esm_assets = get_ecmascript_module_assets(assets); | ||
let import_references = collect_import_references(esm_assets).await?; | ||
|
||
let mut graph = DiGraphMap::new(); | ||
|
||
for (parent, child) in &*import_references { | ||
graph.add_edge(*parent, *child, ()); | ||
} | ||
|
||
let sccs = tarjan_scc(&graph); | ||
|
||
let mut scc_map = HashMap::new(); | ||
for scc in sccs { | ||
let scc_vc = EsmScopeScc(scc.clone()).cell(); | ||
|
||
for placeable in scc { | ||
scc_map.insert(placeable, scc_vc); | ||
} | ||
} | ||
|
||
let mut scc_graph = DiGraphMap::new(); | ||
for (parent, child, _) in graph.all_edges() { | ||
let parent_scc_vc = *scc_map | ||
.get(&parent) | ||
.context("unexpected missing SCC in map")?; | ||
let child_scc_vc = *scc_map | ||
.get(&child) | ||
.context("unexpected missing SCC in map")?; | ||
|
||
if parent_scc_vc != child_scc_vc { | ||
scc_graph.add_edge(parent_scc_vc, child_scc_vc, ()); | ||
} | ||
} | ||
|
||
Ok(Self::cell(EsmScope { scc_map, scc_graph })) | ||
} | ||
|
||
#[turbo_tasks::function] | ||
pub(crate) async fn get_scc( | ||
self: Vc<Self>, | ||
placeable: Vc<Box<dyn EcmascriptChunkPlaceable>>, | ||
) -> Result<Vc<OptionEsmScopeScc>> { | ||
let this = self.await?; | ||
|
||
Ok(Vc::cell(this.scc_map.get(&placeable).copied())) | ||
} | ||
|
||
#[turbo_tasks::function] | ||
pub(crate) async fn get_scc_children( | ||
self: Vc<Self>, | ||
scc: Vc<EsmScopeScc>, | ||
) -> Result<Vc<EsmScopeSccs>> { | ||
let this = self.await?; | ||
|
||
let children = this.scc_graph.neighbors(scc).collect(); | ||
|
||
Ok(Vc::cell(children)) | ||
} | ||
} | ||
|
||
#[turbo_tasks::function] | ||
async fn get_ecmascript_module_assets( | ||
modules: Vc<ModulesSet>, | ||
) -> Result<Vc<EcmascriptModuleAssets>> { | ||
let esm_assets = modules | ||
.await? | ||
.iter() | ||
.copied() | ||
.map(|r| async move { anyhow::Ok(Vc::try_resolve_downcast_type(r).await?) }) | ||
.try_flat_join() | ||
.await?; | ||
|
||
Ok(Vc::cell(esm_assets)) | ||
} | ||
|
||
// for clippy | ||
type PlaceableVc = Vc<Box<dyn EcmascriptChunkPlaceable>>; | ||
|
||
/// A directional reference between 2 [EcmascriptChunkPlaceable]s. | ||
#[turbo_tasks::value(transparent)] | ||
struct ImportReferences(Vec<(PlaceableVc, PlaceableVc)>); | ||
|
||
#[turbo_tasks::function] | ||
async fn collect_import_references( | ||
esm_assets: Vc<EcmascriptModuleAssets>, | ||
) -> Result<Vc<ImportReferences>> { | ||
let import_references = esm_assets | ||
.await? | ||
.iter() | ||
.copied() | ||
.map(|a| async move { | ||
let placeable = Vc::upcast::<Box<dyn EcmascriptChunkPlaceable>>(a) | ||
.resolve() | ||
.await?; | ||
|
||
a.references() | ||
.await? | ||
.iter() | ||
.copied() | ||
.map(|r| async move { | ||
let Some(r) = Vc::try_resolve_downcast_type::<EsmAssetReference>(r).await? | ||
else { | ||
return Ok(None); | ||
}; | ||
|
||
let ReferencedAsset::Some(child_placeable) = &*r.get_referenced_asset().await? | ||
else { | ||
return Ok(None); | ||
}; | ||
|
||
let child_placeable = child_placeable.resolve().await?; | ||
|
||
anyhow::Ok(Some((placeable, child_placeable))) | ||
}) | ||
.try_flat_join() | ||
.await | ||
}) | ||
.try_flat_join() | ||
.await?; | ||
|
||
Ok(Vc::cell(import_references)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters