Skip to content

Commit 8675099

Browse files
authored
perf: use fxhash for HashMap in napi (#11080)
1 parent 65b7ef6 commit 8675099

File tree

18 files changed

+50
-48
lines changed

18 files changed

+50
-48
lines changed

crates/rspack_binding_api/src/chunk.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use std::{cell::RefCell, collections::HashMap, ptr::NonNull};
1+
use std::{cell::RefCell, ptr::NonNull};
22

3-
use napi::{bindgen_prelude::ToNapiValue, Either, Env, JsString};
3+
use napi::{
4+
bindgen_prelude::{Object, ToNapiValue},
5+
Either, Env, JsString,
6+
};
47
use napi_derive::napi;
8+
use rspack_collections::UkeyMap;
59
use rspack_core::{Compilation, CompilationId};
610
use rspack_napi::OneShotRef;
711

@@ -116,20 +120,17 @@ impl Chunk {
116120
)
117121
}
118122

119-
#[napi(getter)]
120-
pub fn content_hash(&self) -> napi::Result<HashMap<String, &str>> {
123+
#[napi(getter, ts_return_type = "Record<string, string>")]
124+
pub fn content_hash(&self, env: &Env) -> napi::Result<Object> {
121125
let (compilation, chunk) = self.as_ref()?;
122-
Ok(
123-
chunk
124-
.content_hash(&compilation.chunk_hashes_artifact)
125-
.map(|content_hash| {
126-
content_hash
127-
.iter()
128-
.map(|(key, v)| (key.to_string(), v.encoded()))
129-
.collect::<HashMap<String, &str>>()
130-
})
131-
.unwrap_or_default(),
132-
)
126+
127+
let mut object = Object::new(env)?;
128+
if let Some(content_hash) = chunk.content_hash(&compilation.chunk_hashes_artifact) {
129+
for (key, value) in content_hash.iter() {
130+
object.set(key.to_string(), value.encoded())?;
131+
}
132+
}
133+
Ok(object)
133134
}
134135

135136
#[napi(getter)]
@@ -246,7 +247,7 @@ impl Chunk {
246247
}
247248

248249
thread_local! {
249-
static CHUNK_INSTANCE_REFS: RefCell<HashMap<CompilationId, HashMap<rspack_core::ChunkUkey, OneShotRef>>> = Default::default();
250+
static CHUNK_INSTANCE_REFS: RefCell<UkeyMap<CompilationId, UkeyMap<rspack_core::ChunkUkey, OneShotRef>>> = Default::default();
250251
}
251252

252253
pub struct ChunkWrapper {
@@ -287,7 +288,7 @@ impl ToNapiValue for ChunkWrapper {
287288
let refs = match entry {
288289
std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(),
289290
std::collections::hash_map::Entry::Vacant(entry) => {
290-
let refs = HashMap::default();
291+
let refs = UkeyMap::default();
291292
entry.insert(refs)
292293
}
293294
};

crates/rspack_binding_api/src/chunk_group.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::{cell::RefCell, ptr::NonNull};
22

33
use napi::{bindgen_prelude::ToNapiValue, Either, Env, JsString};
44
use napi_derive::napi;
5+
use rspack_collections::UkeyMap;
56
use rspack_core::{Compilation, CompilationId};
67
use rspack_napi::OneShotRef;
7-
use rustc_hash::FxHashMap as HashMap;
88

99
use crate::{location::RealDependencyLocation, ChunkWrapper, ModuleObject, ModuleObjectRef};
1010

@@ -179,7 +179,7 @@ impl ChunkGroup {
179179
}
180180

181181
thread_local! {
182-
static CHUNK_GROUP_INSTANCE_REFS: RefCell<HashMap<CompilationId, HashMap<rspack_core::ChunkGroupUkey, OneShotRef>>> = Default::default();
182+
static CHUNK_GROUP_INSTANCE_REFS: RefCell<UkeyMap<CompilationId, UkeyMap<rspack_core::ChunkGroupUkey, OneShotRef>>> = Default::default();
183183
}
184184

185185
pub struct ChunkGroupWrapper {
@@ -217,7 +217,7 @@ impl ToNapiValue for ChunkGroupWrapper {
217217
let refs = match entry {
218218
std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(),
219219
std::collections::hash_map::Entry::Vacant(entry) => {
220-
let refs = HashMap::default();
220+
let refs = UkeyMap::default();
221221
entry.insert(refs)
222222
}
223223
};

crates/rspack_binding_api/src/codegen_result.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::collections::HashMap;
2-
31
use napi_derive::napi;
42
use rspack_core::{get_runtime_key, CodeGenerationResult, CodeGenerationResults};
3+
use rustc_hash::FxHashMap as HashMap;
54

65
#[napi(object)]
76
#[derive(Debug)]

crates/rspack_binding_api/src/html.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::HashMap;
2-
31
use cow_utils::CowUtils;
42
use napi::Either;
53
use napi_derive::napi;
@@ -10,6 +8,7 @@ use rspack_plugin_html::{
108
AfterEmitData, AfterTemplateExecutionData, AlterAssetTagGroupsData, AlterAssetTagsData,
119
BeforeAssetTagGenerationData, BeforeEmitData,
1210
};
11+
use rustc_hash::FxHashMap as HashMap;
1312

1413
#[napi(object)]
1514
pub struct JsHtmlPluginTag {

crates/rspack_binding_api/src/options/raw_resolve.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::HashMap;
2-
31
use napi::Either;
42
use napi_derive::napi;
53
use rspack_core::{
@@ -8,6 +6,7 @@ use rspack_core::{
86
};
97
use rspack_error::error;
108
use rspack_regex::RspackRegex;
9+
use rustc_hash::FxHashMap as HashMap;
1110

1211
pub type AliasValue = serde_json::Value;
1312

crates/rspack_binding_api/src/plugins/context_replacement.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use napi_derive::napi;
33
use rspack_error::{miette::IntoDiagnostic, Error};
44
use rspack_plugin_context_replacement::ContextReplacementPluginOptions;
55
use rspack_regex::RspackRegex;
6-
use rustc_hash::FxHashMap as HashMap;
6+
use rustc_hash::FxHashMap;
77

88
#[napi(object, object_to_js = false)]
99
pub struct RawContextReplacementPluginOptions<'a> {
@@ -31,7 +31,7 @@ impl<'a> TryFrom<RawContextReplacementPluginOptions<'a>> for ContextReplacementP
3131
} = val;
3232

3333
let new_content_create_context_map = if let Some(raw) = new_content_create_context_map {
34-
let mut map = HashMap::default();
34+
let mut map = FxHashMap::default();
3535
let keys = Object::keys(&raw).into_diagnostic()?;
3636
for key in keys {
3737
let value = raw.get::<String>(&key).into_diagnostic()?;

crates/rspack_binding_api/src/plugins/js_loader/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use std::{collections::HashMap, ptr::NonNull, sync::Arc};
1+
use std::{ptr::NonNull, sync::Arc};
22

33
use napi::bindgen_prelude::*;
44
use napi_derive::napi;
55
use rspack_core::{LoaderContext, Module, RunnerContext};
66
use rspack_error::ToStringResultToRspackResultExt;
77
use rspack_loader_runner::State as LoaderState;
88
use rspack_napi::threadsafe_js_value_ref::ThreadsafeJsValueRef;
9+
use rustc_hash::FxHashMap as HashMap;
910

1011
use crate::{ModuleObject, RspackError};
1112

crates/rspack_binding_api/src/raw_options/raw_builtins/raw_css_extract.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use std::collections::HashMap;
2-
31
use napi_derive::napi;
42
use rspack_plugin_extract_css::plugin::{CssExtractOptions, InsertType};
3+
use rustc_hash::FxHashMap as HashMap;
54

65
use crate::JsFilename;
76

crates/rspack_binding_api/src/raw_options/raw_builtins/raw_html.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, str::FromStr};
1+
use std::str::FromStr;
22

33
use napi::bindgen_prelude::{Either3, Promise};
44
use napi_derive::napi;
@@ -10,6 +10,7 @@ use rspack_plugin_html::{
1010
},
1111
sri::HtmlSriHashFunction,
1212
};
13+
use rustc_hash::FxHashMap as HashMap;
1314

1415
pub type RawHtmlScriptLoading = String;
1516
pub type RawHtmlInject = String;

crates/rspack_binding_api/src/raw_options/raw_builtins/raw_http_uri.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, fmt::Debug, sync::Arc};
1+
use std::{fmt::Debug, sync::Arc};
22

33
use async_trait::async_trait;
44
use napi::bindgen_prelude::{Buffer, Either, FnArgs, Promise};
@@ -10,6 +10,7 @@ use rspack_plugin_schemes::{
1010
};
1111
use rspack_regex::RspackRegex;
1212
use rspack_util::asset_condition::{AssetCondition, AssetConditions};
13+
use rustc_hash::FxHashMap as HashMap;
1314

1415
type HttpClientRequest =
1516
ThreadsafeFunction<FnArgs<(String, HashMap<String, String>)>, Promise<JsHttpResponseRaw>>;

0 commit comments

Comments
 (0)