Skip to content

Commit

Permalink
refactor: copiable export info (#7390)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Jul 31, 2024
1 parent 82674e1 commit 0f5d217
Show file tree
Hide file tree
Showing 36 changed files with 988 additions and 1,138 deletions.
6 changes: 5 additions & 1 deletion crates/rspack_collections/src/ukey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use std::collections::HashSet;
use std::hash::Hash;
use std::{collections::HashMap, fmt::Debug, hash::BuildHasherDefault};

use dashmap::{DashMap, DashSet};
use indexmap::{IndexMap, IndexSet};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};

#[macro_export]
macro_rules! impl_item_ukey {
Expand All @@ -18,16 +20,18 @@ macro_rules! impl_item_ukey {

pub type UkeyMap<K, V> = HashMap<K, V, BuildHasherDefault<UkeyHasher>>;
pub type UkeyIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<UkeyHasher>>;
pub type UkeyDashMap<K, V> = DashMap<K, V, BuildHasherDefault<UkeyHasher>>;

pub type UkeySet<K> = HashSet<K, BuildHasherDefault<UkeyHasher>>;
pub type UkeyIndexSet<K> = IndexSet<K, BuildHasherDefault<UkeyHasher>>;
pub type UkeyDashSet<K> = DashSet<K, BuildHasherDefault<UkeyHasher>>;

pub trait ItemUkey {
fn ukey(&self) -> Ukey;
}

/// Ukey stands for Unique key
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Ukey(u32);

impl Ukey {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/chunk_graph/chunk_graph_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::hash::Hasher;

use rspack_collections::{IdentifierMap, UkeySet};
use rspack_util::ext::DynHash;
use rustc_hash::{FxHashSet as HashSet, FxHasher};
use rustc_hash::FxHasher;

use crate::update_hash::{UpdateHashContext, UpdateRspackHash};
use crate::ChunkGraph;
Expand Down Expand Up @@ -186,7 +186,7 @@ impl ChunkGraph {

module_graph
.get_exports_info(&module.identifier())
.export_info_hash(&mut hasher, module_graph, &mut HashSet::default());
.export_info_hash(&mut hasher, module_graph, &mut UkeySet::default());

module
.get_blocks()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ mod t {
use crate::{
compiler::make::cutout::has_module_graph_change::ModuleDeps, AsContextDependency, BuildInfo,
BuildMeta, CodeGenerationResult, Compilation, ConcatenationScope, Context, DependenciesBlock,
Dependency, DependencyId, DependencyTemplate, ExportsInfoId, FactoryMeta, Module,
Dependency, DependencyId, DependencyTemplate, ExportsInfo, FactoryMeta, Module,
ModuleDependency, ModuleGraph, ModuleGraphModule, ModuleGraphPartial, ModuleIdentifier,
ModuleType, RuntimeSpec, SourceType,
};
Expand Down Expand Up @@ -304,9 +304,9 @@ mod t {
let module1_id = module1.id;

mg.add_module(module_orig);
mg.add_module_graph_module(ModuleGraphModule::new(module_orig_id, ExportsInfoId::new()));
mg.add_module_graph_module(ModuleGraphModule::new(module_orig_id, ExportsInfo::new()));
mg.add_module(module1);
mg.add_module_graph_module(ModuleGraphModule::new(module1_id, ExportsInfoId::new()));
mg.add_module_graph_module(ModuleGraphModule::new(module1_id, ExportsInfo::new()));
mg.add_dependency(dep1);
mg.set_resolved_module(Some(module_orig_id), dep1_id, module1_id)
.unwrap();
Expand Down
27 changes: 14 additions & 13 deletions crates/rspack_core/src/compiler/make/repair/factorize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use super::{add::AddTask, MakeTaskContext};
use crate::{
module_graph::ModuleGraphModule,
utils::task_loop::{Task, TaskResult, TaskType},
BoxDependency, CompilerOptions, Context, DependencyId, ExportInfo, ExportsInfo, ModuleFactory,
ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer, ModuleProfile,
Resolve,
BoxDependency, CompilerOptions, Context, DependencyId, ExportInfoData, ExportsInfoData,
ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer,
ModuleProfile, Resolve,
};

#[derive(Debug)]
Expand Down Expand Up @@ -58,9 +58,9 @@ impl Task<MakeTaskContext> for FactorizeTask {
.or(self.issuer_layer.as_ref())
.cloned();

let other_exports_info = ExportInfo::new(None, None);
let side_effects_only_info = ExportInfo::new(Some("*side effects only*".into()), None);
let exports_info = ExportsInfo::new(other_exports_info.id, side_effects_only_info.id);
let other_exports_info = ExportInfoData::new(None, None);
let side_effects_only_info = ExportInfoData::new(Some("*side effects only*".into()), None);
let exports_info = ExportsInfoData::new(other_exports_info.id(), side_effects_only_info.id());
let factorize_result_task = FactorizeResultTask {
// dependency: dep_id,
original_module_identifier: self.original_module_identifier,
Expand Down Expand Up @@ -141,9 +141,9 @@ impl Task<MakeTaskContext> for FactorizeTask {
/// a struct temporarily used creating ExportsInfo
#[derive(Debug)]
pub struct ExportsInfoRelated {
pub exports_info: ExportsInfo,
pub other_exports_info: ExportInfo,
pub side_effects_info: ExportInfo,
pub exports_info: ExportsInfoData,
pub other_exports_info: ExportInfoData,
pub side_effects_info: ExportInfoData,
}

#[derive(Debug)]
Expand Down Expand Up @@ -250,19 +250,20 @@ impl Task<MakeTaskContext> for FactorizeResultTask {
return Ok(vec![]);
};
let module_identifier = module.identifier();
let mut mgm = ModuleGraphModule::new(module.identifier(), exports_info_related.exports_info.id);
let mut mgm =
ModuleGraphModule::new(module.identifier(), exports_info_related.exports_info.id());
mgm.set_issuer_if_unset(original_module_identifier);

module_graph.set_exports_info(
exports_info_related.exports_info.id,
exports_info_related.exports_info.id(),
exports_info_related.exports_info,
);
module_graph.set_export_info(
exports_info_related.side_effects_info.id,
exports_info_related.side_effects_info.id(),
exports_info_related.side_effects_info,
);
module_graph.set_export_info(
exports_info_related.other_exports_info.id,
exports_info_related.other_exports_info.id(),
exports_info_related.other_exports_info,
);
tracing::trace!("Module created: {}", &module_identifier);
Expand Down
66 changes: 35 additions & 31 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
ChunkInitFragments, CodeGenerationDataTopLevelDeclarations, CodeGenerationExportsFinalNames,
CodeGenerationResult, Compilation, ConcatenatedModuleIdent, ConcatenationScope, ConnectionId,
ConnectionState, Context, DependenciesBlock, DependencyId, DependencyTemplate, DependencyType,
ErrorSpan, ExportInfoId, ExportInfoProvided, ExportsArgument, ExportsType, FactoryMeta,
ErrorSpan, ExportInfo, ExportInfoProvided, ExportsArgument, ExportsType, FactoryMeta,
IdentCollector, LibIdentOptions, Module, ModuleDependency, ModuleGraph, ModuleGraphConnection,
ModuleIdentifier, ModuleLayer, ModuleType, Resolve, RuntimeCondition, RuntimeGlobals,
RuntimeSpec, SourceType, SpanExt, Template, UsageState, UsedName, DEFAULT_EXPORT,
Expand Down Expand Up @@ -923,13 +923,18 @@ impl Module for ConcatenatedModule {
let exports_info = module_graph.get_exports_info(&root_module_id);
let mut exports_final_names: Vec<(String, String)> = vec![];

for (_, export_info_id) in exports_info.exports.iter() {
let export_info = export_info_id.get_export_info(&module_graph);
let name = export_info.name.clone().unwrap_or("".into());
if matches!(export_info.provided, Some(ExportInfoProvided::False)) {
for export_info in exports_info.ordered_exports(&module_graph) {
let name = export_info
.name(&module_graph)
.cloned()
.unwrap_or("".into());
if matches!(
export_info.provided(&module_graph),
Some(ExportInfoProvided::False)
) {
continue;
}
let used_name = export_info.get_used_name(None, runtime);
let used_name = export_info.get_used_name(&module_graph, None, runtime);

let Some(used_name) = used_name else {
unused_exports.insert(name);
Expand All @@ -952,7 +957,7 @@ impl Module for ConcatenatedModule {
exports_final_names.push((used_name.to_string(), final_name.clone()));
format!(
"/* {} */ {}",
if export_info.is_reexport() {
if export_info.is_reexport(&module_graph) {
"reexport"
} else {
"binding"
Expand All @@ -969,8 +974,8 @@ impl Module for ConcatenatedModule {
if compilation
.get_module_graph()
.get_exports_info(&self.id())
.other_exports_info
.get_used(&compilation.get_module_graph(), runtime)
.other_exports_info(&module_graph)
.get_used(&module_graph, runtime)
!= UsageState::Unused
{
should_add_harmony_flag = true
Expand Down Expand Up @@ -1071,17 +1076,22 @@ impl Module for ConcatenatedModule {

let mut ns_obj = Vec::new();
let exports_info = module_graph.get_exports_info(module_info_id);
for (_name, export_info_id) in exports_info.exports.iter() {
let export_info = export_info_id.get_export_info(&module_graph);
if matches!(export_info.provided, Some(ExportInfoProvided::False)) {
for export_info in exports_info.ordered_exports(&module_graph) {
if matches!(
export_info.provided(&module_graph),
Some(ExportInfoProvided::False)
) {
continue;
}

if let Some(used_name) = export_info.get_used_name(None, runtime) {
if let Some(used_name) = export_info.get_used_name(&module_graph, None, runtime) {
let final_name = Self::get_final_name(
&compilation.get_module_graph(),
module_info_id,
vec![export_info.name.clone().unwrap_or("".into())],
vec![export_info
.name(&module_graph)
.cloned()
.unwrap_or("".into())],
&mut module_to_info_map,
runtime,
&mut needed_namespace_objects,
Expand Down Expand Up @@ -1822,7 +1832,7 @@ impl ConcatenatedModule {
as_call: bool,
strict_harmony_module: bool,
asi_safe: Option<bool>,
already_visited: &mut HashSet<ExportInfoId>,
already_visited: &mut HashSet<ExportInfo>,
) -> Binding {
let info = module_to_info_map
.get(info_id)
Expand Down Expand Up @@ -1984,12 +1994,9 @@ impl ConcatenatedModule {
let exports_info = mg.get_exports_info(&info.id());
// webpack use get_exports_info here, https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad41535d/lib/optimize/ConcatenatedModule.js#L377-L377
// But in our arch, there is no way to modify module graph during code_generation phase
let export_info_id = exports_info
.id
.get_read_only_export_info(&export_name[0], mg)
.id;
let export_info = exports_info.get_read_only_export_info(mg, &export_name[0]);

if already_visited.contains(&export_info_id) {
if already_visited.contains(&export_info) {
return Binding::Raw(RawBinding {
raw_name: "/* circular reexport */ Object(function x() { x() }())".into(),
ids: Vec::new(),
Expand All @@ -1999,13 +2006,15 @@ impl ConcatenatedModule {
});
}

already_visited.insert(export_info_id);
already_visited.insert(export_info);

match info {
ModuleInfo::Concatenated(info) => {
let export_id = export_name.first().cloned();
let export_info = export_info_id.get_export_info(mg);
if matches!(export_info.provided, Some(crate::ExportInfoProvided::False)) {
if matches!(
export_info.provided(mg),
Some(crate::ExportInfoProvided::False)
) {
needed_namespace_objects.insert(info.module);
return Binding::Raw(RawBinding {
raw_name: info
Expand All @@ -2024,9 +2033,7 @@ impl ConcatenatedModule {
&& let Some(direct_export) = info.export_map.as_ref().and_then(|map| map.get(export_id))
{
if let Some(used_name) =
exports_info
.id
.get_used_name(mg, runtime, UsedName::Vec(export_name.clone()))
exports_info.get_used_name(mg, runtime, UsedName::Vec(export_name.clone()))
{
// https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L402-L404
let used_name = used_name.to_used_name_vec();
Expand Down Expand Up @@ -2064,7 +2071,7 @@ impl ConcatenatedModule {
});
}

let reexport = export_info_id.find_target(
let reexport = export_info.find_target(
mg,
Arc::new(|module: &ModuleIdentifier| module_to_info_map.contains_key(module)),
);
Expand Down Expand Up @@ -2106,7 +2113,6 @@ impl ConcatenatedModule {
if info.namespace_export_symbol.is_some() {
// That's how webpack write https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/optimize/ConcatenatedModule.js#L463-L471
let used_name = exports_info
.id
.get_used_name(mg, runtime, UsedName::Vec(export_name.clone()))
.expect("should have export name");
let used_name = used_name.to_used_name_vec();
Expand All @@ -2131,9 +2137,7 @@ impl ConcatenatedModule {
}
ModuleInfo::External(info) => {
if let Some(used_name) =
exports_info
.id
.get_used_name(mg, runtime, UsedName::Vec(export_name.clone()))
exports_info.get_used_name(mg, runtime, UsedName::Vec(export_name.clone()))
{
let used_name = used_name.to_used_name_vec();
let comment = if used_name == export_name {
Expand Down
7 changes: 3 additions & 4 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,10 @@ pub fn export_from_import(

if !export_name.is_empty() {
let used_name: Cow<Vec<Atom>> = {
let exports_info_id = compilation
let exports_info = compilation
.get_module_graph()
.get_exports_info(&module_identifier)
.id;
let used = exports_info_id.get_used_name(
.get_exports_info(&module_identifier);
let used = exports_info.get_used_name(
&compilation.get_module_graph(),
*runtime,
crate::UsedName::Vec(export_name.clone()),
Expand Down
Loading

2 comments on commit 0f5d217

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ❌ failure
rspress ✅ success
rsbuild ✅ success
examples ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-07-31 9e2cbe9) Current Change
10000_development-mode + exec 2.27 s ± 20 ms 2.25 s ± 14 ms -1.09 %
10000_development-mode_hmr + exec 702 ms ± 11 ms 715 ms ± 3.6 ms +1.88 %
10000_production-mode + exec 2.76 s ± 39 ms 2.81 s ± 24 ms +1.69 %
arco-pro_development-mode + exec 1.88 s ± 66 ms 1.89 s ± 68 ms +0.51 %
arco-pro_development-mode_hmr + exec 433 ms ± 1.7 ms 434 ms ± 3 ms +0.24 %
arco-pro_production-mode + exec 3.45 s ± 114 ms 3.4 s ± 102 ms -1.51 %
threejs_development-mode_10x + exec 1.75 s ± 25 ms 1.75 s ± 14 ms +0.46 %
threejs_development-mode_10x_hmr + exec 863 ms ± 8.3 ms 878 ms ± 6.1 ms +1.77 %
threejs_production-mode_10x + exec 5.49 s ± 26 ms 5.51 s ± 40 ms +0.41 %

Please sign in to comment.