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 {
client_shared_entries: Vc<EvaluatableAssets>,
has_layout_segments: bool,
) -> Result<Vc<ModuleGraphs>> {
let client_shared_entries = client_shared_entries
.await?
.into_iter()
.map(|m| ResolvedVc::upcast(*m))
.collect();
let should_trace = self.project.next_mode().await?.is_production();
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
.await?
.into_iter()
.map(|m| ResolvedVc::upcast(*m))
.collect();
// Implements layout segment optimization to compute a graph "chain" for each layout
// segment
async move {
......@@ -1255,7 +1254,7 @@ impl AppEndpoint {
client_assets.insert(chunk);
let chunk_path = chunk.path().await?;
if chunk_path.extension_ref() == Some("js") {
if chunk_path.has_extension(".js") {
client_shared_chunks.push(chunk);
}
}
......
......@@ -82,7 +82,7 @@ impl NextDynamicGraph {
}
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
return Ok(Vc::cell(vec![]));
}
......@@ -185,7 +185,7 @@ impl ServerActionsGraph {
// The graph contains the whole app, traverse and collect all reachable imports.
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
return Ok(Vc::cell(Default::default()));
}
......@@ -266,7 +266,7 @@ impl ClientReferencesGraph {
}
#[turbo_tasks::function]
pub async fn get_client_references_for_endpoint(
async fn get_client_references_for_endpoint(
&self,
entry: ResolvedVc<Box<dyn Module>>,
) -> Result<Vc<ClientReferenceGraphResult>> {
......@@ -276,7 +276,7 @@ impl ClientReferencesGraph {
let graph = &*self.graph.await?;
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
return Ok(ClientReferenceGraphResult::default().cell());
}
......@@ -467,9 +467,7 @@ async fn validate_pages_css_imports(
let module_name_map = module_name_map.await?;
let entries = if !is_single_page {
// TODO: Optimize this code by checking if the node is an entry using `get_module` and then
// checking if the node is an entry in the graph by looking for the reverse edges.
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
return Ok(());
}
......
......@@ -218,7 +218,7 @@ impl Asset for NftJsonAsset {
}
let referenced_chunk_path = referenced_chunk.path().await?;
if referenced_chunk_path.extension_ref() == Some("map") {
if referenced_chunk_path.has_extension(".map") {
continue;
}
......
......@@ -268,11 +268,10 @@ impl AppPageLoaderTreeBuilder {
writeln!(self.loader_tree_code, "{s} width: {identifier}.width,")?;
writeln!(self.loader_tree_code, "{s} height: {identifier}.height,")?;
} else {
let ext = path.extension();
// 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.
// 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()
} else {
format!("${{{identifier}.width}}x${{{identifier}.height}}")
......
......@@ -32,7 +32,7 @@ pub struct ClientReferencesChunks {
/// 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.
#[turbo_tasks::function]
pub async fn get_app_client_references_chunks(
......
......@@ -218,26 +218,22 @@ impl Visit<FindServerEntriesNode> for FindServerEntries {
}
fn edges(&mut self, node: &FindServerEntriesNode) -> Self::EdgesFuture {
let node = node.clone();
let include_traced = self.include_traced;
let parent_module = match node {
// This should never occur since we always skip visiting these
// nodes' edges.
FindServerEntriesNode::ClientReference => {
unreachable!("ClientReference node should not be visited")
}
FindServerEntriesNode::Internal(module, _) => **module,
FindServerEntriesNode::ServerUtilEntry(module, _) => Vc::upcast(**module),
FindServerEntriesNode::ServerComponentEntry(module, _) => Vc::upcast(**module),
};
async move {
let parent_module = match node {
// This should never occur since we always skip visiting these
// nodes' edges.
FindServerEntriesNode::ClientReference => {
unreachable!("ClientReference node should not be visited")
}
FindServerEntriesNode::Internal(module, _) => module,
FindServerEntriesNode::ServerUtilEntry(module, _) => ResolvedVc::upcast(module),
FindServerEntriesNode::ServerComponentEntry(module, _) => {
ResolvedVc::upcast(module)
}
};
// Pass include_traced to reuse the same cached `primary_chunkable_referenced_modules`
// task result, but the traced references will be filtered out again afterwards.
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
.iter()
......
......@@ -331,60 +331,52 @@ impl ClientReferenceManifest {
.value_to_string()
.owned()
.await?;
let mut entry_css_files_with_chunk = Vec::new();
let entry_js_files = entry_manifest
.entry_js_files
.entry(server_component_name.clone())
.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_with_path =
cached_chunk_paths(&mut client_chunk_path_cache, client_chunks.iter().copied())
.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 {
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
// applied because it is added by Nex.js during rendering.
// applied because it is added by Next.js during rendering.
let path = path.into();
if chunk_path.extension_ref() == Some("css") {
entry_css_files_with_chunk.push((path, chunk));
if chunk_path.has_extension(".css") {
let content = if inlined_css {
Some(
if let Some(content_file) =
chunk.content().file_content().await?.as_content()
{
content_file.content().to_str()?.into()
} else {
RcStr::default()
},
)
} else {
None
};
entry_css_files.insert(CssResource {
path,
inlined: inlined_css,
content,
});
} else {
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) =
chunk.content().file_content().await?.as_content()
{
Some(content_file.content().to_str()?.into())
} else {
Some("".into())
}
} else {
None
};
Ok(CssResource {
path,
inlined,
content,
})
})
.try_join()
.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();
......
......@@ -169,35 +169,32 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
) -> Result<FileType> {
// node.js only supports these file extensions
// mjs is an esm module and we can't bundle that yet
let ext = raw_fs_path.extension_ref();
if matches!(ext, Some("cjs" | "node" | "json")) {
return Ok(FileType::CommonJs);
}
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
// package.json
let FindContextFileResult::Found(package_json, _) =
&*find_context_file(fs_path.parent(), package_json()).await?
else {
// can't find package.json
return Ok(FileType::CommonJs);
};
let FileJsonContent::Content(package) = &*package_json.read_json().await? else {
// can't parse package.json
return Ok(FileType::InvalidPackageJson);
};
if let Some("module") = package["type"].as_str() {
return Ok(FileType::EcmaScriptModule);
Ok(match raw_fs_path.extension_ref() {
Some("cjs" | "node" | "json") => FileType::CommonJs,
Some("mjs") => FileType::EcmaScriptModule,
Some("js") => {
// for .js extension in cjs context, we need to check the actual module type via
// package.json
let FindContextFileResult::Found(package_json, _) =
&*find_context_file(fs_path.parent(), package_json()).await?
else {
// can't find package.json
return Ok(FileType::CommonJs);
};
let FileJsonContent::Content(package) = &*package_json.read_json().await?
else {
// can't parse package.json
return Ok(FileType::InvalidPackageJson);
};
if let Some("module") = package["type"].as_str() {
FileType::EcmaScriptModule
} else {
FileType::CommonJs
}
}
return Ok(FileType::CommonJs);
}
Ok(FileType::UnsupportedExtension)
_ => FileType::UnsupportedExtension,
})
}
let unable_to_externalize = |reason: Vec<StyledString>| {
......
......@@ -1110,6 +1110,16 @@ impl FileSystemPath {
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> {
let (_, extension) = self.split_extension();
extension
......
......@@ -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>>> + '_ {
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