Skip to content

Commit aa63c5a

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

File tree

15 files changed

+473
-124
lines changed

15 files changed

+473
-124
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2212,7 +2212,7 @@ impl ConcatenatedModule {
22122212
ModuleInfo::Concatenated(info) => {
22132213
let export_id = export_name.first().cloned();
22142214
if matches!(
2215-
export_info.provided(mg),
2215+
export_info.provided(),
22162216
Some(crate::ExportProvided::NotProvided)
22172217
) {
22182218
needed_namespace_objects.insert(info.module);

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);

crates/rspack_core/src/exports/exports_info.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,17 @@ impl ExportsInfo {
313313
// An alternative version of `get_export_info`, and don't need `&mut ModuleGraph`.
314314
// You can use this when you can't or don't want to use `&mut ModuleGraph`.
315315
// Currently this function is used to finding a reexport's target.
316-
pub fn get_export_info_without_mut_module_graph(
316+
pub fn get_export_info_without_mut_module_graph<'a>(
317317
&self,
318-
mg: &ModuleGraph,
318+
mg: &'a ModuleGraph,
319319
name: &Atom,
320-
) -> MaybeDynamicTargetExportInfo {
320+
) -> MaybeDynamicTargetExportInfo<'a> {
321321
let exports_info = self.as_exports_info(mg);
322322
let redirect_id = exports_info.redirect_to;
323323
let other_exports_info_id = exports_info.other_exports_info;
324324
let export_info_id = exports_info.exports.get(name);
325325
if let Some(export_info_id) = export_info_id {
326-
return MaybeDynamicTargetExportInfo::Static(*export_info_id);
326+
return MaybeDynamicTargetExportInfo::Static(export_info_id.as_data(mg));
327327
}
328328
if let Some(redirect_id) = redirect_id {
329329
return redirect_id.get_export_info_without_mut_module_graph(mg, name);
@@ -333,7 +333,7 @@ impl ExportsInfo {
333333
let data = ExportInfoData::new(Some(name.clone()), Some(other_export_info));
334334
MaybeDynamicTargetExportInfo::Dynamic {
335335
export_name: name.clone(),
336-
other_export_info: other_exports_info_id,
336+
other_export_info,
337337
data,
338338
}
339339
}

0 commit comments

Comments
 (0)