Skip to content

Commit

Permalink
perf: parallelize side effects optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Dec 19, 2024
1 parent 0e749f0 commit 1f491cf
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 205 deletions.
101 changes: 64 additions & 37 deletions crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,43 +1307,6 @@ impl ExportInfo {
false
}

pub fn move_target(
&self,
mg: &mut ModuleGraph,
resolve_filter: ResolveFilterFnTy,
update_original_connection: UpdateOriginalFunctionTy,
) -> Option<ResolvedExportInfoTarget> {
let target = self.get_target_with_filter(mg, resolve_filter)?;
let max_target = self.get_max_target(mg);
let original_target = max_target
.values()
.next()
.expect("should have export info target"); // refer https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad41535d/lib/ExportsInfo.js#L1388-L1394
if original_target.dependency.as_ref() == Some(&target.dependency)
&& original_target.export == target.export
{
return None;
}
let export_info_mut = self.as_export_info_mut(mg);
export_info_mut.target.clear();
let updated_dependency_id = update_original_connection(&target, mg);

// shadowning `export_info_mut` to reduce `&mut ModuleGraph` borrow life time, since
// `update_original_connection` also needs `&mut ModuleGraph`
let export_info_mut = self.as_export_info_mut(mg);
export_info_mut.target.insert(
None,
ExportInfoTargetValue {
dependency: updated_dependency_id,
export: target.export.clone(),
priority: 0,
},
);

export_info_mut.target_is_set = true;
Some(target)
}

pub fn set_used_conditionally(
&self,
mg: &mut ModuleGraph,
Expand Down Expand Up @@ -1871,6 +1834,18 @@ impl MaybeDynamicTargetExportInfo {
}
}

pub fn get_target_with_filter(
&self,
mg: &ModuleGraph,
resolve_filter: ResolveFilterFnTy,
) -> Option<ResolvedExportInfoTarget> {
match self.get_target_impl(mg, resolve_filter, &mut Default::default()) {
Some(ResolvedExportInfoTargetWithCircular::Circular) => None,
Some(ResolvedExportInfoTargetWithCircular::Target(target)) => Some(target),
None => None,
}
}

fn get_target_impl(
&self,
mg: &ModuleGraph,
Expand All @@ -1894,6 +1869,58 @@ impl MaybeDynamicTargetExportInfo {
}
}
}

fn get_max_target<'a>(
&'a self,
mg: &'a ModuleGraph,
) -> Cow<'a, HashMap<Option<DependencyId>, ExportInfoTargetValue>> {
match self {
MaybeDynamicTargetExportInfo::Static(export_info) => export_info.get_max_target(mg),
MaybeDynamicTargetExportInfo::Dynamic { data, .. } => data.get_max_target(),
}
}
}

impl MaybeDynamicTargetExportInfo {
pub fn can_move_target(
&self,
mg: &ModuleGraph,
resolve_filter: ResolveFilterFnTy,
) -> Option<ResolvedExportInfoTarget> {
let target = self.get_target_with_filter(mg, resolve_filter)?;
let max_target = self.get_max_target(mg);
let original_target = max_target
.values()
.next()
.expect("should have export info target"); // refer https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad41535d/lib/ExportsInfo.js#L1388-L1394
if original_target.dependency.as_ref() == Some(&target.dependency)
&& original_target.export == target.export
{
return None;
}
Some(target)
}
}

impl ExportInfo {
pub fn do_move_target(
&self,
mg: &mut ModuleGraph,
dependency: DependencyId,
target_export: Option<Vec<Atom>>,
) {
let export_info_mut = self.as_export_info_mut(mg);
export_info_mut.target.clear();
export_info_mut.target.insert(
None,
ExportInfoTargetValue {
dependency: Some(dependency),
export: target_export,
priority: 0,
},
);
export_info_mut.target_is_set = true;
}
}

pub type ResolveFilterFnTy<'a> = Rc<dyn Fn(&ResolvedExportInfoTarget, &ModuleGraph) -> bool + 'a>;
Expand Down
15 changes: 9 additions & 6 deletions crates/rspack_core/src/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,16 +937,20 @@ impl<'a> ModuleGraph<'a> {
.insert(dep_id, DependencyExtraMeta { ids });
}

pub fn update_module(&mut self, dep_id: &DependencyId, module_id: &ModuleIdentifier) -> bool {
pub fn can_update_module(&self, dep_id: &DependencyId, module_id: &ModuleIdentifier) -> bool {
let connection = self
.connection_by_dependency_id(dep_id)
.expect("should have connection");
connection.module_identifier() != module_id
}

pub fn do_update_module(&mut self, dep_id: &DependencyId, module_id: &ModuleIdentifier) {
let connection = self
.connection_by_dependency_id_mut(dep_id)
.expect("should have connection");
let old_module_identifier = *connection.module_identifier();
if &old_module_identifier == module_id {
return false;
}

connection.set_module_identifier(*module_id);

// remove dep_id from old module mgm incoming connection
let old_mgm = self
.module_graph_module_by_identifier_mut(&old_module_identifier)
Expand All @@ -958,7 +962,6 @@ impl<'a> ModuleGraph<'a> {
.module_graph_module_by_identifier_mut(module_id)
.expect("should exist mgm");
new_mgm.add_incoming_connection(*dep_id);
true
}

pub fn get_exports_info(&self, module_identifier: &ModuleIdentifier) -> ExportsInfo {
Expand Down
Loading

0 comments on commit 1f491cf

Please sign in to comment.