perf: avoid building module graph if dynamic specifier already in graph (#24035)

This commit is contained in:
David Sherret 2024-05-29 14:59:35 -04:00 committed by GitHub
parent 94f040ac28
commit 814ac9a75d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 43 additions and 3 deletions

View File

@ -171,7 +171,7 @@ impl ModuleLoadPreparer {
) )
.await?; .await?;
self.module_graph_builder.graph_roots_valid(graph, roots)?; self.graph_roots_valid(graph, roots)?;
// write the lockfile if there is one // write the lockfile if there is one
if let Some(lockfile) = &self.lockfile { if let Some(lockfile) = &self.lockfile {
@ -205,6 +205,14 @@ impl ModuleLoadPreparer {
Ok(()) Ok(())
} }
pub fn graph_roots_valid(
&self,
graph: &ModuleGraph,
roots: &[ModuleSpecifier],
) -> Result<(), AnyError> {
self.module_graph_builder.graph_roots_valid(graph, roots)
}
} }
struct SharedCliModuleLoaderState { struct SharedCliModuleLoaderState {
@ -806,8 +814,26 @@ impl<TGraphContainer: ModuleGraphContainer> ModuleLoader
let inner = self.0.clone(); let inner = self.0.clone();
async move { async move {
let graph_container = inner.graph_container.clone(); let graph_container = &inner.graph_container;
let module_load_preparer = inner.shared.module_load_preparer.clone(); let module_load_preparer = &inner.shared.module_load_preparer;
if is_dynamic {
// When the specifier is already in the graph then it means it
// was previously loaded, so we can skip that and only check if
// this part of the graph is valid.
//
// This doesn't acquire a graph update permit because that will
// clone the graph which is a bit slow.
let graph = graph_container.graph();
if !graph.roots.is_empty() && graph.get(&specifier).is_some() {
log::debug!("Skipping prepare module load.");
// roots are already validated so we can skip those
if !graph.roots.contains(&specifier) {
module_load_preparer.graph_roots_valid(&graph, &[specifier])?;
}
return Ok(());
}
}
let root_permissions = if is_dynamic { let root_permissions = if is_dynamic {
inner.dynamic_permissions.clone() inner.dynamic_permissions.clone()

View File

@ -0,0 +1,8 @@
{
"tests": {
"skips_prepare_module_load": {
"args": "run -A --log-level=debug main.ts",
"output": "main.out"
}
}
}

View File

@ -0,0 +1 @@
console.log(1);

View File

@ -0,0 +1 @@
await import("./dynamic3.ts");

View File

@ -0,0 +1 @@
await import("./dynamic.ts");

View File

@ -0,0 +1 @@
[WILDCARD]Prepared module load.[WILDCARD]Skipping prepare module load.[WILDCARD]Skipping prepare module load.[WILDCARD]Skipping prepare module load.[WILDCARD]

View File

@ -0,0 +1,2 @@
await import("./dynamic.ts");
await import("./dynamic2.ts");