Skip to content

Commit 667e371

Browse files
committed
refactor: nested exports info data of getters part 2
1 parent 0fc2cf3 commit 667e371

33 files changed

+741
-703
lines changed

crates/node_binding/src/exports_info.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::ptr::NonNull;
33
use napi::Either;
44
use napi_derive::napi;
55
use rspack_core::{Compilation, ExportsInfo, ExportsInfoGetter, ModuleGraph, RuntimeSpec};
6+
use rspack_util::atom::Atom;
67

78
use crate::JsRuntimeSpec;
89

@@ -43,7 +44,8 @@ impl JsExportsInfo {
4344
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
4445
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
4546
});
46-
Ok(self.exports_info.is_used(&module_graph, runtime.as_ref()))
47+
let exports_info = ExportsInfoGetter::prefetch(&self.exports_info, &module_graph, None);
48+
Ok(ExportsInfoGetter::is_used(&exports_info, runtime.as_ref()))
4749
}
4850

4951
#[napi(ts_args_type = "runtime: string | string[] | undefined")]
@@ -88,16 +90,12 @@ impl JsExportsInfo {
8890
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
8991
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
9092
});
91-
let used = match js_name {
92-
Either::A(s) => self
93-
.exports_info
94-
.get_used(&module_graph, &[s.into()], runtime.as_ref()),
95-
Either::B(v) => self.exports_info.get_used(
96-
&module_graph,
97-
v.into_iter().map(Into::into).collect::<Vec<_>>().as_slice(),
98-
runtime.as_ref(),
99-
),
93+
let names = match js_name {
94+
Either::A(s) => vec![Atom::from(s)],
95+
Either::B(v) => v.into_iter().map(Into::into).collect::<Vec<_>>(),
10096
};
97+
let exports_info = ExportsInfoGetter::prefetch(&self.exports_info, &module_graph, Some(&names));
98+
let used = ExportsInfoGetter::get_used(&exports_info, &names, runtime.as_ref());
10199
Ok(used as u32)
102100
}
103101
}

