Unverified Commit e2422f4c authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Add FileSystemPath.has_extension and optimize module graph operations (#81205)

## Optimize file extension checks and module graph traversal

### What?
This PR introduces several optimizations:

1. Added a new `has_extension()` method to `FileSystemPath` for more efficient extension checking
2. Improved module graph traversal with a dedicated `has_entry_module()` method
3. Reordered code in `AppProject` to avoid unnecessary clones
4. Simplified CSS resource handling in client reference manifest generation

### Why?
These changes improve performance by:
- Replacing string comparisons with more efficient suffix matching for file extensions
- Providing a direct way to check if a module is an entry point without iterating all entries
- Avoiding unnecessary clones operations when possible
parent 8ecf21e5
...@@ -861,14 +861,13 @@ impl AppProject { ...@@ -861,14 +861,13 @@ impl AppProject {
client_shared_entries: Vc<EvaluatableAssets>, client_shared_entries: Vc<EvaluatableAssets>,
has_layout_segments: bool, has_layout_segments: bool,
) -> Result<Vc<ModuleGraphs>> { ) -> Result<Vc<ModuleGraphs>> {
if *self.project.per_page_module_graph().await? {
let should_trace = self.project.next_mode().await?.is_production();
let client_shared_entries = client_shared_entries let client_shared_entries = client_shared_entries
.await? .await?
.into_iter() .into_iter()
.map(|m| ResolvedVc::upcast(*m)) .map(|m| ResolvedVc::upcast(*m))
.collect(); .collect();
let should_trace = self.project.next_mode().await?.is_production();
if *self.project.per_page_module_graph().await? {
// Implements layout segment optimization to compute a graph "chain" for each layout // Implements layout segment optimization to compute a graph "chain" for each layout
// segment // segment
async move { async move {
...@@ -1255,7 +1254,7 @@ impl AppEndpoint { ...@@ -1255,7 +1254,7 @@ impl AppEndpoint {
client_assets.insert(chunk); client_assets.insert(chunk);
let chunk_path = chunk.path().await?; let chunk_path = chunk.path().await?;
if chunk_path.extension_ref() == Some("js") { if chunk_path.has_extension(".js") {
client_shared_chunks.push(chunk); client_shared_chunks.push(chunk);
} }
} }
......
...@@ -82,7 +82,7 @@ impl NextDynamicGraph { ...@@ -82,7 +82,7 @@ impl NextDynamicGraph {
} }
let entries = if !self.is_single_page { let entries = if !self.is_single_page {
if !graph.entry_modules().any(|m| m == entry) { if !graph.has_entry_module(entry) {
// the graph doesn't contain the entry, e.g. for the additional module graph // the graph doesn't contain the entry, e.g. for the additional module graph
return Ok(Vc::cell(vec![])); return Ok(Vc::cell(vec![]));
} }
...@@ -185,7 +185,7 @@ impl ServerActionsGraph { ...@@ -185,7 +185,7 @@ impl ServerActionsGraph {
// The graph contains the whole app, traverse and collect all reachable imports. // The graph contains the whole app, traverse and collect all reachable imports.
let graph = &*self.graph.await?; let graph = &*self.graph.await?;
if !graph.entry_modules().any(|m| m == entry) { if !graph.has_entry_module(entry) {
// the graph doesn't contain the entry, e.g. for the additional module graph // the graph doesn't contain the entry, e.g. for the additional module graph
return Ok(Vc::cell(Default::default())); return Ok(Vc::cell(Default::default()));
} }
...@@ -266,7 +266,7 @@ impl ClientReferencesGraph { ...@@ -266,7 +266,7 @@ impl ClientReferencesGraph {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_client_references_for_endpoint( async fn get_client_references_for_endpoint(
&self, &self,
entry: ResolvedVc<Box<dyn Module>>, entry: ResolvedVc<Box<dyn Module>>,
) -> Result<Vc<ClientReferenceGraphResult>> { ) -> Result<Vc<ClientReferenceGraphResult>> {
...@@ -276,7 +276,7 @@ impl ClientReferencesGraph { ...@@ -276,7 +276,7 @@ impl ClientReferencesGraph {
let graph = &*self.graph.await?; let graph = &*self.graph.await?;
let entries = if !self.is_single_page { let entries = if !self.is_single_page {
if !graph.entry_modules().any(|m| m == entry) { if !graph.has_entry_module(entry) {
// the graph doesn't contain the entry, e.g. for the additional module graph // the graph doesn't contain the entry, e.g. for the additional module graph
return Ok(ClientReferenceGraphResult::default().cell()); return Ok(ClientReferenceGraphResult::default().cell());
} }
...@@ -467,9 +467,7 @@ async fn validate_pages_css_imports( ...@@ -467,9 +467,7 @@ async fn validate_pages_css_imports(
let module_name_map = module_name_map.await?; let module_name_map = module_name_map.await?;
let entries = if !is_single_page { let entries = if !is_single_page {
// TODO: Optimize this code by checking if the node is an entry using `get_module` and then if !graph.has_entry_module(entry) {
// checking if the node is an entry in the graph by looking for the reverse edges.
if !graph.entry_modules().any(|m| m == entry) {
// the graph doesn't contain the entry, e.g. for the additional module graph // the graph doesn't contain the entry, e.g. for the additional module graph
return Ok(()); return Ok(());
} }
......
...@@ -218,7 +218,7 @@ impl Asset for NftJsonAsset { ...@@ -218,7 +218,7 @@ impl Asset for NftJsonAsset {
} }
let referenced_chunk_path = referenced_chunk.path().await?; let referenced_chunk_path = referenced_chunk.path().await?;
if referenced_chunk_path.extension_ref() == Some("map") { if referenced_chunk_path.has_extension(".map") {
continue; continue;
} }
......
...@@ -268,11 +268,10 @@ impl AppPageLoaderTreeBuilder { ...@@ -268,11 +268,10 @@ impl AppPageLoaderTreeBuilder {
writeln!(self.loader_tree_code, "{s} width: {identifier}.width,")?; writeln!(self.loader_tree_code, "{s} width: {identifier}.width,")?;
writeln!(self.loader_tree_code, "{s} height: {identifier}.height,")?; writeln!(self.loader_tree_code, "{s} height: {identifier}.height,")?;
} else { } else {
let ext = path.extension();
// For SVGs, skip sizes and use "any" to let it scale automatically based on viewport, // For SVGs, skip sizes and use "any" to let it scale automatically based on viewport,
// For the images doesn't provide the size properly, use "any" as well. // For the images doesn't provide the size properly, use "any" as well.
// If the size is presented, use the actual size for the image. // If the size is presented, use the actual size for the image.
let sizes = if ext == "svg" { let sizes = if path.has_extension(".svg") {
"any".to_string() "any".to_string()
} else { } else {
format!("${{{identifier}.width}}x${{{identifier}.height}}") format!("${{{identifier}.width}}x${{{identifier}.height}}")
......
...@@ -32,7 +32,7 @@ pub struct ClientReferencesChunks { ...@@ -32,7 +32,7 @@ pub struct ClientReferencesChunks {
/// Computes all client references chunks. /// Computes all client references chunks.
/// ///
/// This returns a map from client reference type to the chunks that reference /// This returns a map from client reference type to the chunks that the reference
/// type needs to load. /// type needs to load.
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn get_app_client_references_chunks( pub async fn get_app_client_references_chunks(
......
...@@ -218,26 +218,22 @@ impl Visit<FindServerEntriesNode> for FindServerEntries { ...@@ -218,26 +218,22 @@ impl Visit<FindServerEntriesNode> for FindServerEntries {
} }
fn edges(&mut self, node: &FindServerEntriesNode) -> Self::EdgesFuture { fn edges(&mut self, node: &FindServerEntriesNode) -> Self::EdgesFuture {
let node = node.clone();
let include_traced = self.include_traced; let include_traced = self.include_traced;
async move {
let parent_module = match node { let parent_module = match node {
// This should never occur since we always skip visiting these // This should never occur since we always skip visiting these
// nodes' edges. // nodes' edges.
FindServerEntriesNode::ClientReference => { FindServerEntriesNode::ClientReference => {
unreachable!("ClientReference node should not be visited") unreachable!("ClientReference node should not be visited")
} }
FindServerEntriesNode::Internal(module, _) => module, FindServerEntriesNode::Internal(module, _) => **module,
FindServerEntriesNode::ServerUtilEntry(module, _) => ResolvedVc::upcast(module), FindServerEntriesNode::ServerUtilEntry(module, _) => Vc::upcast(**module),
FindServerEntriesNode::ServerComponentEntry(module, _) => { FindServerEntriesNode::ServerComponentEntry(module, _) => Vc::upcast(**module),
ResolvedVc::upcast(module)
}
}; };
async move {
// Pass include_traced to reuse the same cached `primary_chunkable_referenced_modules` // Pass include_traced to reuse the same cached `primary_chunkable_referenced_modules`
// task result, but the traced references will be filtered out again afterwards. // task result, but the traced references will be filtered out again afterwards.
let referenced_modules = let referenced_modules =
primary_chunkable_referenced_modules(*parent_module, include_traced).await?; primary_chunkable_referenced_modules(parent_module, include_traced).await?;
let referenced_modules = referenced_modules let referenced_modules = referenced_modules
.iter() .iter()
......
...@@ -331,60 +331,52 @@ impl ClientReferenceManifest { ...@@ -331,60 +331,52 @@ impl ClientReferenceManifest {
.value_to_string() .value_to_string()
.owned() .owned()
.await?; .await?;
let mut entry_css_files_with_chunk = Vec::new();
let entry_js_files = entry_manifest let entry_js_files = entry_manifest
.entry_js_files .entry_js_files
.entry(server_component_name.clone()) .entry(server_component_name.clone())
.or_default(); .or_default();
let entry_css_files = entry_manifest
.entry_css_files
.entry(server_component_name)
.or_default();
let client_chunks = &client_chunks.await?; let client_chunks = &client_chunks.await?;
let client_chunks_with_path = let client_chunks_with_path =
cached_chunk_paths(&mut client_chunk_path_cache, client_chunks.iter().copied()) cached_chunk_paths(&mut client_chunk_path_cache, client_chunks.iter().copied())
.await?; .await?;
// Inlining breaks HMR so it is always disabled in dev.
let inlined_css = next_config.await?.experimental.inline_css.unwrap_or(false)
&& mode.is_production();
for (chunk, chunk_path) in client_chunks_with_path { for (chunk, chunk_path) in client_chunks_with_path {
if let Some(path) = client_relative_path.get_path_to(&chunk_path) { if let Some(path) = client_relative_path.get_path_to(&chunk_path) {
// The entry CSS files and entry JS files don't have prefix and suffix // The entry CSS files and entry JS files don't have prefix and suffix
// applied because it is added by Nex.js during rendering. // applied because it is added by Next.js during rendering.
let path = path.into(); let path = path.into();
if chunk_path.extension_ref() == Some("css") { if chunk_path.has_extension(".css") {
entry_css_files_with_chunk.push((path, chunk)); let content = if inlined_css {
} else { Some(
entry_js_files.insert(path);
}
}
}
let inlined = next_config.await?.experimental.inline_css.unwrap_or(false)
&& mode.is_production();
let entry_css_files_vec = entry_css_files_with_chunk
.into_iter()
.map(async |(path, chunk)| {
let content = if inlined {
if let Some(content_file) = if let Some(content_file) =
chunk.content().file_content().await?.as_content() chunk.content().file_content().await?.as_content()
{ {
Some(content_file.content().to_str()?.into()) content_file.content().to_str()?.into()
} else { } else {
Some("".into()) RcStr::default()
} },
)
} else { } else {
None None
}; };
Ok(CssResource { entry_css_files.insert(CssResource {
path, path,
inlined, inlined: inlined_css,
content, content,
}) });
}) } else {
.try_join() entry_js_files.insert(path);
.await?; }
}
let entry_css_files = entry_manifest }
.entry_css_files
.entry(server_component_name)
.or_default();
entry_css_files.extend(entry_css_files_vec);
} }
let client_reference_manifest_json = serde_json::to_string(&entry_manifest).unwrap(); let client_reference_manifest_json = serde_json::to_string(&entry_manifest).unwrap();
......
...@@ -169,14 +169,10 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin { ...@@ -169,14 +169,10 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
) -> Result<FileType> { ) -> Result<FileType> {
// node.js only supports these file extensions // node.js only supports these file extensions
// mjs is an esm module and we can't bundle that yet // mjs is an esm module and we can't bundle that yet
let ext = raw_fs_path.extension_ref(); Ok(match raw_fs_path.extension_ref() {
if matches!(ext, Some("cjs" | "node" | "json")) { Some("cjs" | "node" | "json") => FileType::CommonJs,
return Ok(FileType::CommonJs); Some("mjs") => FileType::EcmaScriptModule,
} Some("js") => {
if matches!(ext, Some("mjs")) {
return Ok(FileType::EcmaScriptModule);
}
if matches!(ext, Some("js")) {
// for .js extension in cjs context, we need to check the actual module type via // for .js extension in cjs context, we need to check the actual module type via
// package.json // package.json
let FindContextFileResult::Found(package_json, _) = let FindContextFileResult::Found(package_json, _) =
...@@ -185,19 +181,20 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin { ...@@ -185,19 +181,20 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
// can't find package.json // can't find package.json
return Ok(FileType::CommonJs); return Ok(FileType::CommonJs);
}; };
let FileJsonContent::Content(package) = &*package_json.read_json().await? else { let FileJsonContent::Content(package) = &*package_json.read_json().await?
else {
// can't parse package.json // can't parse package.json
return Ok(FileType::InvalidPackageJson); return Ok(FileType::InvalidPackageJson);
}; };
if let Some("module") = package["type"].as_str() { if let Some("module") = package["type"].as_str() {
return Ok(FileType::EcmaScriptModule); FileType::EcmaScriptModule
} else {
FileType::CommonJs
} }
return Ok(FileType::CommonJs);
} }
_ => FileType::UnsupportedExtension,
Ok(FileType::UnsupportedExtension) })
} }
let unable_to_externalize = |reason: Vec<StyledString>| { let unable_to_externalize = |reason: Vec<StyledString>| {
......
...@@ -1110,6 +1110,16 @@ impl FileSystemPath { ...@@ -1110,6 +1110,16 @@ impl FileSystemPath {
file_name file_name
} }
/// Returns true if this path has the given extension
///
/// slightly faster than `self.extension_ref() == Some(extension)` as we can simply match a
/// suffix
pub fn has_extension(&self, extension: &str) -> bool {
debug_assert!(!extension.contains('/') && extension.starts_with('.'));
self.path.ends_with(extension)
}
/// Returns the extension (without a leading `.`)
pub fn extension_ref(&self) -> Option<&str> { pub fn extension_ref(&self) -> Option<&str> {
let (_, extension) = self.split_extension(); let (_, extension) = self.split_extension();
extension extension
......
...@@ -399,7 +399,19 @@ impl SingleModuleGraph { ...@@ -399,7 +399,19 @@ impl SingleModuleGraph {
}) })
} }
/// Iterate over all nodes in the graph /// Returns true if the given module is in this graph and is an entry module
pub fn has_entry_module(&self, module: ResolvedVc<Box<dyn Module>>) -> bool {
if let Some(index) = self.modules.get(&module) {
self.graph
.edges_directed(*index, petgraph::Direction::Incoming)
.next()
.is_none()
} else {
false
}
}
/// Iterate over graph entry points
pub fn entry_modules(&self) -> impl Iterator<Item = ResolvedVc<Box<dyn Module>>> + '_ { pub fn entry_modules(&self) -> impl Iterator<Item = ResolvedVc<Box<dyn Module>>> + '_ {
self.entries.iter().flat_map(|e| e.entries()) self.entries.iter().flat_map(|e| e.entries())
} }
......
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