Skip to content

refactor: prefetch exports info data of getters part 2 #10576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions crates/node_binding/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::ptr::NonNull;

use napi::Either;
use napi_derive::napi;
use rspack_core::{Compilation, ExportsInfo, ExportsInfoGetter, ModuleGraph, RuntimeSpec};
use rspack_core::{
Compilation, ExportsInfo, ExportsInfoGetter, ModuleGraph, PrefetchExportsInfoMode, RuntimeSpec,
};
use rspack_util::atom::Atom;

use crate::JsRuntimeSpec;

Expand Down Expand Up @@ -43,7 +46,12 @@ impl JsExportsInfo {
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
});
Ok(self.exports_info.is_used(&module_graph, runtime.as_ref()))
let exports_info = ExportsInfoGetter::prefetch(
&self.exports_info,
&module_graph,
PrefetchExportsInfoMode::AllExports,
);
Ok(ExportsInfoGetter::is_used(&exports_info, runtime.as_ref()))
}

#[napi(ts_args_type = "runtime: string | string[] | undefined")]
Expand All @@ -53,7 +61,11 @@ impl JsExportsInfo {
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
});
let exports_info = ExportsInfoGetter::prefetch(&self.exports_info, &module_graph, None);
let exports_info = ExportsInfoGetter::prefetch(
&self.exports_info,
&module_graph,
PrefetchExportsInfoMode::AllExports,
);
Ok(ExportsInfoGetter::is_module_used(
&exports_info,
runtime.as_ref(),
Expand Down Expand Up @@ -88,16 +100,16 @@ impl JsExportsInfo {
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
});
let used = match js_name {
Either::A(s) => self
.exports_info
.get_used(&module_graph, &[s.into()], runtime.as_ref()),
Either::B(v) => self.exports_info.get_used(
&module_graph,
v.into_iter().map(Into::into).collect::<Vec<_>>().as_slice(),
runtime.as_ref(),
),
let names = match js_name {
Either::A(s) => vec![Atom::from(s)],
Either::B(v) => v.into_iter().map(Into::into).collect::<Vec<_>>(),
};
let exports_info = ExportsInfoGetter::prefetch(
&self.exports_info,
&module_graph,
PrefetchExportsInfoMode::NamedNestedExports(&names),
);
let used = ExportsInfoGetter::get_used(&exports_info, &names, runtime.as_ref());
Ok(used as u32)
}
}
7 changes: 4 additions & 3 deletions crates/node_binding/src/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ptr::NonNull;

use napi::{Either, Env, JsString};
use napi_derive::napi;
use rspack_core::{Compilation, ModuleGraph, RuntimeSpec};
use rspack_core::{Compilation, ModuleGraph, PrefetchExportsInfoMode, RuntimeSpec};

