Skip to content

Commit 4392366

Browse files
committed
perf: use UkeyMap and IdentifierMap in ModuleGraphPartial
1 parent 9b14875 commit 4392366

File tree

1 file changed

+22
-14
lines changed
  • crates/rspack_core/src/module_graph

1 file changed

+22
-14
lines changed

crates/rspack_core/src/module_graph/mod.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
use std::collections::hash_map::Entry;
1+
use std::{
2+
collections::{hash_map::Entry, HashMap},
3+
hash::BuildHasherDefault,
4+
};
25

3-
use rspack_collections::{IdentifierMap, UkeyMap};
6+
use rspack_collections::{IdentifierHasher, IdentifierMap, UkeyMap};
47
use rspack_error::Result;
58
use rspack_hash::RspackHashDigest;
6-
use rustc_hash::FxHashMap as HashMap;
9+
use rustc_hash::FxHashMap;
710
use swc_core::ecma::atoms::Atom;
811

912
use crate::{
@@ -22,7 +25,7 @@ use crate::{
2225

2326
// TODO Here request can be used Atom
2427
pub type ImportVarMap =
25-
HashMap<Option<ModuleIdentifier> /* request */, String /* import_var */>;
28+
FxHashMap<Option<ModuleIdentifier> /* request */, String /* import_var */>;
2629

2730
/// https://github.com/webpack/webpack/blob/ac7e531436b0d47cd88451f497cdfd0dad41535d/lib/ModuleGraph.js#L742-L748
2831
#[derive(Debug)]
@@ -44,16 +47,20 @@ pub struct ModuleGraphPartial {
4447
pub(crate) modules: IdentifierMap<Option<BoxModule>>,
4548

4649
/// Dependencies indexed by `DependencyId`.
47-
dependencies: HashMap<DependencyId, Option<BoxDependency>>,
50+
dependencies: UkeyMap<DependencyId, Option<BoxDependency>>,
4851

4952
/// AsyncDependenciesBlocks indexed by `AsyncDependenciesBlockIdentifier`.
50-
blocks: HashMap<AsyncDependenciesBlockIdentifier, Option<Box<AsyncDependenciesBlock>>>,
53+
blocks: HashMap<
54+
AsyncDependenciesBlockIdentifier,
55+
Option<Box<AsyncDependenciesBlock>>,
56+
BuildHasherDefault<IdentifierHasher>,
57+
>,
5158

5259
/// ModuleGraphModule indexed by `ModuleIdentifier`.
5360
module_graph_modules: IdentifierMap<Option<ModuleGraphModule>>,
5461

5562
/// ModuleGraphConnection indexed by `DependencyId`.
56-
connections: HashMap<DependencyId, Option<ModuleGraphConnection>>,
63+
connections: UkeyMap<DependencyId, Option<ModuleGraphConnection>>,
5764

5865
/// Dependency_id to parent module identifier and parent block
5966
///
@@ -73,13 +80,13 @@ pub struct ModuleGraphPartial {
7380
/// assert_eq!(parents_info, parent_module_id);
7481
/// })
7582
/// ```
76-
dependency_id_to_parents: HashMap<DependencyId, Option<DependencyParents>>,
83+
dependency_id_to_parents: UkeyMap<DependencyId, Option<DependencyParents>>,
7784

7885
// Module's ExportsInfo is also a part of ModuleGraph
7986
exports_info_map: UkeyMap<ExportsInfo, ExportsInfoData>,
8087
export_info_map: UkeyMap<ExportInfo, ExportInfoData>,
81-
connection_to_condition: HashMap<DependencyId, DependencyCondition>,
82-
dep_meta_map: HashMap<DependencyId, DependencyExtraMeta>,
88+
connection_to_condition: UkeyMap<DependencyId, DependencyCondition>,
89+
dep_meta_map: UkeyMap<DependencyId, DependencyExtraMeta>,
8390
}
8491

8592
#[derive(Debug, Default)]
@@ -190,13 +197,14 @@ impl<'a> ModuleGraph<'a> {
190197
pub fn get_incoming_connections_by_origin_module(
191198
&self,
192199
module_id: &ModuleIdentifier,
193-
) -> HashMap<Option<ModuleIdentifier>, Vec<ModuleGraphConnection>> {
200+
) -> FxHashMap<Option<ModuleIdentifier>, Vec<ModuleGraphConnection>> {
194201
let connections = self
195202
.module_graph_module_by_identifier(module_id)
196203
.expect("should have mgm")
197204
.incoming_connections();
198205

199-
let mut map: HashMap<Option<ModuleIdentifier>, Vec<ModuleGraphConnection>> = HashMap::default();
206+
let mut map: FxHashMap<Option<ModuleIdentifier>, Vec<ModuleGraphConnection>> =
207+
FxHashMap::default();
200208
for dep_id in connections {
201209
let con = self
202210
.connection_by_dependency_id(dep_id)
@@ -600,8 +608,8 @@ impl<'a> ModuleGraph<'a> {
600608
.expect("block has been removed to None")
601609
}
602610

603-
pub fn dependencies(&self) -> HashMap<DependencyId, &BoxDependency> {
604-
let mut res = HashMap::default();
611+
pub fn dependencies(&self) -> UkeyMap<DependencyId, &BoxDependency> {
612+
let mut res = UkeyMap::default();
605613
for item in self.partials.iter() {
606614
for (k, v) in &item.dependencies {
607615
if let Some(v) = v {

0 commit comments

Comments
 (0)