Skip to content

Commit

Permalink
fix: hash exports info should not fall into inf recursive (#5770)
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng authored Feb 26, 2024
1 parent d1fe2d1 commit 87deb9c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crates/rspack_core/src/chunk_graph/chunk_graph_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl ChunkGraph {

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

module
.get_blocks()
Expand Down
54 changes: 42 additions & 12 deletions crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ use crate::{
};

pub trait ExportsHash {
fn export_info_hash(&self, hasher: &mut dyn Hasher, module_graph: &ModuleGraph);
fn export_info_hash(
&self,
hasher: &mut dyn Hasher,
module_graph: &ModuleGraph,
already_visited: &mut HashSet<ExportInfoId>,
);
}

#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
Expand All @@ -34,9 +39,14 @@ pub struct ExportsInfoId(u32);
pub static EXPORTS_INFO_ID: AtomicU32 = AtomicU32::new(0);

impl ExportsHash for ExportsInfoId {
fn export_info_hash(&self, hasher: &mut dyn Hasher, module_graph: &ModuleGraph) {
fn export_info_hash(
&self,
hasher: &mut dyn Hasher,
module_graph: &ModuleGraph,
already_visited: &mut HashSet<ExportInfoId>,
) {
if let Some(exports_info) = module_graph.exports_info_map.try_get(**self as usize) {
exports_info.export_info_hash(hasher, module_graph);
exports_info.export_info_hash(hasher, module_graph, already_visited);
}
}
}
Expand Down Expand Up @@ -456,26 +466,31 @@ pub struct ExportsInfo {
}

impl ExportsHash for ExportsInfo {
fn export_info_hash(&self, hasher: &mut dyn Hasher, module_graph: &ModuleGraph) {
fn export_info_hash(
&self,
hasher: &mut dyn Hasher,
module_graph: &ModuleGraph,
already_visited: &mut HashSet<ExportInfoId>,
) {
if let Some(hash) = module_graph.exports_info_hash.get(&self.id) {
hash.dyn_hash(hasher);
return;
};
let mut default_hash = DefaultHasher::default();
for (name, export_info_id) in &self.exports {
name.dyn_hash(&mut default_hash);
export_info_id.export_info_hash(&mut default_hash, module_graph);
export_info_id.export_info_hash(&mut default_hash, module_graph, already_visited);
}
self
.other_exports_info
.export_info_hash(&mut default_hash, module_graph);
.export_info_hash(&mut default_hash, module_graph, already_visited);
self
._side_effects_only_info
.export_info_hash(&mut default_hash, module_graph);
.export_info_hash(&mut default_hash, module_graph, already_visited);
self._exports_are_ordered.dyn_hash(&mut default_hash);

if let Some(redirect_to) = self.redirect_to {
redirect_to.export_info_hash(&mut default_hash, module_graph);
redirect_to.export_info_hash(&mut default_hash, module_graph, already_visited);
}
let hash = default_hash.finish();
module_graph.exports_info_hash.insert(self.id, hash);
Expand Down Expand Up @@ -786,9 +801,19 @@ pub struct ExportInfoId(u32);
pub static EXPORT_INFO_ID: AtomicU32 = AtomicU32::new(0);

impl ExportsHash for ExportInfoId {
fn export_info_hash(&self, hasher: &mut dyn Hasher, module_graph: &ModuleGraph) {
fn export_info_hash(
&self,
hasher: &mut dyn Hasher,
module_graph: &ModuleGraph,
already_visited: &mut HashSet<ExportInfoId>,
) {
if already_visited.contains(self) {
return;
}
already_visited.insert(*self);

if let Some(export_info) = module_graph.export_info_map.try_get(**self as usize) {
export_info.export_info_hash(hasher, module_graph);
export_info.export_info_hash(hasher, module_graph, already_visited);
}
}
}
Expand Down Expand Up @@ -1306,7 +1331,12 @@ pub struct ExportInfo {
}

impl ExportsHash for ExportInfo {
fn export_info_hash(&self, hasher: &mut dyn Hasher, module_graph: &ModuleGraph) {
fn export_info_hash(
&self,
hasher: &mut dyn Hasher,
module_graph: &ModuleGraph,
already_visited: &mut HashSet<ExportInfoId>,
) {
self.name.dyn_hash(hasher);
self.module_identifier.dyn_hash(hasher);
self.usage_state.dyn_hash(hasher);
Expand All @@ -1325,7 +1355,7 @@ impl ExportsHash for ExportInfo {
self.target_is_set.dyn_hash(hasher);
self.max_target_is_set.dyn_hash(hasher);
if let Some(exports_info_id) = self.exports_info {
exports_info_id.export_info_hash(hasher, module_graph);
exports_info_id.export_info_hash(hasher, module_graph, already_visited);
}
self.exports_info_owned.dyn_hash(hasher);
}
Expand Down

1 comment on commit 87deb9c

@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-02-26 531a19a) Current Change
10000_development-mode + exec 1.7 s ± 26 ms 1.7 s ± 24 ms -0.26 %
10000_development-mode_hmr + exec 920 ms ± 13 ms 923 ms ± 17 ms +0.32 %
10000_production-mode + exec 2.65 s ± 53 ms 2.7 s ± 84 ms +1.99 %
arco-pro_development-mode + exec 2.45 s ± 34 ms 2.53 s ± 63 ms +3.43 %
arco-pro_development-mode_hmr + exec 877 ms ± 20 ms 860 ms ± 14 ms -1.93 %
arco-pro_production-mode + exec 4.07 s ± 42 ms 4.12 s ± 81 ms +1.46 %
threejs_development-mode_10x + exec 1.9 s ± 36 ms 1.92 s ± 25 ms +1.24 %
threejs_development-mode_10x_hmr + exec 1.13 s ± 10 ms 1.12 s ± 6.9 ms -0.46 %
threejs_production-mode_10x + exec 5.83 s ± 31 ms 5.81 s ± 157 ms -0.41 %

Please sign in to comment.