Skip to content

Commit

Permalink
perf(next-custom-transforms): Replace non-Sync Rc<DashMap<_, _>> usag…
Browse files Browse the repository at this point in the history
…e with Rc<RefCell<FxHashMap<_, _>>>
  • Loading branch information
bgw committed Feb 4, 2025
1 parent fd27e58 commit 18621bc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
7 changes: 3 additions & 4 deletions crates/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ use std::{
};

use backtrace::Backtrace;
use dashmap::DashMap;
use napi::bindgen_prelude::*;
use rustc_hash::FxHashSet;
use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{
base::{Compiler, TransformOutput},
common::{FilePathMapping, SourceMap},
Expand Down Expand Up @@ -108,7 +107,7 @@ pub fn complete_output(
env: &Env,
output: TransformOutput,
eliminated_packages: FxHashSet<String>,
use_cache_telemetry_tracker: DashMap<String, usize>,
use_cache_telemetry_tracker: FxHashMap<String, usize>,
) -> napi::Result<Object> {
let mut js_output = env.create_object()?;
js_output.set_named_property("code", env.create_string_from_std(output.code)?)?;
Expand All @@ -127,7 +126,7 @@ pub fn complete_output(
env.create_string_from_std(serde_json::to_string(
&use_cache_telemetry_tracker
.iter()
.map(|entry| (entry.key().clone(), *entry.value()))
.map(|(k, v)| (k.clone(), *v))
.collect::<Vec<_>>(),
)?)?,
)?;
Expand Down
15 changes: 10 additions & 5 deletions crates/napi/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ use std::{
};

use anyhow::{anyhow, bail, Context as _};
use dashmap::DashMap;
use napi::bindgen_prelude::*;
use next_custom_transforms::chain_transforms::{custom_before_pass, TransformOptions};
use once_cell::sync::Lazy;
use rustc_hash::FxHashSet;
use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{
base::{try_with_handler, Compiler, TransformOutput},
common::{comments::SingleThreadedComments, errors::ColorConfig, FileName, Mark, GLOBALS},
Expand Down Expand Up @@ -82,13 +81,14 @@ fn skip_filename() -> bool {
}

impl Task for TransformTask {
type Output = (TransformOutput, FxHashSet<String>, DashMap<String, usize>);
type Output = (TransformOutput, FxHashSet<String>, FxHashMap<String, usize>);
type JsValue = Object;

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

let res = catch_unwind(AssertUnwindSafe(|| {
try_with_handler(
Expand Down Expand Up @@ -169,7 +169,12 @@ impl Task for TransformTask {
(
o,
eliminated_packages.replace(Default::default()),
(*use_cache_telemetry_tracker).clone(),
Rc::into_inner(use_cache_telemetry_tracker)
.expect(
"All other copies of use_cache_telemetry_tracker should be \
dropped by this point",
)
.into_inner(),
)
})
.convert_err(),
Expand Down
5 changes: 2 additions & 3 deletions crates/next-custom-transforms/src/chain_transforms.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};

use dashmap::DashMap;
use either::Either;
use modularize_imports;
use preset_env_base::query::targets_to_versions;
use rustc_hash::FxHashSet;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::Deserialize;
use swc_core::{
common::{
Expand Down Expand Up @@ -126,7 +125,7 @@ pub fn custom_before_pass<'a, C>(
comments: C,
eliminated_packages: Rc<RefCell<FxHashSet<String>>>,
unresolved_mark: Mark,
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>,
use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
) -> impl Pass + 'a
where
C: Clone + Comments + 'a,
Expand Down
21 changes: 10 additions & 11 deletions crates/next-custom-transforms/src/transforms/server_actions.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::{
collections::{BTreeMap, HashSet},
cell::RefCell,
collections::{hash_map, BTreeMap, HashSet},
convert::{TryFrom, TryInto},
mem::{replace, take},
rc::Rc,
};

use dashmap::DashMap;
use hex::encode as hex_encode;
use indoc::formatdoc;
use rustc_hash::FxHashSet;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::Deserialize;
use sha1::{Digest, Sha1};
use swc_core::{
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn server_actions<C: Comments>(
file_name: &FileName,
config: Config,
comments: C,
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>,
use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
) -> impl Pass {
visit_mut_pass(ServerActions {
config,
Expand Down Expand Up @@ -220,7 +220,7 @@ struct ServerActions<C: Comments> {
arrow_or_fn_expr_ident: Option<Ident>,
exported_local_ids: HashSet<Id>,

use_cache_telemetry_tracker: Rc<DashMap<String, usize>>,
use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
}

impl<C: Comments> ServerActions<C> {
Expand Down Expand Up @@ -2618,7 +2618,7 @@ struct DirectiveVisitor<'a> {
directive: Option<Directive>,
has_file_directive: bool,
is_allowed_position: bool,
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>,
use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
}

impl DirectiveVisitor<'_> {
Expand Down Expand Up @@ -2779,14 +2779,13 @@ impl DirectiveVisitor<'_> {

// Increment telemetry counter tracking usage of "use cache" directives
fn increment_cache_usage_counter(&mut self, cache_kind: &str) {
let entry = self
.use_cache_telemetry_tracker
.entry(cache_kind.to_string());
let mut tracker_map = RefCell::borrow_mut(&self.use_cache_telemetry_tracker);
let entry = tracker_map.entry(cache_kind.to_string());
match entry {
dashmap::mapref::entry::Entry::Occupied(mut occupied) => {
hash_map::Entry::Occupied(mut occupied) => {
*occupied.get_mut() += 1;
}
dashmap::mapref::entry::Entry::Vacant(vacant) => {
hash_map::Entry::Vacant(vacant) => {
vacant.insert(1);
}
}
Expand Down

0 comments on commit 18621bc

Please sign in to comment.