From ec659a16e6d36111512c022e6a0ed0681467e040 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Tue, 11 Jun 2024 15:07:23 +0800 Subject: [PATCH] perf: put visited_chunk_group_keys out of the loop and use FxHashMap and FxHashSet --- Cargo.lock | 1 + .../Cargo.toml | 1 + .../src/lib.rs | 13 ++++++------- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d9c1b03f6c..b09b27c3dee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3279,6 +3279,7 @@ dependencies = [ "rspack_core", "rspack_error", "rspack_hook", + "rustc-hash", ] [[package]] diff --git a/crates/rspack_plugin_ensure_chunk_conditions/Cargo.toml b/crates/rspack_plugin_ensure_chunk_conditions/Cargo.toml index b8cbc3fcff9..66b06484e88 100644 --- a/crates/rspack_plugin_ensure_chunk_conditions/Cargo.toml +++ b/crates/rspack_plugin_ensure_chunk_conditions/Cargo.toml @@ -11,3 +11,4 @@ version = "0.1.0" rspack_core = { path = "../rspack_core" } rspack_error = { path = "../rspack_error" } rspack_hook = { path = "../rspack_hook" } +rustc-hash = { workspace = true } diff --git a/crates/rspack_plugin_ensure_chunk_conditions/src/lib.rs b/crates/rspack_plugin_ensure_chunk_conditions/src/lib.rs index ffbf93e3bce..808672abadb 100644 --- a/crates/rspack_plugin_ensure_chunk_conditions/src/lib.rs +++ b/crates/rspack_plugin_ensure_chunk_conditions/src/lib.rs @@ -1,11 +1,10 @@ -use std::collections::{HashMap, HashSet}; - use rspack_core::{ get_chunk_from_ukey, get_chunk_group_from_ukey, Compilation, CompilationOptimizeChunks, Logger, Plugin, PluginContext, }; use rspack_error::Result; use rspack_hook::{plugin, plugin_hook}; +use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; #[plugin] #[derive(Debug, Default)] @@ -16,7 +15,7 @@ fn optimize_chunks(&self, compilation: &mut Compilation) -> Result> let logger = compilation.get_logger(self.name()); let start = logger.time("ensure chunk conditions"); - let mut source_module_chunks = HashMap::new(); + let mut source_module_chunks = HashMap::default(); compilation .get_module_graph() .modules() @@ -38,14 +37,14 @@ fn optimize_chunks(&self, compilation: &mut Compilation) -> Result> } }); - let mut target_module_chunks = HashMap::new(); - + let mut target_module_chunks = HashMap::default(); + let mut visited_chunk_group_keys = HashSet::default(); for (module_id, chunk_keys) in &source_module_chunks { - let mut target_chunks = HashSet::new(); + let mut target_chunks = HashSet::default(); for chunk_key in chunk_keys { if let Some(chunk) = get_chunk_from_ukey(chunk_key, &compilation.chunk_by_ukey) { let mut chunk_group_keys = chunk.groups.iter().collect::>(); - let mut visited_chunk_group_keys = HashSet::new(); + visited_chunk_group_keys.clear(); 'out: while let Some(chunk_group_key) = chunk_group_keys.pop() { if visited_chunk_group_keys.contains(chunk_group_key) { continue;