Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: introducing the faster code-splitting algorithm #8823

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,7 @@ export interface RawExperiments {
layers: boolean
topLevelAwait: boolean
incremental?: false | { [key: string]: boolean }
parallelCodeSplitting: boolean
rspackFuture?: RawRspackFuture
cache: boolean | { type: "persistent" } & RawExperimentCacheOptionsPersistent | { type: "memory" }
}
Expand Down
10 changes: 10 additions & 0 deletions crates/rspack/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2686,6 +2686,7 @@ pub struct ExperimentsBuilder {
output_module: Option<bool>,
future_defaults: Option<bool>,
css: Option<bool>,
parallel_code_splitting: Option<bool>,
async_web_assembly: Option<bool>,
// TODO: lazy compilation
}
Expand All @@ -2701,6 +2702,7 @@ impl From<&mut ExperimentsBuilder> for ExperimentsBuilder {
output_module: value.output_module.take(),
future_defaults: value.future_defaults.take(),
css: value.css.take(),
parallel_code_splitting: value.parallel_code_splitting.take(),
async_web_assembly: value.async_web_assembly.take(),
}
}
Expand Down Expand Up @@ -2742,6 +2744,11 @@ impl ExperimentsBuilder {
self
}

pub fn parallel_code_splitting(&mut self, parallel_code_splitting: bool) -> &mut Self {
self.parallel_code_splitting = Some(parallel_code_splitting);
self
}

pub fn build(
&mut self,
_builder_context: &mut BuilderContext,
Expand Down Expand Up @@ -2772,11 +2779,14 @@ impl ExperimentsBuilder {
w!(self.async_web_assembly, *future_defaults);
w!(self.output_module, false);

let parallel_code_splitting = d!(self.parallel_code_splitting, false);

Experiments {
layers,
incremental,
top_level_await,
rspack_future,
parallel_code_splitting,
cache,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct RawExperiments {
pub top_level_await: bool,
#[napi(ts_type = "false | { [key: string]: boolean }")]
pub incremental: Option<WithFalse<RawIncremental>>,
pub parallel_code_splitting: bool,
pub rspack_future: Option<RawRspackFuture>,
#[napi(
ts_type = r#"boolean | { type: "persistent" } & RawExperimentCacheOptionsPersistent | { type: "memory" }"#
Expand All @@ -34,6 +35,7 @@ impl From<RawExperiments> for Experiments {
},
None => IncrementalPasses::empty(),
},
parallel_code_splitting: value.parallel_code_splitting,
layers: value.layers,
top_level_await: value.top_level_await,
rspack_future: value.rspack_future.unwrap_or_default().into(),
Expand Down
13 changes: 8 additions & 5 deletions crates/rspack_core/src/build_chunk_graph/code_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
assign_depths(
&mut assign_depths_map,
&compilation.get_module_graph(),
modules.iter().collect(),
modules.iter(),
);
input_entrypoints_and_modules.insert(entry_point, modules);
}
Expand Down Expand Up @@ -1559,7 +1559,11 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
return modules.clone();
}

self.extract_block_modules(module.get_root_block(compilation), runtime, compilation);
self.extract_block_modules(
module.get_root_block(&compilation.get_module_graph()),
runtime,
compilation,
);
self
.block_modules_runtime_map
.get::<OptionalRuntimeSpec>(&runtime.cloned().into())
Expand Down Expand Up @@ -1952,11 +1956,10 @@ pub(crate) enum DependenciesBlockIdentifier {
}

impl DependenciesBlockIdentifier {
pub fn get_root_block<'a>(&'a self, compilation: &'a Compilation) -> ModuleIdentifier {
pub fn get_root_block<'a>(&'a self, module_graph: &'a ModuleGraph) -> ModuleIdentifier {
match self {
DependenciesBlockIdentifier::Module(m) => *m,
DependenciesBlockIdentifier::AsyncDependenciesBlock(id) => *compilation
.get_module_graph()
DependenciesBlockIdentifier::AsyncDependenciesBlock(id) => *module_graph
.block_by_id(id)
.expect("should have block")
.parent(),
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/build_chunk_graph/incremental.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl CodeSplitter {
continue;
};

parent_cg.children.remove(&chunk_group_ukey);
parent_cg.children.swap_remove_full(&chunk_group_ukey);

if let Some(parent_cgi) = self.chunk_group_info_map.get(parent) {
if let Some(parent_cgi) = self.chunk_group_infos.get_mut(parent_cgi) {
Expand Down
7 changes: 7 additions & 0 deletions crates/rspack_core/src/build_chunk_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{incremental::IncrementalPasses, Compilation};

pub(crate) mod code_splitter;
pub(crate) mod incremental;
pub(crate) mod new_code_splitter;

#[instrument("Compilation:build_chunk_graph", skip_all)]
pub(crate) fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::Result<()> {
Expand Down Expand Up @@ -46,3 +47,9 @@ pub(crate) fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::

Ok(())
}

#[instrument(skip_all)]
pub(crate) fn build_chunk_graph_new(compilation: &mut Compilation) -> rspack_error::Result<()> {
new_code_splitter::code_split(compilation)?;
Ok(())
}
Loading
Loading