Skip to content

Commit

Permalink
perf: avoid building module graph if dynamic specifier already in gra…
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed May 29, 2024
1 parent 94f040a commit 814ac9a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
32 changes: 29 additions & 3 deletions cli/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl ModuleLoadPreparer {
)
.await?;

self.module_graph_builder.graph_roots_valid(graph, roots)?;
self.graph_roots_valid(graph, roots)?;

// write the lockfile if there is one
if let Some(lockfile) = &self.lockfile {
Expand Down Expand Up @@ -205,6 +205,14 @@ impl ModuleLoadPreparer {

Ok(())
}

pub fn graph_roots_valid(
&self,
graph: &ModuleGraph,
roots: &[ModuleSpecifier],
) -> Result<(), AnyError> {
self.module_graph_builder.graph_roots_valid(graph, roots)
}
}

struct SharedCliModuleLoaderState {
Expand Down Expand Up @@ -806,8 +814,26 @@ impl<TGraphContainer: ModuleGraphContainer> ModuleLoader
let inner = self.0.clone();

async move {
let graph_container = inner.graph_container.clone();
let module_load_preparer = inner.shared.module_load_preparer.clone();
let graph_container = &inner.graph_container;
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 {
inner.dynamic_permissions.clone()
Expand Down
8 changes: 8 additions & 0 deletions tests/specs/run/dynamic_already_prepared/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tests": {
"skips_prepare_module_load": {
"args": "run -A --log-level=debug main.ts",
"output": "main.out"
}
}
}
1 change: 1 addition & 0 deletions tests/specs/run/dynamic_already_prepared/dynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(1);
1 change: 1 addition & 0 deletions tests/specs/run/dynamic_already_prepared/dynamic2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
await import("./dynamic3.ts");
1 change: 1 addition & 0 deletions tests/specs/run/dynamic_already_prepared/dynamic3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
await import("./dynamic.ts");
1 change: 1 addition & 0 deletions tests/specs/run/dynamic_already_prepared/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[WILDCARD]Prepared module load.[WILDCARD]Skipping prepare module load.[WILDCARD]Skipping prepare module load.[WILDCARD]Skipping prepare module load.[WILDCARD]
2 changes: 2 additions & 0 deletions tests/specs/run/dynamic_already_prepared/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
await import("./dynamic.ts");
await import("./dynamic2.ts");

0 comments on commit 814ac9a

Please sign in to comment.