Skip to content

Commit 6a921f7

Browse files
authored
perf: reduce ModuleGraphModule memory size (#4034)
1 parent 8dca426 commit 6a921f7

File tree

10 files changed

+27
-47
lines changed

10 files changed

+27
-47
lines changed

crates/rspack_core/src/build_chunk_graph/code_splitter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::{
1313

1414
pub(super) struct CodeSplitter<'me> {
1515
pub(super) compilation: &'me mut Compilation,
16-
next_free_module_pre_order_index: usize,
17-
next_free_module_post_order_index: usize,
16+
next_free_module_pre_order_index: u32,
17+
next_free_module_post_order_index: u32,
1818
queue: Vec<QueueItem>,
1919
queue_delayed: Vec<QueueItem>,
2020
split_point_modules: IdentifierSet,

crates/rspack_core/src/compiler/compilation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ impl Compilation {
700700
.module_graph
701701
.module_graph_module_by_identifier_mut(&module.identifier())
702702
.expect("Failed to get mgm");
703-
mgm.dependencies = dep_ids;
703+
mgm.dependencies = Box::new(dep_ids);
704704
if let Some(current_profile) = current_profile {
705705
mgm.set_profile(current_profile);
706706
}

crates/rspack_core/src/dependency/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod entry;
22
mod span;
3-
use std::sync::atomic::AtomicUsize;
3+
use std::sync::atomic::AtomicU32;
44
use std::sync::atomic::Ordering::Relaxed;
55

66
pub use entry::*;
@@ -357,9 +357,9 @@ pub fn is_async_dependency(dep: &dyn ModuleDependency) -> bool {
357357
}
358358

359359
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
360-
pub struct DependencyId(usize);
360+
pub struct DependencyId(u32);
361361

362-
pub static DEPENDENCY_ID: Lazy<AtomicUsize> = Lazy::new(|| AtomicUsize::new(0));
362+
pub static DEPENDENCY_ID: Lazy<AtomicU32> = Lazy::new(|| AtomicU32::new(0));
363363

364364
impl DependencyId {
365365
pub fn new() -> Self {
@@ -373,15 +373,15 @@ impl Default for DependencyId {
373373
}
374374

375375
impl std::ops::Deref for DependencyId {
376-
type Target = usize;
376+
type Target = u32;
377377

378378
fn deref(&self) -> &Self::Target {
379379
&self.0
380380
}
381381
}
382382

383-
impl From<usize> for DependencyId {
384-
fn from(id: usize) -> Self {
383+
impl From<u32> for DependencyId {
384+
fn from(id: u32) -> Self {
385385
Self(id)
386386
}
387387
}

crates/rspack_core/src/module_graph/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl ModuleGraph {
325325
removed
326326
}
327327

328-
pub fn get_pre_order_index(&self, module_identifier: &ModuleIdentifier) -> Option<usize> {
328+
pub fn get_pre_order_index(&self, module_identifier: &ModuleIdentifier) -> Option<u32> {
329329
self
330330
.module_graph_module_by_identifier(module_identifier)
331331
.and_then(|mgm| mgm.pre_order_index)

crates/rspack_core/src/module_graph_module.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ pub struct ModuleGraphModule {
2020
pub module_identifier: ModuleIdentifier,
2121
// TODO remove this since its included in module
2222
pub module_type: ModuleType,
23-
pub dependencies: Vec<DependencyId>,
24-
pub(crate) pre_order_index: Option<usize>,
25-
pub post_order_index: Option<usize>,
23+
pub dependencies: Box<Vec<DependencyId>>,
24+
pub(crate) pre_order_index: Option<u32>,
25+
pub post_order_index: Option<u32>,
2626
pub module_syntax: ModuleSyntax,
2727
pub factory_meta: Option<FactoryMeta>,
2828
pub build_info: Option<BuildInfo>,
2929
pub build_meta: Option<BuildMeta>,
30-
pub exports: ExportsInfo,
31-
pub profile: Option<ModuleProfile>,
30+
pub exports: Box<ExportsInfo>,
31+
pub profile: Option<Box<ModuleProfile>>,
3232
}
3333

3434
impl ModuleGraphModule {
@@ -48,7 +48,7 @@ impl ModuleGraphModule {
4848
factory_meta: None,
4949
build_info: None,
5050
build_meta: None,
51-
exports: ExportsInfo::new(),
51+
exports: Box::new(ExportsInfo::new()),
5252
profile: None,
5353
}
5454
}
@@ -175,11 +175,11 @@ impl ModuleGraphModule {
175175
}
176176

177177
pub fn set_profile(&mut self, profile: ModuleProfile) {
178-
self.profile = Some(profile);
178+
self.profile = Some(Box::new(profile));
179179
}
180180

181181
pub fn get_profile(&self) -> Option<&ModuleProfile> {
182-
self.profile.as_ref()
182+
self.profile.as_deref()
183183
}
184184

185185
pub fn set_issuer_if_unset(&mut self, issuer: Option<ModuleIdentifier>) {

crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_accept.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ pub struct ImportMetaHotAcceptDependency {
1010
request: JsWord,
1111
start: u32,
1212
end: u32,
13-
category: &'static DependencyCategory,
14-
dependency_type: &'static DependencyType,
15-
1613
span: Option<ErrorSpan>,
1714
}
1815

@@ -22,8 +19,6 @@ impl ImportMetaHotAcceptDependency {
2219
start,
2320
end,
2421
request,
25-
category: &DependencyCategory::Esm,
26-
dependency_type: &DependencyType::ImportMetaHotAccept,
2722
span,
2823
id: DependencyId::new(),
2924
}
@@ -36,11 +31,11 @@ impl Dependency for ImportMetaHotAcceptDependency {
3631
}
3732

3833
fn category(&self) -> &DependencyCategory {
39-
self.category
34+
&DependencyCategory::Esm
4035
}
4136

4237
fn dependency_type(&self) -> &DependencyType {
43-
self.dependency_type
38+
&DependencyType::ImportMetaHotAccept
4439
}
4540
}
4641

crates/rspack_plugin_javascript/src/dependency/hmr/import_meta_hot_decline.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ pub struct ImportMetaHotDeclineDependency {
1010
request: JsWord,
1111
start: u32,
1212
end: u32,
13-
category: &'static DependencyCategory,
14-
dependency_type: &'static DependencyType,
15-
1613
span: Option<ErrorSpan>,
1714
}
1815

@@ -22,8 +19,6 @@ impl ImportMetaHotDeclineDependency {
2219
start,
2320
end,
2421
request,
25-
category: &DependencyCategory::Esm,
26-
dependency_type: &DependencyType::ImportMetaHotDecline,
2722
span,
2823
id: DependencyId::new(),
2924
}
@@ -36,11 +31,11 @@ impl Dependency for ImportMetaHotDeclineDependency {
3631
}
3732

3833
fn category(&self) -> &DependencyCategory {
39-
self.category
34+
&DependencyCategory::Esm
4035
}
4136

4237
fn dependency_type(&self) -> &DependencyType {
43-
self.dependency_type
38+
&DependencyType::ImportMetaHotDecline
4439
}
4540
}
4641

crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_accept.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ pub struct ModuleHotAcceptDependency {
1010
request: JsWord,
1111
start: u32,
1212
end: u32,
13-
category: &'static DependencyCategory,
14-
dependency_type: &'static DependencyType,
15-
1613
span: Option<ErrorSpan>,
1714
}
1815

@@ -21,8 +18,6 @@ impl ModuleHotAcceptDependency {
2118
Self {
2219
id: DependencyId::new(),
2320
request,
24-
category: &DependencyCategory::CommonJS,
25-
dependency_type: &DependencyType::ModuleHotAccept,
2621
span,
2722
start,
2823
end,
@@ -36,11 +31,11 @@ impl Dependency for ModuleHotAcceptDependency {
3631
}
3732

3833
fn category(&self) -> &DependencyCategory {
39-
self.category
34+
&DependencyCategory::CommonJS
4035
}
4136

4237
fn dependency_type(&self) -> &DependencyType {
43-
self.dependency_type
38+
&DependencyType::ModuleHotAccept
4439
}
4540
}
4641

crates/rspack_plugin_javascript/src/dependency/hmr/module_hot_decline.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ pub struct ModuleHotDeclineDependency {
1010
request: JsWord,
1111
start: u32,
1212
end: u32,
13-
category: &'static DependencyCategory,
14-
dependency_type: &'static DependencyType,
15-
1613
span: Option<ErrorSpan>,
1714
}
1815

@@ -21,8 +18,6 @@ impl ModuleHotDeclineDependency {
2118
Self {
2219
id: DependencyId::new(),
2320
request,
24-
category: &DependencyCategory::CommonJS,
25-
dependency_type: &DependencyType::ModuleHotDecline,
2621
span,
2722
start,
2823
end,
@@ -36,11 +31,11 @@ impl Dependency for ModuleHotDeclineDependency {
3631
}
3732

3833
fn category(&self) -> &DependencyCategory {
39-
self.category
34+
&DependencyCategory::CommonJS
4035
}
4136

4237
fn dependency_type(&self) -> &DependencyType {
43-
self.dependency_type
38+
&DependencyType::ModuleHotDecline
4439
}
4540
}
4641

crates/rspack_util/src/comparators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn compare_ids(a: &str, b: &str) -> Ordering {
1313
}
1414
}
1515
#[allow(clippy::comparison_chain)]
16-
pub fn compare_numbers(a: usize, b: usize) -> Ordering {
16+
pub fn compare_numbers(a: u32, b: u32) -> Ordering {
1717
if a < b {
1818
Ordering::Less
1919
} else if a > b {

0 commit comments

Comments
 (0)