Skip to content

Commit 61fd95a

Browse files
authored
perf: reduce memory allocation in esm_export_imported_specifier_dependency (#10657)
* Reduce memory allocation * Split ExportMode into variants * Box largest * Vec -> HashSet * Fix Clippy * &Option<_> to Option<&_>
1 parent 7c97b3d commit 61fd95a

File tree

5 files changed

+226
-232
lines changed

5 files changed

+226
-232
lines changed

crates/rspack_core/src/artifacts/module_graph_cache_artifact.rs

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -157,44 +157,68 @@ pub struct NormalReexportItem {
157157
pub export_info: ExportInfo,
158158
}
159159

160-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
161-
pub enum ExportModeType {
160+
#[derive(Debug, Clone)]
161+
pub enum ExportMode {
162162
Missing,
163-
Unused,
164-
EmptyStar,
165-
ReexportDynamicDefault,
166-
ReexportNamedDefault,
167-
ReexportNamespaceObject,
168-
ReexportFakeNamespaceObject,
169-
ReexportUndefined,
170-
NormalReexport,
171-
DynamicReexport,
163+
Unused(ExportModeUnused),
164+
EmptyStar(ExportModeEmptyStar),
165+
ReexportDynamicDefault(ExportModeReexportDynamicDefault),
166+
ReexportNamedDefault(ExportModeReexportNamedDefault),
167+
ReexportNamespaceObject(ExportModeReexportNamespaceObject),
168+
ReexportFakeNamespaceObject(ExportModeFakeNamespaceObject),
169+
ReexportUndefined(ExportModeReexportUndefined),
170+
NormalReexport(ExportModeNormalReexport),
171+
DynamicReexport(Box<ExportModeDynamicReexport>),
172172
}
173173

174174
#[derive(Debug, Clone)]
175-
pub struct ExportMode {
176-
/// corresponding to `type` field in webpack's `EpxortMode`
177-
pub ty: ExportModeType,
178-
pub items: Option<Vec<NormalReexportItem>>,
179-
pub name: Option<Atom>,
180-
pub fake_type: u8,
181-
pub partial_namespace_export_info: Option<ExportInfo>,
182-
pub ignored: Option<HashSet<Atom>>,
175+
pub struct ExportModeUnused {
176+
pub name: Atom,
177+
}
178+
179+
#[derive(Debug, Clone)]
180+
pub struct ExportModeEmptyStar {
183181
pub hidden: Option<HashSet<Atom>>,
184182
}
185183

186-
impl ExportMode {
187-
pub fn new(ty: ExportModeType) -> Self {
188-
Self {
189-
ty,
190-
items: None,
191-
name: None,
192-
fake_type: 0,
193-
partial_namespace_export_info: None,
194-
ignored: None,
195-
hidden: None,
196-
}
197-
}
184+
#[derive(Debug, Clone)]
185+
pub struct ExportModeReexportDynamicDefault {
186+
pub name: Atom,
187+
}
188+
189+
#[derive(Debug, Clone)]
190+
pub struct ExportModeReexportNamedDefault {
191+
pub name: Atom,
192+
pub partial_namespace_export_info: ExportInfo,
193+
}
194+
195+
#[derive(Debug, Clone)]
196+
pub struct ExportModeReexportNamespaceObject {
197+
pub name: Atom,
198+
pub partial_namespace_export_info: ExportInfo,
199+
}
200+
201+
#[derive(Debug, Clone)]
202+
pub struct ExportModeFakeNamespaceObject {
203+
pub name: Atom,
204+
pub fake_type: u8,
205+
pub partial_namespace_export_info: ExportInfo,
206+
}
207+
208+
#[derive(Debug, Clone)]
209+
pub struct ExportModeReexportUndefined {
210+
pub name: Atom,
211+
}
212+
213+
#[derive(Debug, Clone)]
214+
pub struct ExportModeNormalReexport {
215+
pub items: Vec<NormalReexportItem>,
216+
}
217+
218+
#[derive(Debug, Clone)]
219+
pub struct ExportModeDynamicReexport {
220+
pub ignored: HashSet<Atom>,
221+
pub hidden: Option<HashSet<Atom>>,
198222
}
199223

200224
#[derive(Debug, Default)]

crates/rspack_core/src/dependency/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub use runtime_requirements_dependency::{
3737
RuntimeRequirementsDependency, RuntimeRequirementsDependencyTemplate,
3838
};
3939
pub use runtime_template::*;
40-
use rustc_hash::FxHashMap;
40+
use rustc_hash::{FxHashMap, FxHashSet};
4141
use serde::Serialize;
4242
pub use span::SpanExt;
4343
pub use static_exports_dependency::{StaticExportsDependency, StaticExportsSpec};
@@ -106,8 +106,8 @@ pub struct ExportsSpec {
106106
pub terminal_binding: Option<bool>,
107107
pub from: Option<ModuleGraphConnection>,
108108
pub dependencies: Option<Vec<ModuleIdentifier>>,
109-
pub hide_export: Option<Vec<Atom>>,
110-
pub exclude_exports: Option<Vec<Atom>>,
109+
pub hide_export: Option<FxHashSet<Atom>>,
110+
pub exclude_exports: Option<FxHashSet<Atom>>,
111111
}
112112

113113
pub trait DependencyConditionFn: Sync + Send {

crates/rspack_core/src/exports/exports_info.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use either::Either;
44
use rspack_cacheable::cacheable;
55
use rspack_collections::{impl_item_ukey, Ukey, UkeySet};
66
use rspack_util::atom::Atom;
7+
use rustc_hash::FxHashSet;
78
use serde::Serialize;
89

910
use super::{
@@ -129,15 +130,15 @@ impl ExportsInfo {
129130
&self,
130131
mg: &mut ModuleGraph,
131132
can_mangle: bool,
132-
exclude_exports: Option<Vec<Atom>>,
133+
exclude_exports: Option<&FxHashSet<Atom>>,
133134
target_key: Option<DependencyId>,
134135
target_module: Option<DependencyId>,
135136
priority: Option<u8>,
136137
) -> bool {
137138
let mut changed = false;
138139

139140
if let Some(exclude_exports) = &exclude_exports {
140-
for name in exclude_exports {
141+
for name in exclude_exports.iter() {
141142
self.get_export_info(mg, name);
142143
}
143144
}

0 commit comments

Comments
 (0)