Skip to content

Commit 9865cba

Browse files
committed
perf(next-custom-transforms): Replace non-Sync Rc<DashMap<_, _>> usage with Rc<RefCell<FxHashMap<_, _>>>
1 parent 4711122 commit 9865cba

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

crates/napi/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ use std::{
4040
};
4141

4242
use backtrace::Backtrace;
43-
use dashmap::DashMap;
4443
use napi::bindgen_prelude::*;
45-
use rustc_hash::FxHashSet;
44+
use rustc_hash::{FxHashMap, FxHashSet};
4645
use swc_core::{
4746
base::{Compiler, TransformOutput},
4847
common::{FilePathMapping, SourceMap},
@@ -108,7 +107,7 @@ pub fn complete_output(
108107
env: &Env,
109108
output: TransformOutput,
110109
eliminated_packages: FxHashSet<String>,
111-
use_cache_telemetry_tracker: DashMap<String, usize>,
110+
use_cache_telemetry_tracker: FxHashMap<String, usize>,
112111
) -> napi::Result<Object> {
113112
let mut js_output = env.create_object()?;
114113
js_output.set_named_property("code", env.create_string_from_std(output.code)?)?;
@@ -127,7 +126,7 @@ pub fn complete_output(
127126
env.create_string_from_std(serde_json::to_string(
128127
&use_cache_telemetry_tracker
129128
.iter()
130-
.map(|entry| (entry.key().clone(), *entry.value()))
129+
.map(|(k, v)| (k.clone(), *v))
131130
.collect::<Vec<_>>(),
132131
)?)?,
133132
)?;

crates/napi/src/transform.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ use std::{
3535
};
3636

3737
use anyhow::{anyhow, bail, Context as _};
38-
use dashmap::DashMap;
3938
use napi::bindgen_prelude::*;
4039
use next_custom_transforms::chain_transforms::{custom_before_pass, TransformOptions};
4140
use once_cell::sync::Lazy;
42-
use rustc_hash::FxHashSet;
41+
use rustc_hash::{FxHashMap, FxHashSet};
4342
use swc_core::{
4443
base::{try_with_handler, Compiler, TransformOutput},
4544
common::{comments::SingleThreadedComments, errors::ColorConfig, FileName, Mark, GLOBALS},
@@ -82,13 +81,14 @@ fn skip_filename() -> bool {
8281
}
8382

8483
impl Task for TransformTask {
85-
type Output = (TransformOutput, FxHashSet<String>, DashMap<String, usize>);
84+
type Output = (TransformOutput, FxHashSet<String>, FxHashMap<String, usize>);
8685
type JsValue = Object;
8786

8887
fn compute(&mut self) -> napi::Result<Self::Output> {
8988
GLOBALS.set(&Default::default(), || {
9089
let eliminated_packages: Rc<RefCell<FxHashSet<String>>> = Default::default();
91-
let use_cache_telemetry_tracker: Rc<DashMap<String, usize>> = Default::default();
90+
let use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>> =
91+
Default::default();
9292

9393
let res = catch_unwind(AssertUnwindSafe(|| {
9494
try_with_handler(
@@ -169,7 +169,12 @@ impl Task for TransformTask {
169169
(
170170
o,
171171
eliminated_packages.replace(Default::default()),
172-
(*use_cache_telemetry_tracker).clone(),
172+
Rc::into_inner(use_cache_telemetry_tracker)
173+
.expect(
174+
"All other copies of use_cache_telemetry_tracker should be \
175+
dropped by this point",
176+
)
177+
.into_inner(),
173178
)
174179
})
175180
.convert_err(),

crates/next-custom-transforms/src/chain_transforms.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
22

3-
use dashmap::DashMap;
43
use either::Either;
54
use modularize_imports;
65
use preset_env_base::query::targets_to_versions;
7-
use rustc_hash::FxHashSet;
6+
use rustc_hash::{FxHashMap, FxHashSet};
87
use serde::Deserialize;
98
use swc_core::{
109
common::{
@@ -126,7 +125,7 @@ pub fn custom_before_pass<'a, C>(
126125
comments: C,
127126
eliminated_packages: Rc<RefCell<FxHashSet<String>>>,
128127
unresolved_mark: Mark,
129-
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>,
128+
use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
130129
) -> impl Pass + 'a
131130
where
132131
C: Clone + Comments + 'a,

crates/next-custom-transforms/src/transforms/server_actions.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::{
2-
collections::{BTreeMap, HashSet},
2+
cell::RefCell,
3+
collections::{hash_map, BTreeMap, HashSet},
34
convert::{TryFrom, TryInto},
45
mem::{replace, take},
56
rc::Rc,
67
};
78

8-
use dashmap::DashMap;
99
use hex::encode as hex_encode;
1010
use indoc::formatdoc;
11-
use rustc_hash::FxHashSet;
11+
use rustc_hash::{FxHashMap, FxHashSet};
1212
use serde::Deserialize;
1313
use sha1::{Digest, Sha1};
1414
use swc_core::{
@@ -123,7 +123,7 @@ pub fn server_actions<C: Comments>(
123123
file_name: &FileName,
124124
config: Config,
125125
comments: C,
126-
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>,
126+
use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
127127
) -> impl Pass {
128128
visit_mut_pass(ServerActions {
129129
config,
@@ -220,7 +220,7 @@ struct ServerActions<C: Comments> {
220220
arrow_or_fn_expr_ident: Option<Ident>,
221221
exported_local_ids: HashSet<Id>,
222222

223-
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>,
223+
use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
224224
}
225225

226226
impl<C: Comments> ServerActions<C> {
@@ -2618,7 +2618,7 @@ struct DirectiveVisitor<'a> {
26182618
directive: Option<Directive>,
26192619
has_file_directive: bool,
26202620
is_allowed_position: bool,
2621-
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>,
2621+
use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
26222622
}
26232623

26242624
impl DirectiveVisitor<'_> {
@@ -2779,14 +2779,13 @@ impl DirectiveVisitor<'_> {
27792779

27802780
// Increment telemetry counter tracking usage of "use cache" directives
27812781
fn increment_cache_usage_counter(&mut self, cache_kind: &str) {
2782-
let entry = self
2783-
.use_cache_telemetry_tracker
2784-
.entry(cache_kind.to_string());
2782+
let mut tracker_map = RefCell::borrow_mut(&self.use_cache_telemetry_tracker);
2783+
let entry = tracker_map.entry(cache_kind.to_string());
27852784
match entry {
2786-
dashmap::mapref::entry::Entry::Occupied(mut occupied) => {
2785+
hash_map::Entry::Occupied(mut occupied) => {
27872786
*occupied.get_mut() += 1;
27882787
}
2789-
dashmap::mapref::entry::Entry::Vacant(vacant) => {
2788+
hash_map::Entry::Vacant(vacant) => {
27902789
vacant.insert(1);
27912790
}
27922791
}

0 commit comments

Comments
 (0)