Skip to content

Commit

Permalink
fix: should re-build chunk graph when adding a entry connection (#9537)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Mar 4, 2025
1 parent 1a69bdb commit 44f96be
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 230 deletions.
15 changes: 13 additions & 2 deletions crates/rspack_core/src/artifacts/cgm_hash_artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@ impl CgmHashArtifact {
hashes.get(runtime)
}

pub fn set_hashes(&mut self, module: ModuleIdentifier, hashes: RuntimeSpecMap<RspackHashDigest>) {
self.module_to_hashes.insert(module, hashes);
pub fn set_hashes(
&mut self,
module: ModuleIdentifier,
hashes: RuntimeSpecMap<RspackHashDigest>,
) -> bool {
if let Some(old) = self.module_to_hashes.get(&module)
&& old == &hashes
{
false
} else {
self.module_to_hashes.insert(module, hashes);
true
}
}

pub fn remove(&mut self, module: &ModuleIdentifier) -> Option<RuntimeSpecMap<RspackHashDigest>> {
Expand Down
18 changes: 8 additions & 10 deletions crates/rspack_core/src/build_chunk_graph/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,16 +509,14 @@ impl CodeSplitter {

// If after edges rebuild there are still some entries not included in entrypoints
// then they are new added entries and we build them.
if compilation.entries.len() > compilation.entrypoints.len() {
let new_entries: Vec<_> = compilation
.entries
.keys()
.filter(|entry| !compilation.entrypoints.contains_key(entry.as_str()))
.map(|entry| ChunkReCreation::Entry(entry.to_owned()))
.collect();
for edge in new_entries {
edge.rebuild(self, compilation)?;
}
let new_entries: Vec<_> = compilation
.entries
.keys()
.filter(|entry| !compilation.entrypoints.contains_key(entry.as_str()))
.map(|entry| ChunkReCreation::Entry(entry.to_owned()))
.collect();
for edge in new_entries {
edge.rebuild(self, compilation)?;
}

// Ensure entrypoints always have the same order with entries
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/chunk_graph/chunk_graph_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ impl ChunkGraph {
compilation: &mut Compilation,
module_identifier: ModuleIdentifier,
hashes: RuntimeSpecMap<RspackHashDigest>,
) {
) -> bool {
compilation
.cgm_hash_artifact
.set_hashes(module_identifier, hashes);
.set_hashes(module_identifier, hashes)
}

pub fn try_get_module_chunks(
Expand Down
62 changes: 55 additions & 7 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1421,14 +1421,46 @@ impl Compilation {
for revoked_module in revoked_modules {
self.cgm_hash_artifact.remove(&revoked_module);
}
let modules = mutations.get_affected_modules_with_chunk_graph(self);
let mg = self.get_module_graph();
let mut affected_modules = mutations.get_affected_modules_with_module_graph(&mg);
for mutation in mutations.iter() {
match mutation {
Mutation::ModuleSetAsync { module } => {
affected_modules.insert(*module);
}
Mutation::ModuleSetId { module } => {
affected_modules.insert(*module);
affected_modules.extend(
mg.get_incoming_connections(module)
.filter_map(|c| c.original_module_identifier),
);
}
Mutation::ChunkAdd { chunk } => {
affected_modules.extend(self.chunk_graph.get_chunk_modules_identifier(chunk));
}
Mutation::ChunkSetId { chunk } => {
let chunk = self.chunk_by_ukey.expect_get(chunk);
affected_modules.extend(
chunk
.groups()
.iter()
.flat_map(|group| {
let group = self.chunk_group_by_ukey.expect_get(group);
group.origins()
})
.filter_map(|origin| origin.module),
);
}
_ => {}
}
}
let logger = self.get_logger("rspack.incremental.modulesHashes");
logger.log(format!(
"{} modules are affected, {} in total",
modules.len(),
self.get_module_graph().modules().len()
affected_modules.len(),
mg.modules().len()
));
modules
affected_modules
} else {
self.get_module_graph().modules().keys().copied().collect()
};
Expand Down Expand Up @@ -1456,7 +1488,13 @@ impl Compilation {
for revoked_module in revoked_modules {
self.code_generation_results.remove(&revoked_module);
}
let modules = mutations.get_affected_modules_with_chunk_graph(self);
let modules: IdentifierSet = mutations
.iter()
.filter_map(|mutation| match mutation {
Mutation::ModuleSetHashes { module } => Some(*module),
_ => None,
})
.collect();
let logger = self.get_logger("rspack.incremental.modulesCodegen");
logger.log(format!(
"{} modules are affected, {} in total",
Expand Down Expand Up @@ -1490,7 +1528,13 @@ impl Compilation {
.cgm_runtime_requirements_artifact
.remove(&revoked_module);
}
let modules = mutations.get_affected_modules_with_chunk_graph(self);
let modules: IdentifierSet = mutations
.iter()
.filter_map(|mutation| match mutation {
Mutation::ModuleSetHashes { module } => Some(*module),
_ => None,
})
.collect();
let logger = self.get_logger("rspack.incremental.modulesRuntimeRequirements");
logger.log(format!(
"{} modules are affected, {} in total",
Expand Down Expand Up @@ -2164,7 +2208,11 @@ impl Compilation {
})
.collect::<Result<_>>()?;
for (module, hashes) in results {
ChunkGraph::set_module_hashes(self, module, hashes);
if ChunkGraph::set_module_hashes(self, module, hashes)
&& let Some(mutations) = self.incremental.mutations_write()
{
mutations.add(Mutation::ModuleSetHashes { module });
}
}
Ok(())
}
Expand Down
5 changes: 0 additions & 5 deletions crates/rspack_core/src/compiler/rebuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ impl Compiler {

new_compilation.hot_index = self.compilation.hot_index + 1;

if let Some(mutations) = new_compilation.incremental.mutations_write()
&& let Some(old_mutations) = self.compilation.incremental.mutations_write()
{
mutations.swap_modules_with_chunk_graph_cache(old_mutations);
}
if new_compilation
.incremental
.can_read_mutations(IncrementalPasses::MAKE)
Expand Down
Loading

2 comments on commit 44f96be

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 44f96be Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ecosystem CI detail: Open

suite result
modernjs ✅ success
rspress ✅ success
rslib ❌ failure
rsbuild ✅ success
rsdoctor ❌ failure
examples ✅ success
devserver ✅ success
nuxt ✅ success

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on 44f96be Mar 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2025-03-04 ec71bd0) Current Change
10000_big_production-mode_disable-minimize + exec 36.7 s ± 403 ms 36.6 s ± 642 ms -0.23 %
10000_development-mode + exec 1.71 s ± 20 ms 1.66 s ± 13 ms -3.27 %
10000_development-mode_hmr + exec 681 ms ± 18 ms 664 ms ± 6.4 ms -2.51 %
10000_production-mode + exec 2.21 s ± 117 ms 2.1 s ± 137 ms -5.05 %
10000_production-mode_persistent-cold + exec 2.33 s ± 50 ms 2.24 s ± 112 ms -4.14 %
10000_production-mode_persistent-hot + exec 1.64 s ± 26 ms 1.57 s ± 27 ms -4.40 %
arco-pro_development-mode + exec 1.73 s ± 128 ms 1.7 s ± 144 ms -1.53 %
arco-pro_development-mode_hmr + exec 375 ms ± 1.9 ms 374 ms ± 1.5 ms -0.29 %
arco-pro_production-mode + exec 3.54 s ± 113 ms 3.47 s ± 147 ms -2.13 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.58 s ± 191 ms 3.57 s ± 191 ms -0.28 %
arco-pro_production-mode_persistent-cold + exec 3.56 s ± 148 ms 3.48 s ± 186 ms -2.44 %
arco-pro_production-mode_persistent-hot + exec 2.25 s ± 115 ms 2.29 s ± 121 ms +1.94 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.53 s ± 177 ms 3.44 s ± 89 ms -2.36 %
large-dyn-imports_development-mode + exec 1.95 s ± 58 ms 1.89 s ± 45 ms -3.29 %
large-dyn-imports_production-mode + exec 2 s ± 57 ms 1.96 s ± 70 ms -2.15 %
threejs_development-mode_10x + exec 1.43 s ± 35 ms 1.45 s ± 49 ms +0.79 %
threejs_development-mode_10x_hmr + exec 786 ms ± 10 ms 778 ms ± 6.2 ms -0.99 %
threejs_production-mode_10x + exec 5.03 s ± 77 ms 4.96 s ± 165 ms -1.40 %
threejs_production-mode_10x_persistent-cold + exec 5.13 s ± 357 ms 5.01 s ± 256 ms -2.41 %
threejs_production-mode_10x_persistent-hot + exec 4.47 s ± 335 ms 4.31 s ± 245 ms -3.51 %
10000_big_production-mode_disable-minimize + rss memory 8698 MiB ± 78.5 MiB 9630 MiB ± 438 MiB +10.71 %
10000_development-mode + rss memory 678 MiB ± 11.8 MiB 687 MiB ± 19.6 MiB +1.32 %
10000_development-mode_hmr + rss memory 1247 MiB ± 285 MiB 1428 MiB ± 284 MiB +14.53 %
10000_production-mode + rss memory 650 MiB ± 26.5 MiB 700 MiB ± 59 MiB +7.77 %
10000_production-mode_persistent-cold + rss memory 762 MiB ± 20 MiB 824 MiB ± 50.6 MiB +8.20 %
10000_production-mode_persistent-hot + rss memory 735 MiB ± 11.5 MiB 755 MiB ± 104 MiB +2.64 %
arco-pro_development-mode + rss memory 636 MiB ± 21.9 MiB 597 MiB ± 42.3 MiB -6.09 %
arco-pro_development-mode_hmr + rss memory 709 MiB ± 21.4 MiB 630 MiB ± 54.3 MiB -11.12 %
arco-pro_production-mode + rss memory 776 MiB ± 38.8 MiB 749 MiB ± 54.2 MiB -3.55 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 792 MiB ± 25.6 MiB 738 MiB ± 53 MiB -6.84 %
arco-pro_production-mode_persistent-cold + rss memory 863 MiB ± 20.6 MiB 804 MiB ± 53.5 MiB -6.86 %
arco-pro_production-mode_persistent-hot + rss memory 832 MiB ± 32 MiB 821 MiB ± 60.4 MiB -1.23 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 800 MiB ± 35.8 MiB 757 MiB ± 89.1 MiB -5.40 %
large-dyn-imports_development-mode + rss memory 697 MiB ± 17 MiB 677 MiB ± 7.43 MiB -2.86 %
large-dyn-imports_production-mode + rss memory 576 MiB ± 3.62 MiB 570 MiB ± 10.2 MiB -1.02 %
threejs_development-mode_10x + rss memory 651 MiB ± 59.5 MiB 694 MiB ± 53.1 MiB +6.71 %
threejs_development-mode_10x_hmr + rss memory 974 MiB ± 301 MiB 1027 MiB ± 275 MiB +5.41 %
threejs_production-mode_10x + rss memory 919 MiB ± 56.1 MiB 979 MiB ± 52.6 MiB +6.51 %
threejs_production-mode_10x_persistent-cold + rss memory 1050 MiB ± 71.5 MiB 1092 MiB ± 55.1 MiB +3.96 %
threejs_production-mode_10x_persistent-hot + rss memory 916 MiB ± 60.7 MiB 906 MiB ± 39 MiB -1.07 %

Please sign in to comment.