crates/node_binding/src/module_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ impl JsModuleGraph {
8989
runtime.extend(vec.iter().map(String::as_str).map(ustr::Ustr::from));
9090
}
9191
};
92-
let used_exports =
93-
module_graph.get_used_exports(&js_module.identifier, Some(&RuntimeSpec::new(runtime)));
92+
let exports_info = module_graph.get_prefetched_exports_info(&js_module.identifier, None);
93+
let used_exports = exports_info.get_used_exports(Some(&RuntimeSpec::new(runtime)));
9494
Ok(match used_exports {
9595
rspack_core::UsedExports::Unknown => None,
9696
rspack_core::UsedExports::UsedNamespace(b) => Some(Either::A(b)),

crates/rspack_core/src/concatenated_module.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ use crate::{
4949
CodeGenerationPublicPathAutoReplace, CodeGenerationResult, Compilation, ConcatenatedModuleIdent,
5050
ConcatenationScope, ConditionalInitFragment, ConnectionState, Context, DependenciesBlock,
5151
DependencyId, DependencyType, ErrorSpan, ExportInfoGetter, ExportProvided, ExportsArgument,
52-
ExportsType, FactoryMeta, IdentCollector, InitFragment, InitFragmentStage, LibIdentOptions,
53-
MaybeDynamicTargetExportInfoHashKey, Module, ModuleArgument, ModuleGraph, ModuleGraphConnection,
54-
ModuleIdentifier, ModuleLayer, ModuleType, Resolve, RuntimeCondition, RuntimeGlobals,
55-
RuntimeSpec, SourceType, SpanExt, Template, UsageState, DEFAULT_EXPORT, NAMESPACE_OBJECT_EXPORT,
52+
ExportsInfoGetter, ExportsType, FactoryMeta, IdentCollector, InitFragment, InitFragmentStage,
53+
LibIdentOptions, MaybeDynamicTargetExportInfoHashKey, Module, ModuleArgument, ModuleGraph,
54+
ModuleGraphConnection, ModuleIdentifier, ModuleLayer, ModuleType, Resolve, RuntimeCondition,
55+
RuntimeGlobals, RuntimeSpec, SourceType, SpanExt, Template, UsageState, DEFAULT_EXPORT,
56+
NAMESPACE_OBJECT_EXPORT,
5657
};
5758

5859
type ExportsDefinitionArgs = Vec<(String, String)>;
@@ -2190,10 +2191,10 @@ impl ConcatenatedModule {
21902191
}
21912192
}
21922193

2193-
let exports_info = mg.get_exports_info(&info.id());
2194+
let exports_info = mg.get_prefetched_exports_info(&info.id(), Some(&export_name));
21942195
// webpack use `get_exports_info` here, https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad41535d/lib/optimize/ConcatenatedModule.js#L377-L377
21952196
// But in our arch, there is no way to modify module graph during code_generation phase, so we use `get_export_info_without_mut_module_graph` instead.`
2196-
let export_info = exports_info.get_export_info_without_mut_module_graph(mg, &export_name[0]);
2197+
let export_info = exports_info.get_export_info_without_mut_module_graph(&export_name[0]);
21972198
let export_info_hash_key = export_info.as_hash_key();
21982199

21992200
if already_visited.contains(&export_info_hash_key) {
@@ -2212,7 +2213,7 @@ impl ConcatenatedModule {
22122213
ModuleInfo::Concatenated(info) => {
22132214
let export_id = export_name.first().cloned();
22142215
if matches!(
2215-
export_info.provided(mg),
2216+
export_info.provided(),
22162217
Some(crate::ExportProvided::NotProvided)
22172218
) {
22182219
needed_namespace_objects.insert(info.module);
@@ -2231,7 +2232,9 @@ impl ConcatenatedModule {
22312232
if let Some(ref export_id) = export_id
22322233
&& let Some(direct_export) = info.export_map.as_ref().and_then(|map| map.get(export_id))
22332234
{
2234-
if let Some(used_name) = exports_info.get_used_name(mg, runtime, &export_name) {
2235+
if let Some(used_name) =
2236+
ExportsInfoGetter::get_used_name(&exports_info, runtime, &export_name)
2237+
{
22352238
// https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L402-L404
22362239
let used_name = used_name.to_used_name_vec();
22372240

@@ -2309,8 +2312,7 @@ impl ConcatenatedModule {
23092312

23102313
if info.namespace_export_symbol.is_some() {
23112314
// That's how webpack write https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L463-L471
2312-
let used_name = exports_info
2313-
.get_used_name(mg, runtime, &export_name)
2315+
let used_name = ExportsInfoGetter::get_used_name(&exports_info, runtime, &export_name)
23142316
.expect("should have export name");
23152317
let used_name = used_name.to_used_name_vec();
23162318
return Binding::Raw(RawBinding {
@@ -2334,7 +2336,9 @@ impl ConcatenatedModule {
23342336
);
23352337
}
23362338
ModuleInfo::External(info) => {
2337-
if let Some(used_name) = exports_info.get_used_name(mg, runtime, &export_name) {
2339+
if let Some(used_name) =
2340+
ExportsInfoGetter::get_used_name(&exports_info, runtime, &export_name)
2341+
{
23382342
let used_name = used_name.to_used_name_vec();
23392343
let comment = if used_name == export_name {
23402344
String::new()

crates/rspack_core/src/dependency/runtime_template.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use swc_core::ecma::atoms::Atom;
66
use crate::{
77
compile_boolean_matcher_from_lists, contextify, property_access, to_comment, to_normal_comment,
88
AsyncDependenciesBlockIdentifier, ChunkGraph, Compilation, CompilerOptions, DependenciesBlock,
9-
DependencyId, Environment, ExportsArgument, ExportsType, FakeNamespaceObjectMode,
10-
InitFragmentExt, InitFragmentKey, InitFragmentStage, Module, ModuleGraph, ModuleId,
11-
ModuleIdentifier, NormalInitFragment, PathInfo, RuntimeCondition, RuntimeGlobals, RuntimeSpec,
12-
TemplateContext,
9+
DependencyId, Environment, ExportsArgument, ExportsInfoGetter, ExportsType,
10+
FakeNamespaceObjectMode, InitFragmentExt, InitFragmentKey, InitFragmentStage, Module,
11+
ModuleGraph, ModuleId, ModuleIdentifier, NormalInitFragment, PathInfo, RuntimeCondition,
12+
RuntimeGlobals, RuntimeSpec, TemplateContext,
1313
};
1414

1515
pub fn runtime_condition_expression(
@@ -208,12 +208,13 @@ pub fn export_from_import(
208208
.as_deref()
209209
.unwrap_or(export_name);
210210
if !export_name.is_empty() {
211-
let exports_info = compilation
212-
.get_module_graph()
213-
.get_exports_info(&module_identifier);
214-
let Some(used_name) =
215-
exports_info.get_used_name(&compilation.get_module_graph(), *runtime, export_name)
216-
else {
211+
let Some(used_name) = ExportsInfoGetter::get_used_name(
212+
&compilation
213+
.get_module_graph()
214+
.get_prefetched_exports_info(&module_identifier, Some(export_name)),
215+
*runtime,
216+
export_name,
217+
) else {
217218
return format!(
218219
"{} undefined",
219220
to_normal_comment(&property_access(export_name, 0))

crates/rspack_core/src/exports/export_info.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use serde::Serialize;
1313

1414
use super::{
1515
ExportInfoGetter, ExportInfoTargetValue, ExportProvided, ExportsInfo, ExportsInfoData,
16-
FindTargetRetEnum, FindTargetRetValue, ResolvedExportInfoTarget,
16+
ExportsInfoGetter, FindTargetRetEnum, FindTargetRetValue, ResolvedExportInfoTarget,
1717
ResolvedExportInfoTargetWithCircular, TerminalBinding, UnResolvedExportInfoTarget, UsageState,
1818
NEXT_EXPORT_INFO_UKEY,
1919
};
@@ -88,13 +88,14 @@ impl ExportInfo {
8888
return Some(TerminalBinding::ExportInfo(*self));
8989
}
9090
let target = info.get_target(mg)?;
91+
9192
let exports_info = mg.get_exports_info(&target.module);
9293
let Some(export) = target.export else {
9394
return Some(TerminalBinding::ExportsInfo(exports_info));
9495
};
95-
exports_info
96-
.get_read_only_export_info_recursive(mg, &export)
97-
.map(TerminalBinding::ExportInfo)
96+
ExportsInfoGetter::prefetch(&exports_info, mg, Some(&export))
97+
.get_read_only_export_info_recursive(&export)
98+
.map(|data| TerminalBinding::ExportInfo(data.id))
9899
}
99100

100101
pub fn update_hash_with_visited(
@@ -234,9 +235,8 @@ impl ExportInfoData {
234235
if valid_target_module_filter(&target.module) {
235236
return FindTargetRetEnum::Value(target);
236237
}
237-
let exports_info = mg.get_exports_info(&target.module);
238+
let exports_info = mg.get_prefetched_exports_info(&target.module, None);
238239
let export_info = exports_info.get_export_info_without_mut_module_graph(
239-
mg,
240240
&target.export.as_ref().expect("should have export")[0],
241241
);
242242
let export_info_hash_key = export_info.as_hash_key();
@@ -372,11 +372,11 @@ impl ExportInfoData {
372372
// and the Static variant represents the most situation which FlagDependencyExportsPlugin can
373373
// analyze the exports statically.
374374
#[derive(Debug)]
375-
pub enum MaybeDynamicTargetExportInfo {
376-
Static(ExportInfo),
375+
pub enum MaybeDynamicTargetExportInfo<'a> {
376+
Static(&'a ExportInfoData),
377377
Dynamic {
378378
export_name: Atom,
379-
other_export_info: ExportInfo,
379+
other_export_info: &'a ExportInfoData,
380380
data: ExportInfoData,
381381
},
382382
}
@@ -390,28 +390,26 @@ pub enum MaybeDynamicTargetExportInfoHashKey {
390390
},
391391
}
392392

393-
impl MaybeDynamicTargetExportInfo {
393+
impl<'a> MaybeDynamicTargetExportInfo<'a> {
394394
pub fn as_hash_key(&self) -> MaybeDynamicTargetExportInfoHashKey {
395395
match self {
396396
MaybeDynamicTargetExportInfo::Static(export_info) => {
397-
MaybeDynamicTargetExportInfoHashKey::ExportInfo(*export_info)
397+
MaybeDynamicTargetExportInfoHashKey::ExportInfo(export_info.id())
398398
}
399399
MaybeDynamicTargetExportInfo::Dynamic {
400400
export_name,
401401
other_export_info,
402402
..
403403
} => MaybeDynamicTargetExportInfoHashKey::TemporaryData {
404404
export_name: export_name.clone(),
405-
other_export_info: *other_export_info,
405+
other_export_info: other_export_info.id(),
406406
},
407407
}
408408
}
409409

410-
pub fn provided<'a>(&'a self, mg: &'a ModuleGraph) -> Option<&'a ExportProvided> {
410+
pub fn provided(&'a self) -> Option<&'a ExportProvided> {
411411
match self {
412-
MaybeDynamicTargetExportInfo::Static(export_info) => {
413-
ExportInfoGetter::provided(export_info.as_data(mg))
414-
}
412+
MaybeDynamicTargetExportInfo::Static(export_info) => ExportInfoGetter::provided(export_info),
415413
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => data.provided.as_ref(),
416414
}
417415
}
@@ -432,8 +430,7 @@ impl MaybeDynamicTargetExportInfo {
432430
) -> FindTargetRetEnum {
433431
match self {
434432
MaybeDynamicTargetExportInfo::Static(export_info) => {
435-
let data = export_info.as_data(mg);
436-
data.find_target_impl(mg, valid_target_module_filter, visited)
433+
export_info.find_target_impl(mg, valid_target_module_filter, visited)
437434
}
438435
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => {
439436
data.find_target_impl(mg, valid_target_module_filter, visited)
@@ -461,8 +458,7 @@ impl MaybeDynamicTargetExportInfo {
461458
) -> Option<ResolvedExportInfoTargetWithCircular> {
462459
match self {
463460
MaybeDynamicTargetExportInfo::Static(export_info) => {
464-
let export_info_data = export_info.as_data(mg);
465-
export_info_data.get_target_proxy(mg, resolve_filter, already_visited)
461+
export_info.get_target_proxy(mg, resolve_filter, already_visited)
466462
}
467463
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => {
468464
if !data.target_is_set || data.target.is_empty() {
@@ -478,14 +474,10 @@ impl MaybeDynamicTargetExportInfo {
478474
}
479475
}
480476

481-
fn get_max_target<'a>(
482-
&'a self,
483-
mg: &'a ModuleGraph,
484-
) -> Cow<'a, HashMap<Option<DependencyId>, ExportInfoTargetValue>> {
477+
fn get_max_target(&self) -> Cow<HashMap<Option<DependencyId>, ExportInfoTargetValue>> {
485478
match self {
486479
MaybeDynamicTargetExportInfo::Static(export_info) => {
487-
let data = export_info.as_data(mg);
488-
ExportInfoGetter::get_max_target(data)
480+
ExportInfoGetter::get_max_target(export_info)
489481
}
490482
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => ExportInfoGetter::get_max_target(data),
491483
}
@@ -497,11 +489,11 @@ impl MaybeDynamicTargetExportInfo {
497489
resolve_filter: ResolveFilterFnTy,
498490
) -> Option<ResolvedExportInfoTarget> {
499491
let data = match self {
500-
MaybeDynamicTargetExportInfo::Static(export_info) => export_info.as_data(mg),
492+
MaybeDynamicTargetExportInfo::Static(export_info) => *export_info,
501493
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => data,
502494
};
503495
let target = data.get_target_with_filter(mg, resolve_filter)?;
504-
let max_target = self.get_max_target(mg);
496+
let max_target = self.get_max_target();
505497
let original_target = max_target
506498
.values()
507499
.next()
@@ -567,8 +559,8 @@ fn resolve_target(
567559
return Some(ResolvedExportInfoTargetWithCircular::Target(target));
568560
};
569561

570-
let exports_info = mg.get_exports_info(&target.module);
571-
let export_info = exports_info.get_export_info_without_mut_module_graph(mg, name);
562+
let exports_info = mg.get_prefetched_exports_info(&target.module, None);
563+
let export_info = exports_info.get_export_info_without_mut_module_graph(name);
572564
let export_info_hash_key = export_info.as_hash_key();
573565
if already_visited.contains(&export_info_hash_key) {
574566
return Some(ResolvedExportInfoTargetWithCircular::Circular);

0 commit comments

Comments
 (0)