Skip to content
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

perf: improve determine_export_assignments #6431

Merged
merged 6 commits into from
May 16, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
perf: improve determine_export_assignments (2)
* eliminate Atom clone
* rewrite collect hidden_exports
quininer committed May 10, 2024
commit aec0dbf1d81e9901514564572be723b5e60ac143
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::cell::OnceCell;
use std::{collections::HashMap, sync::Arc};

use rspack_core::{
@@ -308,14 +307,11 @@ impl HarmonyExportImportedSpecifierDependency {
let hidden_exports = self
.discover_active_exports_from_other_star_exports(module_graph)
.map(|other_star_exports| {
let mut hide_exports = HashSet::default();
for i in 0..other_star_exports.names_slice {
hide_exports.insert(other_star_exports.names[i].clone());
}
for e in ignored_exports.iter() {
hide_exports.remove(e);
}
hide_exports
other_star_exports.names.into_iter()
.take(other_star_exports.names_slice)
.filter(|name| !ignored_exports.contains(name))
.cloned()
.collect::<HashSet<_>>()
});
if !no_extra_exports && !no_extra_imports {
return StarReexportsInfo {
@@ -418,10 +414,10 @@ impl HarmonyExportImportedSpecifierDependency {
}
}

pub fn discover_active_exports_from_other_star_exports(
pub fn discover_active_exports_from_other_star_exports<'a>(
&self,
module_graph: &ModuleGraph,
) -> Option<DiscoverActiveExportsFromOtherStarExportsRet> {
module_graph: &'a ModuleGraph,
) -> Option<DiscoverActiveExportsFromOtherStarExportsRet<'a>> {
if let Some(other_star_exports) = &self.other_star_exports {
if other_star_exports.is_empty() {
return None;
@@ -828,8 +824,8 @@ impl HarmonyExportImportedSpecifierDependency {
}

#[derive(Debug)]
pub struct DiscoverActiveExportsFromOtherStarExportsRet {
names: Vec<Atom>,
pub struct DiscoverActiveExportsFromOtherStarExportsRet<'a> {
names: Vec<&'a Atom>,
names_slice: usize,
pub dependency_indices: Vec<usize>,
pub dependency_index: usize,
@@ -1349,34 +1345,31 @@ pub struct StarReexportsInfo {
}

/// return (names, dependency_indices)
fn determine_export_assignments(
module_graph: &ModuleGraph,
fn determine_export_assignments<'a>(
module_graph: &'a ModuleGraph,
dependencies: &[DependencyId],
additional_dependency: Option<DependencyId>,
) -> (Vec<Atom>, Vec<usize>) {
) -> (Vec<&'a Atom>, Vec<usize>) {
// https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad41535d/lib/dependencies/HarmonyExportImportedSpecifierDependency.js#L109
// js `Set` keep the insertion order, use `IndexSet` to align there behavior
// js `Set` keep the insertion order
let mut names = Vec::new();
let mut hints = HashSet::default();
let mut dependency_indices =
Vec::with_capacity(dependencies.len() + additional_dependency.is_some() as usize);
let empty = OnceCell::new();

for dependency in dependencies.iter().chain(additional_dependency.iter()) {
if let Some(module_identifier) = module_graph.module_identifier_by_dependency_id(dependency) {
let exports_info = module_graph.get_exports_info(module_identifier);
for export_info_id in exports_info.exports.values() {
let export_info = module_graph.get_export_info_by_id(export_info_id);
// SAFETY: This is safe because a real export can't export empty string
let export_info_name = export_info.name
.as_ref()
.unwrap_or_else(|| empty.get_or_init(Atom::default));
let export_info_name = export_info.name.as_ref().expect("export name is empty");
if matches!(export_info.provided, Some(ExportInfoProvided::True))
&& export_info_name != "default"
&& !hints.contains(export_info_name)
{
names.push(export_info_name);
hints.insert(export_info_name);
names.push(export_info_name.clone());
}
}
}