Unverified Commit 957b5fb5 authored by lichu acuña's avatar lichu acuña Committed by GitHub

Exhaustive modules graph traversal (#68865)

### What?
Improves the traversal of the module graph by doing a more exhaustive search of the children modules, thus increasing the "hit rate" for optimized module IDs (which should be 100%).

### Why?
Some structs that implement the `Module` trait return an empty list in the `references` method, but still "own" some child modules. This is the case of `EcmascriptClientReferenceModule`, for example, which returns empty in `references` but owns a `client_module` and an `ssr_module`. These modules are ignored when traversing the module graph with the `get_referenced_modules` function we used before, so neither they nor their children are ever assigned an optimized ID.

### How?
To solve this, a `children_modules` method is added to the `Module` trait, which should exhaustively return all direct children of the module. The function defaults to `primary_referenced_modules`, which simply calls `references`, but structs like `EcmascriptClientReferenceModule` can reimplement it and return "hidden" children modules like the aforementioned `client_module` and `ssr_module`.
parent 9ecd1a2b
...@@ -4,7 +4,7 @@ use turbo_tasks::{RcStr, Vc}; ...@@ -4,7 +4,7 @@ use turbo_tasks::{RcStr, Vc};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
ident::AssetIdent, ident::AssetIdent,
module::Module, module::{Module, Modules},
}; };
use turbopack_ecmascript::chunk::EcmascriptChunkPlaceable; use turbopack_ecmascript::chunk::EcmascriptChunkPlaceable;
...@@ -55,6 +55,13 @@ impl Module for EcmascriptClientReferenceModule { ...@@ -55,6 +55,13 @@ impl Module for EcmascriptClientReferenceModule {
self.server_ident self.server_ident
.with_modifier(ecmascript_client_reference_modifier()) .with_modifier(ecmascript_client_reference_modifier())
} }
#[turbo_tasks::function]
async fn additional_layers_modules(&self) -> Result<Vc<Modules>> {
let client_module = Vc::upcast(self.client_module);
let ssr_module = Vc::upcast(self.ssr_module);
Ok(Vc::cell(vec![client_module, ssr_module]))
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
...@@ -8,10 +8,10 @@ use turbo_tasks::{ ...@@ -8,10 +8,10 @@ use turbo_tasks::{
use turbo_tasks_hash::hash_xxh3_hash64; use turbo_tasks_hash::hash_xxh3_hash64;
use crate::{ use crate::{
changed::get_referenced_modules,
chunk::ModuleId, chunk::ModuleId,
ident::AssetIdent, ident::AssetIdent,
module::{Module, Modules}, module::{Module, Modules},
reference::primary_referenced_modules,
}; };
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -22,6 +22,14 @@ pub struct PreprocessedChildrenIdents { ...@@ -22,6 +22,14 @@ pub struct PreprocessedChildrenIdents {
modules_idents: HashMap<AssetIdent, u64>, modules_idents: HashMap<AssetIdent, u64>,
} }
pub async fn get_children_modules(
parent: Vc<Box<dyn Module>>,
) -> Result<impl Iterator<Item = Vc<Box<dyn Module>>> + Send> {
let mut primary_modules = primary_referenced_modules(parent).await?.clone_value();
primary_modules.extend(parent.additional_layers_modules().await?.clone_value());
Ok(primary_modules.into_iter())
}
// NOTE(LichuAcu) Called on endpoint.root_modules(). It would probably be better if this was called // NOTE(LichuAcu) Called on endpoint.root_modules(). It would probably be better if this was called
// directly on `Endpoint`, but such struct is not available in turbopack-core. The whole function // directly on `Endpoint`, but such struct is not available in turbopack-core. The whole function
// could be moved to `next-api`, but it would require adding turbo-tasks-hash to `next-api`, // could be moved to `next-api`, but it would require adding turbo-tasks-hash to `next-api`,
...@@ -32,7 +40,7 @@ pub async fn children_modules_idents( ...@@ -32,7 +40,7 @@ pub async fn children_modules_idents(
) -> Result<Vc<PreprocessedChildrenIdents>> { ) -> Result<Vc<PreprocessedChildrenIdents>> {
let children_modules_iter = AdjacencyMap::new() let children_modules_iter = AdjacencyMap::new()
.skip_duplicates() .skip_duplicates()
.visit(root_modules.await?.iter().copied(), get_referenced_modules) .visit(root_modules.await?.iter().copied(), get_children_modules)
.await .await
.completed()? .completed()?
.into_inner() .into_inner()
......
...@@ -16,6 +16,10 @@ pub trait Module: Asset { ...@@ -16,6 +16,10 @@ pub trait Module: Asset {
fn references(self: Vc<Self>) -> Vc<ModuleReferences> { fn references(self: Vc<Self>) -> Vc<ModuleReferences> {
ModuleReferences::empty() ModuleReferences::empty()
} }
fn additional_layers_modules(self: Vc<Self>) -> Vc<Modules> {
Vc::cell(vec![])
}
} }
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
......
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