Skip to content

Commit 25274d8

Browse files
authored
refactor(core): improve referenced export data struct (#14796)
1 parent 23812cf commit 25274d8

33 files changed

Lines changed: 307 additions & 298 deletions

crates/rspack_core/src/artifacts/exports_info_artifact.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ impl ArtifactExt for ExportsInfoArtifact {
2525
}
2626

2727
impl ExportsInfoArtifact {
28+
pub fn len(&self) -> usize {
29+
self.exports_info_map.len()
30+
}
31+
32+
pub fn is_empty(&self) -> bool {
33+
self.exports_info_map.is_empty()
34+
}
35+
2836
pub fn new_exports_info(&mut self, module_identifier: ModuleIdentifier) {
2937
let info = ExportsInfoData::default();
3038
let id = info.id();

crates/rspack_core/src/compilation/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ use crate::{
7979
CompilationLogger, CompilationLogging, CompilerOptions, CompilerPlatform, ConcatenationScope,
8080
DependenciesDiagnosticsArtifact, DependencyId, DependencyTemplate, DependencyTemplateType,
8181
DependencyType, Entry, EntryData, EntryOptions, EntryRuntime, Entrypoint, ExecuteModuleId,
82-
ExportsInfoArtifact, ExtendedReferencedExport, Filename, ImportPhase, ImportVarMap,
83-
ImportedByDeferModulesArtifact, MemoryGCStorage, ModuleFactory, ModuleGraph,
84-
ModuleGraphCacheArtifact, ModuleIdentifier, ModuleIdsArtifact, ModuleStaticCache, PathData,
85-
ProcessRuntimeRequirementsCacheArtifact, ResolverFactory, RuntimeGlobals, RuntimeKeyMap,
86-
RuntimeMode, RuntimeModule, RuntimeProxyMetadataArtifact, RuntimeSpec, RuntimeSpecMap,
87-
RuntimeTemplate, SharedPluginDriver, SideEffectsOptimizeArtifact, SideEffectsStateArtifact,
88-
SourceType, Stats, StatsContext, StealCell, ValueCacheVersions,
82+
ExportsInfoArtifact, Filename, ImportPhase, ImportVarMap, ImportedByDeferModulesArtifact,
83+
MemoryGCStorage, ModuleFactory, ModuleGraph, ModuleGraphCacheArtifact, ModuleIdentifier,
84+
ModuleIdsArtifact, ModuleStaticCache, PathData, ProcessRuntimeRequirementsCacheArtifact,
85+
ReferencedExport, ResolverFactory, RuntimeGlobals, RuntimeKeyMap, RuntimeMode, RuntimeModule,
86+
RuntimeProxyMetadataArtifact, RuntimeSpec, RuntimeSpecMap, RuntimeTemplate, SharedPluginDriver,
87+
SideEffectsOptimizeArtifact, SideEffectsStateArtifact, SourceType, Stats, StatsContext,
88+
StealCell, ValueCacheVersions,
8989
cache::persistent::occasion::{
9090
devtool::SourceMapDevToolPluginCacheArtifact, minimize::MinimizePersistentCacheArtifact,
9191
},
@@ -110,7 +110,7 @@ define_hook!(CompilationSeal: Series(compilation: &Compilation, diagnostics: &mu
110110
define_hook!(CompilationDependencyReferencedExports: Sync(
111111
compilation: &Compilation,
112112
dependency: &DependencyId,
113-
referenced_exports: &Option<Vec<ExtendedReferencedExport>>,
113+
referenced_exports: &Option<Vec<ReferencedExport>>,
114114
runtime: Option<&RuntimeSpec>,
115115
module_graph: Option<&ModuleGraph>
116116
));

crates/rspack_core/src/dependency/context_element_dependency.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use super::{AffectType, FactorizeInfo};
88
use crate::{
99
AsContextDependency, AsDependencyCodeGeneration, Context, ContextMode, ContextNameSpaceObject,
1010
ContextOptions, Dependency, DependencyCategory, DependencyId, DependencyType,
11-
ExportsInfoArtifact, ExtendedReferencedExport, ImportAttributes, ModuleDependency, ModuleGraph,
12-
ModuleGraphCacheArtifact, ModuleLayer, ReferencedSpecifier, ResourceIdentifier, RuntimeSpec,
11+
ExportsInfoArtifact, ImportAttributes, ModuleDependency, ModuleGraph, ModuleGraphCacheArtifact,
12+
ModuleLayer, ReferencedExport, ReferencedSpecifier, ResourceIdentifier, RuntimeSpec,
1313
create_exports_object_referenced, create_referenced_exports_by_referenced_specifiers,
1414
};
1515

@@ -82,7 +82,7 @@ impl Dependency for ContextElementDependency {
8282
module_graph_cache: &ModuleGraphCacheArtifact,
8383
exports_info_artifact: &ExportsInfoArtifact,
8484
_runtime: Option<&RuntimeSpec>,
85-
) -> Vec<ExtendedReferencedExport> {
85+
) -> Vec<ReferencedExport> {
8686
if let Some(referenced_specifiers) = &self.referenced_specifiers {
8787
let Some(parent_module) = module_graph
8888
.get_parent_module(&self.id)

crates/rspack_core/src/dependency/dependency_trait.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use super::{
1212
dependency_template::AsDependencyCodeGeneration, module_dependency::*,
1313
};
1414
use crate::{
15-
AsContextDependency, ConnectionState, Context, ExportsInfoArtifact, ExtendedReferencedExport,
16-
ForwardId, ImportAttributes, ImportPhase, LazyUntil, ModuleGraph, ModuleGraphCacheArtifact,
17-
ModuleLayer, RuntimeSpec, SideEffectsStateArtifact, create_exports_object_referenced,
15+
AsContextDependency, ConnectionState, Context, ExportsInfoArtifact, ForwardId, ImportAttributes,
16+
ImportPhase, LazyUntil, ModuleGraph, ModuleGraphCacheArtifact, ModuleLayer, ReferencedExport,
17+
RuntimeSpec, SideEffectsStateArtifact, create_exports_object_referenced,
1818
};
1919

2020
#[derive(Debug, Clone, Copy)]
@@ -114,7 +114,7 @@ pub trait Dependency:
114114
_module_graph_cache: &ModuleGraphCacheArtifact,
115115
_exports_info_artifact: &ExportsInfoArtifact,
116116
_runtime: Option<&RuntimeSpec>,
117-
) -> Vec<ExtendedReferencedExport> {
117+
) -> Vec<ReferencedExport> {
118118
create_exports_object_referenced()
119119
}
120120

crates/rspack_core/src/dependency/mod.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,11 @@ pub use static_exports_dependency::{StaticExportsDependency, StaticExportsSpec};
4747
use swc_core::ecma::atoms::Atom;
4848

4949
use crate::{
50-
ConnectionState, EvaluatedInlinableValue, ExportsInfoArtifact, ExportsType,
51-
ExtendedReferencedExport, ModuleGraph, ModuleGraphCacheArtifact, ModuleGraphConnection,
52-
ModuleIdentifier, ReferencedExport, RuntimeSpec, SideEffectsStateArtifact,
53-
create_exports_object_referenced,
50+
ConnectionState, EvaluatedInlinableValue, ExportsInfoArtifact, ExportsType, ModuleGraph,
51+
ModuleGraphCacheArtifact, ModuleGraphConnection, ModuleIdentifier, ReferencedExport, RuntimeSpec,
52+
SideEffectsStateArtifact, create_exports_object_referenced,
5453
};
5554

56-
#[derive(Debug, Clone)]
57-
pub enum ProcessModuleReferencedExports {
58-
Map(FxHashMap<String, ExtendedReferencedExport>),
59-
ExtendRef(Vec<ExtendedReferencedExport>),
60-
}
61-
6255
#[derive(Debug, Default)]
6356
pub struct ExportSpec {
6457
pub name: Atom,
@@ -312,7 +305,7 @@ pub fn create_referenced_exports_by_referenced_specifiers(
312305
referenced_specifiers: &[ReferencedSpecifier],
313306
exports_type: ExportsType,
314307
is_json: bool,
315-
) -> Vec<ExtendedReferencedExport> {
308+
) -> Vec<ReferencedExport> {
316309
let mut refs = vec![];
317310
for ReferencedSpecifier {
318311
names,
@@ -357,11 +350,11 @@ pub fn create_referenced_exports_by_referenced_specifiers(
357350
// remove last one
358351
names = &names[..names.len().saturating_sub(1)];
359352
}
360-
refs.push(ExtendedReferencedExport::Export(ReferencedExport::new(
361-
names.to_vec(),
362-
false,
363-
false,
364-
)));
353+
refs.push(
354+
ReferencedExport::from(names)
355+
.with_can_mangle(false)
356+
.with_can_inline(false),
357+
);
365358
}
366359
refs
367360
}

crates/rspack_core/src/exports/referenced_export.rs

Lines changed: 116 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,150 @@
1+
use bitflags::bitflags;
12
use rspack_util::atom::Atom;
23
use rustc_hash::FxHashSet;
4+
use smallvec::SmallVec;
35

46
use crate::{ExportInfo, ExportInfoData, ExportsInfoArtifact, RuntimeSpec, UsageState};
57

6-
/// refer https://github.com/webpack/webpack/blob/d15c73469fd71cf98734685225250148b68ddc79/lib/FlagDependencyUsagePlugin.js#L64
7-
#[derive(Clone, Debug)]
8-
pub enum ExtendedReferencedExport {
9-
Array(Vec<Atom>),
10-
Export(ReferencedExport),
8+
pub type ReferencedExportPath = SmallVec<[Atom; 2]>;
9+
10+
bitflags! {
11+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12+
pub struct ReferencedExportFlags: u8 {
13+
const CAN_MANGLE = 1 << 0;
14+
const CAN_INLINE = 1 << 1;
15+
const NS_ACCESS = 1 << 2;
16+
}
17+
}
18+
19+
impl Default for ReferencedExportFlags {
20+
fn default() -> Self {
21+
Self::CAN_MANGLE | Self::CAN_INLINE
22+
}
23+
}
24+
25+
impl ReferencedExportFlags {
26+
#[inline]
27+
pub fn merge(&mut self, other: Self) {
28+
self.set(
29+
Self::CAN_MANGLE,
30+
self.contains(Self::CAN_MANGLE) && other.contains(Self::CAN_MANGLE),
31+
);
32+
self.set(
33+
Self::CAN_INLINE,
34+
self.contains(Self::CAN_INLINE) && other.contains(Self::CAN_INLINE),
35+
);
36+
self.set(
37+
Self::NS_ACCESS,
38+
self.contains(Self::NS_ACCESS) || other.contains(Self::NS_ACCESS),
39+
);
40+
}
41+
}
42+
43+
#[derive(Clone, Debug, Default)]
44+
pub struct ReferencedExport {
45+
pub name: ReferencedExportPath,
46+
pub flags: ReferencedExportFlags,
1147
}
1248

13-
pub fn is_no_exports_referenced(exports: &[ExtendedReferencedExport]) -> bool {
49+
pub fn is_no_exports_referenced(exports: &[ReferencedExport]) -> bool {
1450
exports.is_empty()
1551
}
1652

17-
pub fn is_exports_object_referenced(exports: &[ExtendedReferencedExport]) -> bool {
18-
matches!(exports[..], [ExtendedReferencedExport::Array(ref arr)] if arr.is_empty())
53+
pub fn is_exports_object_referenced(exports: &[ReferencedExport]) -> bool {
54+
matches!(exports, [export] if export.name.is_empty())
1955
}
2056

21-
pub fn create_no_exports_referenced() -> Vec<ExtendedReferencedExport> {
57+
pub fn create_no_exports_referenced() -> Vec<ReferencedExport> {
2258
vec![]
2359
}
2460

25-
pub fn create_exports_object_referenced() -> Vec<ExtendedReferencedExport> {
26-
vec![ExtendedReferencedExport::Array(vec![])]
61+
pub fn create_exports_object_referenced() -> Vec<ReferencedExport> {
62+
vec![ReferencedExport::default()]
2763
}
2864

29-
impl From<Vec<Atom>> for ExtendedReferencedExport {
65+
impl From<Vec<Atom>> for ReferencedExport {
66+
#[inline]
3067
fn from(value: Vec<Atom>) -> Self {
31-
ExtendedReferencedExport::Array(value)
68+
Self::from(ReferencedExportPath::from_vec(value))
3269
}
3370
}
34-
impl From<ReferencedExport> for ExtendedReferencedExport {
35-
fn from(value: ReferencedExport) -> Self {
36-
ExtendedReferencedExport::Export(value)
71+
72+
impl From<Vec<&Atom>> for ReferencedExport {
73+
#[inline]
74+
fn from(value: Vec<&Atom>) -> Self {
75+
Self::from(value.into_iter().cloned().collect::<ReferencedExportPath>())
3776
}
3877
}
3978

40-
#[derive(Clone, Debug)]
41-
pub struct ReferencedExport {
42-
pub name: Vec<Atom>,
43-
pub can_mangle: bool,
44-
pub can_inline: bool,
45-
pub ns_access: bool,
79+
impl From<&[Atom]> for ReferencedExport {
80+
#[inline]
81+
fn from(value: &[Atom]) -> Self {
82+
Self::from(ReferencedExportPath::from(value))
83+
}
4684
}
4785

48-
impl ReferencedExport {
49-
pub fn new(name: Vec<Atom>, can_mangle: bool, can_inline: bool) -> Self {
86+
impl From<ReferencedExportPath> for ReferencedExport {
87+
#[inline]
88+
fn from(name: ReferencedExportPath) -> Self {
5089
Self {
5190
name,
52-
can_mangle,
53-
can_inline,
54-
ns_access: false,
91+
flags: ReferencedExportFlags::default(),
5592
}
5693
}
5794
}
5895

59-
impl Default for ReferencedExport {
60-
fn default() -> Self {
61-
Self {
62-
name: vec![],
63-
can_mangle: true,
64-
can_inline: true,
65-
ns_access: false,
66-
}
96+
impl From<Atom> for ReferencedExport {
97+
#[inline]
98+
fn from(value: Atom) -> Self {
99+
let mut path = ReferencedExportPath::new();
100+
path.push(value);
101+
Self::from(path)
102+
}
103+
}
104+
105+
impl From<&Atom> for ReferencedExport {
106+
#[inline]
107+
fn from(value: &Atom) -> Self {
108+
Self::from(value.clone())
109+
}
110+
}
111+
112+
impl ReferencedExport {
113+
#[inline]
114+
pub fn with_can_mangle(mut self, can_mangle: bool) -> Self {
115+
self
116+
.flags
117+
.set(ReferencedExportFlags::CAN_MANGLE, can_mangle);
118+
self
119+
}
120+
121+
#[inline]
122+
pub fn with_can_inline(mut self, can_inline: bool) -> Self {
123+
self
124+
.flags
125+
.set(ReferencedExportFlags::CAN_INLINE, can_inline);
126+
self
127+
}
128+
129+
#[inline]
130+
pub fn with_ns_access(mut self, ns_access: bool) -> Self {
131+
self.flags.set(ReferencedExportFlags::NS_ACCESS, ns_access);
132+
self
133+
}
134+
135+
#[inline]
136+
pub fn can_mangle(&self) -> bool {
137+
self.flags.contains(ReferencedExportFlags::CAN_MANGLE)
138+
}
139+
140+
#[inline]
141+
pub fn can_inline(&self) -> bool {
142+
self.flags.contains(ReferencedExportFlags::CAN_INLINE)
143+
}
144+
145+
#[inline]
146+
pub fn ns_access(&self) -> bool {
147+
self.flags.contains(ReferencedExportFlags::NS_ACCESS)
67148
}
68149
}
69150

crates/rspack_plugin_css/src/dependency/compose.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rspack_cacheable::{
44
};
55
use rspack_core::{
66
AsContextDependency, AsDependencyCodeGeneration, CssExportType, Dependency, DependencyCategory,
7-
DependencyId, DependencyRange, DependencyType, ExportsInfoArtifact, ExtendedReferencedExport,
8-
FactorizeInfo, ModuleDependency, RuntimeSpec,
7+
DependencyId, DependencyRange, DependencyType, ExportsInfoArtifact, FactorizeInfo,
8+
ModuleDependency, ReferencedExport, RuntimeSpec,
99
};
1010
use rspack_util::atom::Atom;
1111

@@ -81,12 +81,8 @@ impl Dependency for CssComposeDependency {
8181
_module_graph_cache: &rspack_core::ModuleGraphCacheArtifact,
8282
_exports_info_artifact: &ExportsInfoArtifact,
8383
_runtime: Option<&RuntimeSpec>,
84-
) -> Vec<ExtendedReferencedExport> {
85-
self
86-
.names
87-
.iter()
88-
.map(|n| ExtendedReferencedExport::Array(vec![n.clone()]))
89-
.collect()
84+
) -> Vec<ReferencedExport> {
85+
self.names.iter().map(ReferencedExport::from).collect()
9086
}
9187
}
9288

crates/rspack_plugin_css/src/dependency/self_reference.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rspack_cacheable::{cacheable, cacheable_dyn};
22
use rspack_core::{
33
AsContextDependency, Dependency, DependencyCategory, DependencyCodeGeneration, DependencyId,
44
DependencyRange, DependencyTemplate, DependencyTemplateType, DependencyType, ExportsInfoArtifact,
5-
ExtendedReferencedExport, FactorizeInfo, ModuleDependency, RuntimeSpec, TemplateContext,
5+
FactorizeInfo, ModuleDependency, ReferencedExport, RuntimeSpec, TemplateContext,
66
TemplateReplaceSource,
77
};
88
use rspack_util::atom::Atom;
@@ -64,11 +64,11 @@ impl Dependency for CssSelfReferenceLocalIdentDependency {
6464
_module_graph_cache: &rspack_core::ModuleGraphCacheArtifact,
6565
_exports_info_artifact: &ExportsInfoArtifact,
6666
_runtime: Option<&RuntimeSpec>,
67-
) -> Vec<ExtendedReferencedExport> {
67+
) -> Vec<ReferencedExport> {
6868
self
6969
.names
7070
.iter()
71-
.map(|n| ExtendedReferencedExport::Array(vec![Atom::from(n.as_str())]))
71+
.map(|n| ReferencedExport::from(Atom::from(n.as_str())))
7272
.collect()
7373
}
7474
}

0 commit comments

Comments
 (0)