use crate::{
DependencyObject, JsExportsInfo, ModuleGraphConnectionWrapper, ModuleObject, ModuleObjectRef,
Expand Down Expand Up @@ -89,8 +89,9 @@ impl JsModuleGraph {
runtime.extend(vec.iter().map(String::as_str).map(ustr::Ustr::from));
}
};
let used_exports =
module_graph.get_used_exports(&js_module.identifier, Some(&RuntimeSpec::new(runtime)));
let exports_info = module_graph
.get_prefetched_exports_info(&js_module.identifier, PrefetchExportsInfoMode::AllExports);
let used_exports = exports_info.get_used_exports(Some(&RuntimeSpec::new(runtime)));
Ok(match used_exports {
rspack_core::UsedExports::Unknown => None,
rspack_core::UsedExports::UsedNamespace(b) => Some(Either::A(b)),
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,7 +2218,7 @@ impl ConcatenatedModule {
ModuleInfo::Concatenated(info) => {
let export_id = export_name.first().cloned();
if matches!(
export_info.provided(mg),
export_info.provided(),
Some(crate::ExportProvided::NotProvided)
) {
needed_namespace_objects.insert(info.module);
Expand Down
22 changes: 12 additions & 10 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use swc_core::ecma::atoms::Atom;
use crate::{
compile_boolean_matcher_from_lists, contextify, property_access, to_comment, to_normal_comment,
AsyncDependenciesBlockIdentifier, ChunkGraph, Compilation, CompilerOptions, DependenciesBlock,
DependencyId, Environment, ExportsArgument, ExportsType, FakeNamespaceObjectMode,
InitFragmentExt, InitFragmentKey, InitFragmentStage, Module, ModuleGraph, ModuleId,
ModuleIdentifier, NormalInitFragment, PathInfo, RuntimeCondition, RuntimeGlobals, RuntimeSpec,
TemplateContext,
DependencyId, Environment, ExportsArgument, ExportsInfoGetter, ExportsType,
FakeNamespaceObjectMode, InitFragmentExt, InitFragmentKey, InitFragmentStage, Module,
ModuleGraph, ModuleId, ModuleIdentifier, NormalInitFragment, PathInfo, PrefetchExportsInfoMode,
RuntimeCondition, RuntimeGlobals, RuntimeSpec, TemplateContext,
};

pub fn runtime_condition_expression(
Expand Down Expand Up @@ -208,12 +208,14 @@ pub fn export_from_import(
.as_deref()
.unwrap_or(export_name);
if !export_name.is_empty() {
let exports_info = compilation
.get_module_graph()
.get_exports_info(&module_identifier);
let Some(used_name) =
exports_info.get_used_name(&compilation.get_module_graph(), *runtime, export_name)
else {
let Some(used_name) = ExportsInfoGetter::get_used_name(
&compilation.get_module_graph().get_prefetched_exports_info(
&module_identifier,
PrefetchExportsInfoMode::NamedNestedExports(export_name),
),
*runtime,
export_name,
) else {
return format!(
"{} undefined",
to_normal_comment(&property_access(export_name, 0))
Expand Down
69 changes: 36 additions & 33 deletions crates/rspack_core/src/exports/export_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ use serde::Serialize;

use super::{
ExportInfoGetter, ExportInfoTargetValue, ExportProvided, ExportsInfo, ExportsInfoData,
FindTargetRetEnum, FindTargetRetValue, ResolvedExportInfoTarget,
ExportsInfoGetter, FindTargetRetEnum, FindTargetRetValue, ResolvedExportInfoTarget,
ResolvedExportInfoTargetWithCircular, TerminalBinding, UnResolvedExportInfoTarget, UsageState,
NEXT_EXPORT_INFO_UKEY,
};
use crate::{Compilation, DependencyId, ModuleGraph, ModuleIdentifier, RuntimeSpec};
use crate::{
Compilation, DependencyId, ModuleGraph, ModuleIdentifier, PrefetchExportsInfoMode, RuntimeSpec,
};

#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
pub struct ExportInfo(Ukey);
Expand Down Expand Up @@ -88,13 +90,18 @@ impl ExportInfo {
return Some(TerminalBinding::ExportInfo(*self));
}
let target = info.get_target(mg)?;

let exports_info = mg.get_exports_info(&target.module);
let Some(export) = target.export else {
return Some(TerminalBinding::ExportsInfo(exports_info));
};
exports_info
.get_read_only_export_info_recursive(mg, &export)
.map(TerminalBinding::ExportInfo)
ExportsInfoGetter::prefetch(
&exports_info,
mg,
PrefetchExportsInfoMode::NamedNestedExports(&export),
)
.get_read_only_export_info_recursive(&export)
.map(|data| TerminalBinding::ExportInfo(data.id))
}

pub fn update_hash_with_visited(
Expand Down Expand Up @@ -234,11 +241,12 @@ impl ExportInfoData {
if valid_target_module_filter(&target.module) {
return FindTargetRetEnum::Value(target);
}
let exports_info = mg.get_exports_info(&target.module);
let export_info = exports_info.get_export_info_without_mut_module_graph(
mg,
&target.export.as_ref().expect("should have export")[0],
let name = &target.export.as_ref().expect("should have export")[0];
let exports_info = mg.get_prefetched_exports_info(
&target.module,
PrefetchExportsInfoMode::NamedExports(std::slice::from_ref(name)),
);
let export_info = exports_info.get_export_info_without_mut_module_graph(name);
let export_info_hash_key = export_info.as_hash_key();
if visited.contains(&export_info_hash_key) {
return FindTargetRetEnum::Undefined;
Expand Down Expand Up @@ -372,11 +380,11 @@ impl ExportInfoData {
// and the Static variant represents the most situation which FlagDependencyExportsPlugin can
// analyze the exports statically.
#[derive(Debug)]
pub enum MaybeDynamicTargetExportInfo {
Static(ExportInfo),
pub enum MaybeDynamicTargetExportInfo<'a> {
Static(&'a ExportInfoData),
Dynamic {
export_name: Atom,
other_export_info: ExportInfo,
other_export_info: &'a ExportInfoData,
data: ExportInfoData,
},
}
Expand All @@ -390,28 +398,26 @@ pub enum MaybeDynamicTargetExportInfoHashKey {
},
}

impl MaybeDynamicTargetExportInfo {
impl<'a> MaybeDynamicTargetExportInfo<'a> {
pub fn as_hash_key(&self) -> MaybeDynamicTargetExportInfoHashKey {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
MaybeDynamicTargetExportInfoHashKey::ExportInfo(*export_info)
MaybeDynamicTargetExportInfoHashKey::ExportInfo(export_info.id())
}
MaybeDynamicTargetExportInfo::Dynamic {
export_name,
other_export_info,
..
} => MaybeDynamicTargetExportInfoHashKey::TemporaryData {
export_name: export_name.clone(),
other_export_info: *other_export_info,
other_export_info: other_export_info.id(),
},
}
}

pub fn provided<'a>(&'a self, mg: &'a ModuleGraph) -> Option<&'a ExportProvided> {
pub fn provided(&'a self) -> Option<&'a ExportProvided> {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
ExportInfoGetter::provided(export_info.as_data(mg))
}
MaybeDynamicTargetExportInfo::Static(export_info) => ExportInfoGetter::provided(export_info),
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => data.provided.as_ref(),
}
}
Expand All @@ -432,8 +438,7 @@ impl MaybeDynamicTargetExportInfo {
) -> FindTargetRetEnum {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
let data = export_info.as_data(mg);
data.find_target_impl(mg, valid_target_module_filter, visited)
export_info.find_target_impl(mg, valid_target_module_filter, visited)
}
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => {
data.find_target_impl(mg, valid_target_module_filter, visited)
Expand Down Expand Up @@ -461,8 +466,7 @@ impl MaybeDynamicTargetExportInfo {
) -> Option<ResolvedExportInfoTargetWithCircular> {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
let export_info_data = export_info.as_data(mg);
export_info_data.get_target_proxy(mg, resolve_filter, already_visited)
export_info.get_target_proxy(mg, resolve_filter, already_visited)
}
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => {
if !data.target_is_set || data.target.is_empty() {
Expand All @@ -478,14 +482,10 @@ impl MaybeDynamicTargetExportInfo {
}
}

fn get_max_target<'a>(
&'a self,
mg: &'a ModuleGraph,
) -> Cow<'a, HashMap<Option<DependencyId>, ExportInfoTargetValue>> {
fn get_max_target(&self) -> Cow<HashMap<Option<DependencyId>, ExportInfoTargetValue>> {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => {
let data = export_info.as_data(mg);
ExportInfoGetter::get_max_target(data)
ExportInfoGetter::get_max_target(export_info)
}
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => ExportInfoGetter::get_max_target(data),
}
Expand All @@ -497,11 +497,11 @@ impl MaybeDynamicTargetExportInfo {
resolve_filter: ResolveFilterFnTy,
) -> Option<ResolvedExportInfoTarget> {
let data = match self {
MaybeDynamicTargetExportInfo::Static(export_info) => export_info.as_data(mg),
MaybeDynamicTargetExportInfo::Static(export_info) => *export_info,
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => data,
};
let target = data.get_target_with_filter(mg, resolve_filter)?;
let max_target = self.get_max_target(mg);
let max_target = self.get_max_target();
let original_target = max_target
.values()
.next()
Expand Down Expand Up @@ -567,8 +567,11 @@ fn resolve_target(
return Some(ResolvedExportInfoTargetWithCircular::Target(target));
};

let exports_info = mg.get_exports_info(&target.module);
let export_info = exports_info.get_export_info_without_mut_module_graph(mg, name);
let exports_info = mg.get_prefetched_exports_info(
&target.module,
PrefetchExportsInfoMode::NamedExports(std::slice::from_ref(name)),
);
let export_info = exports_info.get_export_info_without_mut_module_graph(name);
let export_info_hash_key = export_info.as_hash_key();
if already_visited.contains(&export_info_hash_key) {
return Some(ResolvedExportInfoTargetWithCircular::Circular);
Expand Down
10 changes: 5 additions & 5 deletions crates/rspack_core/src/exports/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,17 @@ impl ExportsInfo {
// An alternative version of `get_export_info`, and don't need `&mut ModuleGraph`.
// You can use this when you can't or don't want to use `&mut ModuleGraph`.
// Currently this function is used to finding a reexport's target.
pub fn get_export_info_without_mut_module_graph(
pub fn get_export_info_without_mut_module_graph<'a>(
&self,
mg: &ModuleGraph,
mg: &'a ModuleGraph,
name: &Atom,
) -> MaybeDynamicTargetExportInfo {
) -> MaybeDynamicTargetExportInfo<'a> {
let exports_info = self.as_exports_info(mg);
let redirect_id = exports_info.redirect_to;
let other_exports_info_id = exports_info.other_exports_info;
let export_info_id = exports_info.exports.get(name);
if let Some(export_info_id) = export_info_id {
return MaybeDynamicTargetExportInfo::Static(*export_info_id);
return MaybeDynamicTargetExportInfo::Static(export_info_id.as_data(mg));
}
if let Some(redirect_id) = redirect_id {
return redirect_id.get_export_info_without_mut_module_graph(mg, name);
Expand All @@ -333,7 +333,7 @@ impl ExportsInfo {
let data = ExportInfoData::new(Some(name.clone()), Some(other_export_info));
MaybeDynamicTargetExportInfo::Dynamic {
export_name: name.clone(),
other_export_info: other_exports_info_id,
other_export_info,
data,
}
}
Expand Down
Loading
Loading