Skip to content

Commit cc5e3f6

Browse files
authored
feat: add rust benchmark case for code splitting (#9127)
1 parent 8e33012 commit cc5e3f6

File tree

12 files changed

+10266
-134
lines changed

12 files changed

+10266
-134
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rspack_core/src/build_chunk_graph/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub(crate) mod incremental;
1010
pub(crate) mod new_code_splitter;
1111

1212
#[instrument("Compilation:build_chunk_graph", skip_all)]
13-
pub(crate) fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::Result<()> {
13+
pub fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::Result<()> {
1414
let enable_incremental = compilation
1515
.incremental
1616
.can_read_mutations(IncrementalPasses::BUILD_CHUNK_GRAPH);
@@ -19,6 +19,7 @@ pub(crate) fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::
1919
} else {
2020
Default::default()
2121
};
22+
2223
splitter.update_with_compilation(compilation)?;
2324

2425
if !enable_incremental || splitter.chunk_group_infos.is_empty() {
@@ -49,7 +50,7 @@ pub(crate) fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::
4950
}
5051

5152
#[instrument(skip_all)]
52-
pub(crate) fn build_chunk_graph_new(compilation: &mut Compilation) -> rspack_error::Result<()> {
53+
pub fn build_chunk_graph_new(compilation: &mut Compilation) -> rspack_error::Result<()> {
5354
new_code_splitter::code_split(compilation)?;
5455
Ok(())
5556
}

crates/rspack_core/src/build_chunk_graph/new_code_splitter.rs

Lines changed: 1 addition & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -828,127 +828,9 @@ impl CodeSplitter {
828828

829829
fn create_chunks(&mut self, compilation: &mut Compilation) -> Result<()> {
830830
let mut errors = vec![];
831-
// let mut roots: Vec<CreateChunkRoot> = vec![];
832-
// for (idx, (name, data)) in entries.iter().enumerate() {
833-
// name_to_idx.insert(name, idx);
834-
// let runtime = if let Some(depend_on) = &data.options.depend_on {
835-
// deps.push((name, depend_on.clone()));
836-
// None
837-
// } else {
838-
// Some(RuntimeSpec::from_entry_options(&data.options).expect("should have runtime"))
839-
// };
840-
841-
// // set runtime later
842-
// roots.push(CreateChunkRoot::Entry(name.clone(), data.clone(), runtime));
843-
// }
844-
845-
// let mut entry_to_deps = HashMap::default();
846-
// for (entry, deps) in deps {
847-
// entry_to_deps.insert(
848-
// entry.as_str(),
849-
// deps
850-
// .into_iter()
851-
// .map(|dep| *name_to_idx.get(&dep).expect("should have idx"))
852-
// .collect::<Vec<_>>(),
853-
// );
854-
// }
855-
856-
// for (entry, _) in entries.iter() {
857-
// let curr = *name_to_idx.get(entry).expect("unreachable");
858-
// if roots[curr].get_runtime().is_some() {
859-
// // already set
860-
// continue;
861-
// }
862-
// let mut visited = Default::default();
863-
// if let Err(dep) =
864-
// set_entry_runtime_and_depend_on(curr, &mut roots, &entry_to_deps, &mut visited, &mut vec![])
865-
// {
866-
// let dep_name = roots[dep].entry_name();
867-
// let dep_rt = RuntimeSpec::from_entry(dep_name, None);
868-
// error_roots.push((curr, Diagnostic::from(error!("Entrypoints '{entry}' and '{dep_name}' use 'dependOn' to depend on each other in a circular way."))));
869-
// roots[dep].set_runtime(dep_rt);
870-
// roots[curr].set_runtime(RuntimeSpec::from_entry(entry, None));
871-
// }
872-
// }
873-
874-
// let mut entry_module_runtime = IdentifierMap::<RuntimeSpec>::default();
875-
// let module_graph: ModuleGraph = compilation.get_module_graph();
876-
// for root in &roots {
877-
// if let CreateChunkRoot::Entry(_name, data, runtime) = root {
878-
// let runtime = runtime
879-
// .as_ref()
880-
// .expect("should have runtime after calculated depend on");
881-
// for dep_id in compilation
882-
// .global_entry
883-
// .all_dependencies()
884-
// .chain(data.all_dependencies())
885-
// {
886-
// let Some(module) = module_graph.module_identifier_by_dependency_id(dep_id) else {
887-
// continue;
888-
// };
889-
// match entry_module_runtime.entry(*module) {
890-
// std::collections::hash_map::Entry::Occupied(mut existing) => {
891-
// let new_runtime = merge_runtime(existing.get(), &runtime);
892-
// existing.insert(new_runtime);
893-
// }
894-
// std::collections::hash_map::Entry::Vacant(vacant) => {
895-
// vacant.insert(runtime.clone());
896-
// }
897-
// }
898-
// }
899-
// }
900-
// }
901-
902-
// let module_graph = compilation.get_module_graph();
903-
// let module_cache = DashMap::default();
904-
905-
// roots.extend(
906-
// self
907-
// .blocks
908-
// .par_iter()
909-
// .map(|(block_id, origin)| {
910-
// let visited = IdentifierDashSet::default();
911-
// let block = module_graph
912-
// .block_by_id(block_id)
913-
// .expect("should have block");
914-
// let runtime = if let Some(group_options) = block.get_group_options()
915-
// && let Some(entry_options) = group_options.entry_options()
916-
// {
917-
// RuntimeSpec::from_entry_options(entry_options).or_else(|| {
918-
// determine_runtime(
919-
// *origin,
920-
// &module_graph,
921-
// &entry_module_runtime,
922-
// &module_cache,
923-
// &visited,
924-
// )
925-
// })
926-
// } else {
927-
// determine_runtime(
928-
// *origin,
929-
// &module_graph,
930-
// &entry_module_runtime,
931-
// &module_cache,
932-
// &visited,
933-
// )
934-
// };
935-
936-
// if runtime.is_none() {
937-
// dbg!(block.identifier());
938-
// panic!()
939-
// }
940-
// CreateChunkRoot::Block(*origin, *block_id, runtime)
941-
// })
942-
// .collect::<Vec<_>>(),
943-
// );
944-
945-
// for root in &roots {
946-
// if let Some(runtime) = root.get_runtime() {
947-
// self.module_deps.insert(runtime.clone(), Default::default());
948-
// }
949-
// }
950831

951832
let roots = self.analyze_module_graph(compilation)?;
833+
952834
// fill chunk with modules in parallel
953835
let chunks = roots
954836
.into_par_iter()

crates/rspack_core/src/compiler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ impl Compiler {
484484
Ok(())
485485
}
486486

487-
fn new_compilation_params(&self) -> CompilationParams {
487+
pub fn new_compilation_params(&self) -> CompilationParams {
488488
CompilationParams {
489489
normal_module_factory: Arc::new(NormalModuleFactory::new(
490490
self.options.clone(),

crates/rspack_core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use ustr::Ustr;
7575
pub use utils::*;
7676
mod chunk_graph;
7777
pub use chunk_graph::*;
78-
mod build_chunk_graph;
78+
pub mod build_chunk_graph;
7979
mod stats;
8080
pub use stats::*;
8181
mod runtime;

crates/rspack_plugin_split_chunks/src/plugin/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl SplitChunksPlugin {
4949
tracing::trace!("prepared module_group_map {:#?}", module_group_map);
5050
logger.time_end(start);
5151

52-
let start = logger.time("ensure min size fit");
52+
let start: rspack_core::StartTime = logger.time("ensure min size fit");
5353
self.ensure_min_size_fit(compilation, &mut module_group_map);
5454
logger.time_end(start);
5555

@@ -162,7 +162,7 @@ impl Debug for SplitChunksPlugin {
162162
}
163163

164164
#[plugin_hook(CompilationOptimizeChunks for SplitChunksPlugin, stage = Compilation::OPTIMIZE_CHUNKS_STAGE_ADVANCED)]
165-
fn optimize_chunks(&self, compilation: &mut Compilation) -> Result<Option<bool>> {
165+
pub fn optimize_chunks(&self, compilation: &mut Compilation) -> Result<Option<bool>> {
166166
self.inner_impl(compilation)?;
167167
Ok(None)
168168
}

tasks/benchmark/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ rspack = { workspace = true }
1717
rspack_fs = { workspace = true }
1818
rspack_core = { workspace = true }
1919
tokio = { workspace = true }
20+
serde_json = { workspace = true }
2021

2122
[[bench]]
22-
name = "basic"
23+
name = "benches"
2324
harness = false

tasks/benchmark/benches/basic.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
#![allow(clippy::unwrap_used)]
21
#![feature(trait_upcasting)]
2+
#![allow(unused_attributes)]
3+
#![allow(clippy::unwrap_used)]
34

45
use std::sync::Arc;
56

7+
use criterion::criterion_group;
68
use rspack::builder::{Builder as _, Devtool};
7-
use rspack_benchmark::{criterion_group, criterion_main, Criterion};
9+
use rspack_benchmark::Criterion;
810
use rspack_core::Compiler;
911
use rspack_fs::{MemoryFileSystem, ReadableFileSystem, WritableFileSystem};
1012
use tokio::runtime::Builder;
1113

1214
trait FileSystem: ReadableFileSystem + WritableFileSystem + Send + Sync {}
1315
impl<T: ReadableFileSystem + WritableFileSystem + Send + Sync> FileSystem for T {}
1416

15-
async fn basic(fs: Arc<dyn FileSystem>, sm: bool) {
17+
async fn basic_compile(fs: Arc<dyn FileSystem>, sm: bool) {
1618
let mut builder = Compiler::builder();
1719

1820
builder
@@ -36,7 +38,7 @@ async fn basic(fs: Arc<dyn FileSystem>, sm: bool) {
3638
.is_empty());
3739
}
3840

39-
pub fn criterion_benchmark(c: &mut Criterion) {
41+
pub fn basic_benchmark(c: &mut Criterion) {
4042
let rt = Builder::new_multi_thread().build().unwrap();
4143

4244
let fs = MemoryFileSystem::default();
@@ -59,17 +61,16 @@ pub fn criterion_benchmark(c: &mut Criterion) {
5961
c.bench_function("basic", |b| {
6062
b.to_async(&rt).iter(|| {
6163
let fs = fs.clone();
62-
basic(fs, false)
64+
basic_compile(fs, false)
6365
});
6466
});
6567

6668
c.bench_function("basic_sourcemap", |b| {
6769
b.to_async(&rt).iter(|| {
6870
let fs = fs.clone();
69-
basic(fs, true)
71+
basic_compile(fs, true)
7072
});
7173
});
7274
}
7375

74-
criterion_group!(benches, criterion_benchmark);
75-
criterion_main!(benches);
76+
criterion_group!(basic, basic_benchmark);

tasks/benchmark/benches/benches.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(trait_upcasting)]
2+
#![allow(clippy::unwrap_used)]
3+
4+
use basic::basic;
5+
use build_chunk_graph::chunk_graph;
6+
use criterion::criterion_main;
7+
8+
mod basic;
9+
mod build_chunk_graph;
10+
11+
criterion_main!(basic, chunk_graph);

0 commit comments

Comments
 (0)