Skip to content

Commit 5b47c08

Browse files
authored
refactor: move code splitting cache into artifact (#12164)
1 parent fdb868e commit 5b47c08

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

crates/rspack_core/src/compilation/build_chunk_graph/artifact.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ impl CodeSplittingCache {
4646
fn can_skip_rebuilding_legacy(&self, this_compilation: &Compilation) -> bool {
4747
let logger = this_compilation.get_logger("rspack.Compilation.codeSplittingCache");
4848

49-
if !this_compilation
50-
.entries
51-
.keys()
52-
.eq(this_compilation.code_splitting_cache.entrypoints.keys())
53-
{
49+
if !this_compilation.entries.keys().eq(
50+
this_compilation
51+
.build_chunk_graph_artifact
52+
.code_splitting_cache
53+
.entrypoints
54+
.keys(),
55+
) {
5456
logger.log("entrypoints change detected, rebuilding chunk graph");
5557
return false;
5658
}
@@ -171,11 +173,13 @@ impl CodeSplittingCache {
171173
fn can_skip_rebuilding_new(&self, this_compilation: &Compilation) -> bool {
172174
let logger = this_compilation.get_logger("rspack.Compilation.codeSplittingCache");
173175

174-
if !this_compilation
175-
.entries
176-
.keys()
177-
.eq(this_compilation.code_splitting_cache.entrypoints.keys())
178-
{
176+
if !this_compilation.entries.keys().eq(
177+
this_compilation
178+
.build_chunk_graph_artifact
179+
.code_splitting_cache
180+
.entrypoints
181+
.keys(),
182+
) {
179183
logger.log("entrypoints change detected, rebuilding chunk graph");
180184
return false;
181185
}
@@ -303,11 +307,12 @@ where
303307
.passes_enabled(IncrementalPasses::BUILD_CHUNK_GRAPH);
304308
let new_code_splitting = compilation.options.experiments.parallel_code_splitting;
305309
let no_change = compilation
310+
.build_chunk_graph_artifact
306311
.code_splitting_cache
307312
.can_skip_rebuilding(compilation);
308313

309314
if (incremental_code_splitting && !new_code_splitting) || no_change {
310-
let cache = &mut compilation.code_splitting_cache;
315+
let cache = &mut compilation.build_chunk_graph_artifact.code_splitting_cache;
311316
rayon::scope(|s| {
312317
s.spawn(|_| compilation.chunk_by_ukey = cache.chunk_by_ukey.clone());
313318
s.spawn(|_| compilation.chunk_graph = cache.chunk_graph.clone());
@@ -335,7 +340,7 @@ where
335340
}
336341

337342
let compilation = task(compilation).await?;
338-
let cache = &mut compilation.code_splitting_cache;
343+
let cache = &mut compilation.build_chunk_graph_artifact.code_splitting_cache;
339344
rayon::scope(|s| {
340345
s.spawn(|_| cache.chunk_by_ukey = compilation.chunk_by_ukey.clone());
341346
s.spawn(|_| cache.chunk_graph = compilation.chunk_graph.clone());
@@ -355,7 +360,12 @@ where
355360

356361
map.insert(mid, (pre, post));
357362
}
358-
let cache = &mut compilation.code_splitting_cache;
363+
let cache = &mut compilation.build_chunk_graph_artifact.code_splitting_cache;
359364
cache.module_idx = map;
360365
Ok(())
361366
}
367+
368+
#[derive(Debug, Default)]
369+
pub struct BuildChunkGraphArtifact {
370+
pub code_splitting_cache: CodeSplittingCache,
371+
}

crates/rspack_core/src/compilation/build_chunk_graph/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ pub fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::Result<
1616
.incremental
1717
.mutations_readable(IncrementalPasses::BUILD_CHUNK_GRAPH);
1818
let mut splitter = if enable_incremental {
19-
std::mem::take(&mut compilation.code_splitting_cache.code_splitter)
19+
std::mem::take(
20+
&mut compilation
21+
.build_chunk_graph_artifact
22+
.code_splitting_cache
23+
.code_splitter,
24+
)
2025
} else {
2126
Default::default()
2227
};
@@ -47,7 +52,10 @@ pub fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::Result<
4752
compilation.chunk_graph.add_module(module_identifier)
4853
}
4954

50-
compilation.code_splitting_cache.code_splitter = splitter;
55+
compilation
56+
.build_chunk_graph_artifact
57+
.code_splitting_cache
58+
.code_splitter = splitter;
5159

5260
Ok(())
5361
}

crates/rspack_core/src/compilation/build_chunk_graph/new_code_splitter.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,7 @@ pub fn code_split(compilation: &mut Compilation) -> Result<()> {
18091809
let module_graph: &ModuleGraph<'_> = &compilation.get_module_graph();
18101810

18111811
let mut splitter = if !compilation
1812+
.build_chunk_graph_artifact
18121813
.code_splitting_cache
18131814
.new_code_splitter
18141815
.module_ordinal
@@ -1823,7 +1824,12 @@ pub fn code_split(compilation: &mut Compilation) -> Result<()> {
18231824
affected.extend(removed);
18241825

18251826
// reuse data from last computation
1826-
let mut splitter = std::mem::take(&mut compilation.code_splitting_cache.new_code_splitter);
1827+
let mut splitter = std::mem::take(
1828+
&mut compilation
1829+
.build_chunk_graph_artifact
1830+
.code_splitting_cache
1831+
.new_code_splitter,
1832+
);
18271833
splitter.invalidate(affected.into_iter());
18281834
splitter
18291835
} else {
@@ -1833,7 +1839,10 @@ pub fn code_split(compilation: &mut Compilation) -> Result<()> {
18331839
// fill chunks with its modules
18341840
splitter.create_chunks(compilation, &outgoings)?;
18351841

1836-
compilation.code_splitting_cache.new_code_splitter = splitter;
1842+
compilation
1843+
.build_chunk_graph_artifact
1844+
.code_splitting_cache
1845+
.new_code_splitter = splitter;
18371846

18381847
Ok(())
18391848
}

crates/rspack_core/src/compilation/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use std::{
1111
};
1212

1313
use build_chunk_graph::{
14-
artifact::{CodeSplittingCache, use_code_splitting_cache},
15-
build_chunk_graph, build_chunk_graph_new,
14+
artifact::use_code_splitting_cache, build_chunk_graph, build_chunk_graph_new,
1615
};
1716
use dashmap::DashSet;
1817
use futures::future::BoxFuture;
@@ -53,6 +52,7 @@ use crate::{
5352
ModuleStaticCacheArtifact, PathData, ResolverFactory, RuntimeGlobals, RuntimeKeyMap, RuntimeMode,
5453
RuntimeModule, RuntimeSpec, RuntimeSpecMap, RuntimeTemplate, SharedPluginDriver,
5554
SideEffectsOptimizeArtifact, SourceType, Stats, ValueCacheVersions,
55+
build_chunk_graph::artifact::BuildChunkGraphArtifact,
5656
compilation::make::{
5757
MakeArtifact, ModuleExecutor, UpdateParam, finish_make, make, update_module_graph,
5858
},
@@ -274,7 +274,7 @@ pub struct Compilation {
274274
pub code_generated_modules: IdentifierSet,
275275
pub build_time_executed_modules: IdentifierSet,
276276
pub old_cache: Arc<OldCache>,
277-
pub code_splitting_cache: CodeSplittingCache,
277+
pub build_chunk_graph_artifact: BuildChunkGraphArtifact,
278278
pub incremental: Incremental,
279279

280280
pub hash: Option<RspackHashDigest>,
@@ -405,7 +405,7 @@ impl Compilation {
405405
build_time_executed_modules: Default::default(),
406406
old_cache,
407407
incremental,
408-
code_splitting_cache: Default::default(),
408+
build_chunk_graph_artifact: Default::default(),
409409

410410
hash: None,
411411

crates/rspack_core/src/compiler/rebuild.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ impl Compiler {
8989
.swap_make_artifact_with_compilation(&mut new_compilation);
9090

9191
// seal stage used
92-
new_compilation.code_splitting_cache =
93-
std::mem::take(&mut self.compilation.code_splitting_cache);
92+
new_compilation.build_chunk_graph_artifact =
93+
std::mem::take(&mut self.compilation.build_chunk_graph_artifact);
9494

9595
// reuse module executor
9696
new_compilation.module_executor = std::mem::take(&mut self.compilation.module_executor);

0 commit comments

Comments
 (0)