Skip to content

Commit

Permalink
perf: reduce code splitting cache
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Nov 29, 2024
1 parent 0da3355 commit 00b19cb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
6 changes: 3 additions & 3 deletions crates/rspack_allocator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#[global_allocator]
#[cfg(not(miri))]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
// #[global_allocator]
// #[cfg(not(miri))]
// static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
4 changes: 2 additions & 2 deletions crates/rspack_core/src/build_chunk_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub(crate) fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::
.incremental
.can_read_mutations(IncrementalPasses::BUILD_CHUNK_GRAPH);
let mut splitter = if enable_incremental {
std::mem::take(&mut compilation.code_splitting_cache.code_splitter)
std::mem::take(&mut compilation.code_splitter_cache)
} else {
Default::default()
};
Expand All @@ -41,7 +41,7 @@ pub(crate) fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::
}

if enable_incremental {
compilation.code_splitting_cache.code_splitter = splitter;
compilation.code_splitter_cache = splitter;
}

Ok(())
Expand Down
10 changes: 6 additions & 4 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ use super::{
module_executor::ModuleExecutor,
};
use crate::{
build_chunk_graph::build_chunk_graph,
build_chunk_graph::{build_chunk_graph, code_splitter::CodeSplitter},
cache::Cache,
cgm_hash_results::CgmHashResults,
cgm_runtime_requirement_results::CgmRuntimeRequirementsResults,
get_runtime_key,
incremental::{Incremental, IncrementalPasses, Mutation},
is_source_equal,
old_cache::{use_code_splitting_cache, Cache as OldCache, CodeSplittingCache},
old_cache::{use_code_splitting_cache, Cache as OldCache},
to_identifier, BoxDependency, BoxModule, CacheCount, CacheOptions, Chunk, ChunkByUkey,
ChunkContentHash, ChunkGraph, ChunkGroupByUkey, ChunkGroupUkey, ChunkHashesResult, ChunkKind,
ChunkRenderResult, ChunkUkey, CodeGenerationJob, CodeGenerationResult, CodeGenerationResults,
Expand Down Expand Up @@ -181,7 +181,8 @@ pub struct Compilation {
pub build_time_executed_modules: IdentifierSet,
pub cache: Arc<dyn Cache>,
pub old_cache: Arc<OldCache>,
pub code_splitting_cache: CodeSplittingCache,
// pub code_splitting_cache: CodeSplittingCache,
pub code_splitter_cache: CodeSplitter,

Check failure on line 185 in crates/rspack_core/src/compiler/compilation.rs

View workflow job for this annotation

GitHub Actions / Rust check

type `build_chunk_graph::code_splitter::CodeSplitter` is more private than the item `compiler::compilation::Compilation::code_splitter_cache`
pub incremental: Incremental,

pub hash: Option<RspackHashDigest>,
Expand Down Expand Up @@ -284,7 +285,8 @@ impl Compilation {
cache,
old_cache,
incremental,
code_splitting_cache: Default::default(),
// code_splitting_cache: Default::default(),
code_splitter_cache: Default::default(),
hash: None,
used_chunk_ids: Default::default(),

Expand Down
20 changes: 17 additions & 3 deletions crates/rspack_core/src/compiler/hmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,27 @@ impl Compiler {
.compilation
.swap_make_artifact_with_compilation(&mut new_compilation);

// seal stage used
new_compilation.code_splitting_cache =
std::mem::take(&mut self.compilation.code_splitting_cache);
// reuse code splitting cache from the old compilation
new_compilation.chunk_by_ukey = std::mem::take(&mut self.compilation.chunk_by_ukey);
new_compilation.chunk_graph = std::mem::take(&mut self.compilation.chunk_graph);
new_compilation.chunk_group_by_ukey =
std::mem::take(&mut self.compilation.chunk_group_by_ukey);
new_compilation.entrypoints = std::mem::take(&mut self.compilation.entrypoints);
new_compilation.async_entrypoints = std::mem::take(&mut self.compilation.async_entrypoints);
new_compilation.named_chunk_groups =
std::mem::take(&mut self.compilation.named_chunk_groups);
new_compilation.named_chunks = std::mem::take(&mut self.compilation.named_chunks);

// reuse module executor
new_compilation.module_executor = std::mem::take(&mut self.compilation.module_executor);
}
if new_compilation
.incremental
.can_read_mutations(IncrementalPasses::BUILD_CHUNK_GRAPH)
{
new_compilation.code_splitter_cache =
std::mem::take(&mut self.compilation.code_splitter_cache);
}
if new_compilation
.incremental
.can_read_mutations(IncrementalPasses::INFER_ASYNC_MODULES)
Expand Down
41 changes: 14 additions & 27 deletions crates/rspack_core/src/old_cache/local/code_splitting_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,24 @@ where
}

let has_change = compilation.has_module_import_export_change();
if !has_change
|| compilation
if has_change
&& !compilation
.incremental
.can_read_mutations(IncrementalPasses::BUILD_CHUNK_GRAPH)
{
let cache = &mut compilation.code_splitting_cache;
rayon::scope(|s| {
s.spawn(|_| compilation.chunk_by_ukey = cache.chunk_by_ukey.clone());
s.spawn(|_| compilation.chunk_graph = cache.chunk_graph.clone());
s.spawn(|_| compilation.chunk_group_by_ukey = cache.chunk_group_by_ukey.clone());
s.spawn(|_| compilation.entrypoints = cache.entrypoints.clone());
s.spawn(|_| compilation.async_entrypoints = cache.async_entrypoints.clone());
s.spawn(|_| compilation.named_chunk_groups = cache.named_chunk_groups.clone());
s.spawn(|_| compilation.named_chunks = cache.named_chunks.clone());
});
compilation.chunk_by_ukey = Default::default();
compilation.chunk_graph = Default::default();
compilation.chunk_group_by_ukey = Default::default();
compilation.entrypoints = Default::default();
compilation.async_entrypoints = Default::default();
compilation.named_chunk_groups = Default::default();
compilation.named_chunks = Default::default();
}

if !has_change {
return Ok(());
}
if !has_change {
return Ok(());
}

let compilation = task(compilation).await?;
let cache = &mut compilation.code_splitting_cache;
rayon::scope(|s| {
s.spawn(|_| cache.chunk_by_ukey = compilation.chunk_by_ukey.clone());
s.spawn(|_| cache.chunk_graph = compilation.chunk_graph.clone());
s.spawn(|_| cache.chunk_group_by_ukey = compilation.chunk_group_by_ukey.clone());
s.spawn(|_| cache.entrypoints = compilation.entrypoints.clone());
s.spawn(|_| cache.async_entrypoints = compilation.async_entrypoints.clone());
s.spawn(|_| cache.named_chunk_groups = compilation.named_chunk_groups.clone());
s.spawn(|_| cache.named_chunks = compilation.named_chunks.clone());
});
Ok(())
task(compilation).await?;
return Ok(());
}

0 comments on commit 00b19cb

Please sign in to comment.