Unverified Commit 7dcc35c8 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

perf(next-custom-transforms): Replace non-`Sync` `Rc<DashMap<_, _>>` usage with…

perf(next-custom-transforms): Replace non-`Sync` `Rc<DashMap<_, _>>` usage with `Rc<RefCell<FxHashMap<_, _>>>` (#75534)

This is a follow-up to https://github.com/vercel/next.js/pull/75007

`DashMap` is needed for situations where you have many threads (or tokio tasks) modifying the same data at the same time. It introduces a lot of memory and CPU overhead to create individually lockable `RwLock` shards. This type of data structure implements `Sync`, indicating that it can be shared between threads.

`Rc` is not `Sync` (`Arc` is `Sync`), so wrapping `DashMap` in `Rc` doesn't make a lot of sense: You're paying a pretty high overhead for the ability to share data between threads, and wrapping it in something that does not support sharing data between threads.

This visitor code is entirely single-threaded, so just use `Rc<RefCell<FxHashMap>>` instead. `RefCell` is a very cheap single-threaded version of a `Mutex`. `FxHashMap` is just a `HashMap` with the faster `FxHash` algorithm used.
parent 1dfca629
...@@ -40,9 +40,8 @@ use std::{ ...@@ -40,9 +40,8 @@ use std::{
}; };
use backtrace::Backtrace; use backtrace::Backtrace;
use dashmap::DashMap;
use napi::bindgen_prelude::*; use napi::bindgen_prelude::*;
use rustc_hash::FxHashSet; use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{ use swc_core::{
base::{Compiler, TransformOutput}, base::{Compiler, TransformOutput},
common::{FilePathMapping, SourceMap}, common::{FilePathMapping, SourceMap},
...@@ -108,7 +107,7 @@ pub fn complete_output( ...@@ -108,7 +107,7 @@ pub fn complete_output(
env: &Env, env: &Env,
output: TransformOutput, output: TransformOutput,
eliminated_packages: FxHashSet<String>, eliminated_packages: FxHashSet<String>,
use_cache_telemetry_tracker: DashMap<String, usize>, use_cache_telemetry_tracker: FxHashMap<String, usize>,
) -> napi::Result<Object> { ) -> napi::Result<Object> {
let mut js_output = env.create_object()?; let mut js_output = env.create_object()?;
js_output.set_named_property("code", env.create_string_from_std(output.code)?)?; js_output.set_named_property("code", env.create_string_from_std(output.code)?)?;
...@@ -127,7 +126,7 @@ pub fn complete_output( ...@@ -127,7 +126,7 @@ pub fn complete_output(
env.create_string_from_std(serde_json::to_string( env.create_string_from_std(serde_json::to_string(
&use_cache_telemetry_tracker &use_cache_telemetry_tracker
.iter() .iter()
.map(|entry| (entry.key().clone(), *entry.value())) .map(|(k, v)| (k.clone(), *v))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
)?)?, )?)?,
)?; )?;
......
...@@ -35,11 +35,10 @@ use std::{ ...@@ -35,11 +35,10 @@ use std::{
}; };
use anyhow::{anyhow, bail, Context as _}; use anyhow::{anyhow, bail, Context as _};
use dashmap::DashMap;
use napi::bindgen_prelude::*; use napi::bindgen_prelude::*;
use next_custom_transforms::chain_transforms::{custom_before_pass, TransformOptions}; use next_custom_transforms::chain_transforms::{custom_before_pass, TransformOptions};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rustc_hash::FxHashSet; use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{ use swc_core::{
base::{try_with_handler, Compiler, TransformOutput}, base::{try_with_handler, Compiler, TransformOutput},
common::{comments::SingleThreadedComments, errors::ColorConfig, FileName, Mark, GLOBALS}, common::{comments::SingleThreadedComments, errors::ColorConfig, FileName, Mark, GLOBALS},
...@@ -82,13 +81,14 @@ fn skip_filename() -> bool { ...@@ -82,13 +81,14 @@ fn skip_filename() -> bool {
} }
impl Task for TransformTask { impl Task for TransformTask {
type Output = (TransformOutput, FxHashSet<String>, DashMap<String, usize>); type Output = (TransformOutput, FxHashSet<String>, FxHashMap<String, usize>);
type JsValue = Object; type JsValue = Object;
fn compute(&mut self) -> napi::Result<Self::Output> { fn compute(&mut self) -> napi::Result<Self::Output> {
GLOBALS.set(&Default::default(), || { GLOBALS.set(&Default::default(), || {
let eliminated_packages: Rc<RefCell<FxHashSet<String>>> = 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(|| { let res = catch_unwind(AssertUnwindSafe(|| {
try_with_handler( try_with_handler(
...@@ -169,7 +169,12 @@ impl Task for TransformTask { ...@@ -169,7 +169,12 @@ impl Task for TransformTask {
( (
o, o,
eliminated_packages.replace(Default::default()), 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(), .convert_err(),
......
use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc}; use std::{cell::RefCell, path::PathBuf, rc::Rc, sync::Arc};
use dashmap::DashMap;
use either::Either; use either::Either;
use modularize_imports; use modularize_imports;
use preset_env_base::query::targets_to_versions; use preset_env_base::query::targets_to_versions;
use rustc_hash::FxHashSet; use rustc_hash::{FxHashMap, FxHashSet};
use serde::Deserialize; use serde::Deserialize;
use swc_core::{ use swc_core::{
common::{ common::{
...@@ -126,7 +125,7 @@ pub fn custom_before_pass<'a, C>( ...@@ -126,7 +125,7 @@ pub fn custom_before_pass<'a, C>(
comments: C, comments: C,
eliminated_packages: Rc<RefCell<FxHashSet<String>>>, eliminated_packages: Rc<RefCell<FxHashSet<String>>>,
unresolved_mark: Mark, unresolved_mark: Mark,
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>, use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
) -> impl Pass + 'a ) -> impl Pass + 'a
where where
C: Clone + Comments + 'a, C: Clone + Comments + 'a,
......
use std::{ use std::{
collections::{BTreeMap, HashSet}, cell::RefCell,
collections::{hash_map, BTreeMap, HashSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
mem::{replace, take}, mem::{replace, take},
rc::Rc, rc::Rc,
}; };
use dashmap::DashMap;
use hex::encode as hex_encode; use hex::encode as hex_encode;
use indoc::formatdoc; use indoc::formatdoc;
use rustc_hash::FxHashSet; use rustc_hash::{FxHashMap, FxHashSet};
use serde::Deserialize; use serde::Deserialize;
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};
use swc_core::{ use swc_core::{
...@@ -123,7 +123,7 @@ pub fn server_actions<C: Comments>( ...@@ -123,7 +123,7 @@ pub fn server_actions<C: Comments>(
file_name: &FileName, file_name: &FileName,
config: Config, config: Config,
comments: C, comments: C,
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>, use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
) -> impl Pass { ) -> impl Pass {
visit_mut_pass(ServerActions { visit_mut_pass(ServerActions {
config, config,
...@@ -220,7 +220,7 @@ struct ServerActions<C: Comments> { ...@@ -220,7 +220,7 @@ struct ServerActions<C: Comments> {
arrow_or_fn_expr_ident: Option<Ident>, arrow_or_fn_expr_ident: Option<Ident>,
exported_local_ids: HashSet<Id>, 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> { impl<C: Comments> ServerActions<C> {
...@@ -2618,7 +2618,7 @@ struct DirectiveVisitor<'a> { ...@@ -2618,7 +2618,7 @@ struct DirectiveVisitor<'a> {
directive: Option<Directive>, directive: Option<Directive>,
has_file_directive: bool, has_file_directive: bool,
is_allowed_position: bool, is_allowed_position: bool,
use_cache_telemetry_tracker: Rc<DashMap<String, usize>>, use_cache_telemetry_tracker: Rc<RefCell<FxHashMap<String, usize>>>,
} }
impl DirectiveVisitor<'_> { impl DirectiveVisitor<'_> {
...@@ -2779,14 +2779,13 @@ impl DirectiveVisitor<'_> { ...@@ -2779,14 +2779,13 @@ impl DirectiveVisitor<'_> {
// Increment telemetry counter tracking usage of "use cache" directives // Increment telemetry counter tracking usage of "use cache" directives
fn increment_cache_usage_counter(&mut self, cache_kind: &str) { fn increment_cache_usage_counter(&mut self, cache_kind: &str) {
let entry = self let mut tracker_map = RefCell::borrow_mut(&self.use_cache_telemetry_tracker);
.use_cache_telemetry_tracker let entry = tracker_map.entry(cache_kind.to_string());
.entry(cache_kind.to_string());
match entry { match entry {
dashmap::mapref::entry::Entry::Occupied(mut occupied) => { hash_map::Entry::Occupied(mut occupied) => {
*occupied.get_mut() += 1; *occupied.get_mut() += 1;
} }
dashmap::mapref::entry::Entry::Vacant(vacant) => { hash_map::Entry::Vacant(vacant) => {
vacant.insert(1); vacant.insert(1);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment