Unverified Commit 883147a9 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(turbopack): Define and use an FxDashMap alias everywhere (#75538)

This is similar to the `FxHashMap`/`FxHashSet`/`FxIndexMap`/`FxIndexSet` aliases we use everywhere.
parent 7dcc35c8
...@@ -19,7 +19,6 @@ use std::{ ...@@ -19,7 +19,6 @@ use std::{
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use auto_hash_map::{AutoMap, AutoSet}; use auto_hash_map::{AutoMap, AutoSet};
use dashmap::DashMap;
use parking_lot::{Condvar, Mutex}; use parking_lot::{Condvar, Mutex};
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
use smallvec::smallvec; use smallvec::smallvec;
...@@ -33,7 +32,7 @@ use turbo_tasks::{ ...@@ -33,7 +32,7 @@ use turbo_tasks::{
registry, registry,
task_statistics::TaskStatisticsApi, task_statistics::TaskStatisticsApi,
util::IdFactoryWithReuse, util::IdFactoryWithReuse,
CellId, FunctionId, RawVc, ReadConsistency, SessionId, TaskId, TraitTypeId, CellId, FunctionId, FxDashMap, RawVc, ReadConsistency, SessionId, TaskId, TraitTypeId,
TurboTasksBackendApi, ValueTypeId, TRANSIENT_TASK_BIT, TurboTasksBackendApi, ValueTypeId, TRANSIENT_TASK_BIT,
}; };
...@@ -152,7 +151,7 @@ struct TurboTasksBackendInner<B: BackingStorage> { ...@@ -152,7 +151,7 @@ struct TurboTasksBackendInner<B: BackingStorage> {
persisted_task_cache_log: Option<TaskCacheLog>, persisted_task_cache_log: Option<TaskCacheLog>,
task_cache: BiMap<Arc<CachedTaskType>, TaskId>, task_cache: BiMap<Arc<CachedTaskType>, TaskId>,
transient_tasks: DashMap<TaskId, Arc<TransientTask>, BuildHasherDefault<FxHasher>>, transient_tasks: FxDashMap<TaskId, Arc<TransientTask>>,
persisted_storage_data_log: Option<PersistedStorageLog>, persisted_storage_data_log: Option<PersistedStorageLog>,
persisted_storage_meta_log: Option<PersistedStorageLog>, persisted_storage_meta_log: Option<PersistedStorageLog>,
...@@ -214,7 +213,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> { ...@@ -214,7 +213,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
), ),
persisted_task_cache_log: need_log.then(|| Sharded::new(shard_amount)), persisted_task_cache_log: need_log.then(|| Sharded::new(shard_amount)),
task_cache: BiMap::new(), task_cache: BiMap::new(),
transient_tasks: DashMap::default(), transient_tasks: FxDashMap::default(),
persisted_storage_data_log: need_log.then(|| PersistedStorageLog::new(shard_amount)), persisted_storage_data_log: need_log.then(|| PersistedStorageLog::new(shard_amount)),
persisted_storage_meta_log: need_log.then(|| PersistedStorageLog::new(shard_amount)), persisted_storage_meta_log: need_log.then(|| PersistedStorageLog::new(shard_amount)),
storage: Storage::new(), storage: Storage::new(),
......
use std::{ use std::{
hash::{BuildHasherDefault, Hash}, hash::Hash,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
thread::available_parallelism, thread::available_parallelism,
}; };
use dashmap::DashMap; use turbo_tasks::{FxDashMap, TaskId};
use rustc_hash::FxHasher;
use turbo_tasks::TaskId;
use crate::{ use crate::{
backend::dynamic_storage::DynamicStorage, backend::dynamic_storage::DynamicStorage,
...@@ -502,7 +500,7 @@ impl InnerStorage { ...@@ -502,7 +500,7 @@ impl InnerStorage {
} }
pub struct Storage { pub struct Storage {
map: DashMap<TaskId, Box<InnerStorage>, BuildHasherDefault<FxHasher>>, map: FxDashMap<TaskId, Box<InnerStorage>>,
} }
impl Storage { impl Storage {
...@@ -510,7 +508,7 @@ impl Storage { ...@@ -510,7 +508,7 @@ impl Storage {
let shard_amount = let shard_amount =
(available_parallelism().map_or(4, |v| v.get()) * 64).next_power_of_two(); (available_parallelism().map_or(4, |v| v.get()) * 64).next_power_of_two();
Self { Self {
map: DashMap::with_capacity_and_hasher_and_shard_amount( map: FxDashMap::with_capacity_and_hasher_and_shard_amount(
1024 * 1024, 1024 * 1024,
Default::default(), Default::default(),
shard_amount, shard_amount,
......
...@@ -10,8 +10,8 @@ use std::{ ...@@ -10,8 +10,8 @@ use std::{
use anyhow::{Ok, Result}; use anyhow::{Ok, Result};
use byteorder::WriteBytesExt; use byteorder::WriteBytesExt;
use dashmap::DashMap;
use rustc_hash::{FxHashMap, FxHasher}; use rustc_hash::{FxHashMap, FxHasher};
use turbo_tasks::FxDashMap;
use crate::database::{ use crate::database::{
by_key_space::ByKeySpace, by_key_space::ByKeySpace,
...@@ -39,7 +39,7 @@ impl<T: KeyValueDatabase> Borrow<[u8]> for ValueBuffer<'_, T> { ...@@ -39,7 +39,7 @@ impl<T: KeyValueDatabase> Borrow<[u8]> for ValueBuffer<'_, T> {
} }
} }
type Cache = ByKeySpace<DashMap<Vec<u8>, Option<Vec<u8>>, BuildHasherDefault<FxHasher>>>; type Cache = ByKeySpace<FxDashMap<Vec<u8>, Option<Vec<u8>>>>;
pub struct StartupCacheLayer<T: KeyValueDatabase> { pub struct StartupCacheLayer<T: KeyValueDatabase> {
database: T, database: T,
...@@ -81,7 +81,7 @@ impl<T: KeyValueDatabase> StartupCacheLayer<T> { ...@@ -81,7 +81,7 @@ impl<T: KeyValueDatabase> StartupCacheLayer<T> {
fresh_db, fresh_db,
cache_size: AtomicUsize::new(0), cache_size: AtomicUsize::new(0),
cache: ByKeySpace::new(|key_space| { cache: ByKeySpace::new(|key_space| {
DashMap::with_capacity_and_hasher( FxDashMap::with_capacity_and_hasher(
match key_space { match key_space {
KeySpace::Infra => 8, KeySpace::Infra => 8,
KeySpace::TaskMeta => 1024 * 1024, KeySpace::TaskMeta => 1024 * 1024,
......
use std::{ use std::{borrow::Borrow, hash::Hash};
borrow::Borrow,
hash::{BuildHasherDefault, Hash},
};
use dashmap::{mapref::entry::Entry, DashMap}; use dashmap::mapref::entry::Entry;
use rustc_hash::FxHasher; use turbo_tasks::FxDashMap;
/// A bidirectional [`DashMap`] that allows lookup by key or value. /// A bidirectional [`FxDashMap`] that allows lookup by key or value.
/// ///
/// As keys and values are stored twice, they should be small types, such as /// As keys and values are stored twice, they should be small types, such as
/// [`Arc`][`std::sync::Arc`]. /// [`Arc`][`std::sync::Arc`].
pub struct BiMap<K, V> { pub struct BiMap<K, V> {
forward: DashMap<K, V, BuildHasherDefault<FxHasher>>, forward: FxDashMap<K, V>,
reverse: DashMap<V, K, BuildHasherDefault<FxHasher>>, reverse: FxDashMap<V, K>,
} }
impl<K, V> BiMap<K, V> impl<K, V> BiMap<K, V>
...@@ -22,8 +19,8 @@ where ...@@ -22,8 +19,8 @@ where
{ {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
forward: DashMap::default(), forward: FxDashMap::default(),
reverse: DashMap::default(), reverse: FxDashMap::default(),
} }
} }
......
...@@ -202,6 +202,7 @@ mod tests { ...@@ -202,6 +202,7 @@ mod tests {
use std::thread::scope; use std::thread::scope;
use rand::prelude::SliceRandom; use rand::prelude::SliceRandom;
use turbo_tasks::FxDashMap;
use super::*; use super::*;
...@@ -210,7 +211,7 @@ mod tests { ...@@ -210,7 +211,7 @@ mod tests {
const N: usize = 100000; const N: usize = 100000;
const THREADS: usize = 20; const THREADS: usize = 20;
let map = DashMap::with_shard_amount(4); let map = FxDashMap::with_hasher_and_shard_amount(Default::default(), 4);
let indicies = (0..THREADS) let indicies = (0..THREADS)
.map(|_| { .map(|_| {
let mut vec = (0..N).collect::<Vec<_>>(); let mut vec = (0..N).collect::<Vec<_>>();
......
...@@ -131,6 +131,7 @@ pub use vc::{ ...@@ -131,6 +131,7 @@ pub use vc::{
pub type FxIndexSet<T> = indexmap::IndexSet<T, BuildHasherDefault<FxHasher>>; pub type FxIndexSet<T> = indexmap::IndexSet<T, BuildHasherDefault<FxHasher>>;
pub type FxIndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasherDefault<FxHasher>>; pub type FxIndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasherDefault<FxHasher>>;
pub type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
// Copied from indexmap! and indexset! // Copied from indexmap! and indexset!
#[macro_export] #[macro_export]
......
...@@ -3,10 +3,12 @@ use std::{ ...@@ -3,10 +3,12 @@ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use dashmap::{mapref::entry::Entry, DashMap}; use dashmap::mapref::entry::Entry;
use crate::FxDashMap;
pub struct OnceConcurrentlyMap<K: Hash + Eq + Ord + Send + Sync + 'static, V: Clone + Send + Sync> { pub struct OnceConcurrentlyMap<K: Hash + Eq + Ord + Send + Sync + 'static, V: Clone + Send + Sync> {
inner: DashMap<&'static K, Arc<Mutex<Option<V>>>>, inner: FxDashMap<&'static K, Arc<Mutex<Option<V>>>>,
} }
impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> Default impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> Default
...@@ -20,7 +22,7 @@ impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> Default ...@@ -20,7 +22,7 @@ impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> Default
impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> OnceConcurrentlyMap<K, V> { impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> OnceConcurrentlyMap<K, V> {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
inner: DashMap::new(), inner: FxDashMap::default(),
} }
} }
...@@ -48,7 +50,7 @@ impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> OnceConcurrentlyM ...@@ -48,7 +50,7 @@ impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> OnceConcurrentlyM
} }
struct TemporarilyInserted<'a, K: 'static + Hash + Eq + Ord + Send + Sync, V: Send + Sync> { struct TemporarilyInserted<'a, K: 'static + Hash + Eq + Ord + Send + Sync, V: Send + Sync> {
inner: &'a DashMap<&'static K, V>, inner: &'a FxDashMap<&'static K, V>,
key: &'a K, key: &'a K,
} }
...@@ -71,7 +73,7 @@ pub struct SafeOnceConcurrentlyMap< ...@@ -71,7 +73,7 @@ pub struct SafeOnceConcurrentlyMap<
K: Clone + Hash + Eq + Ord + Send + Sync + 'static, K: Clone + Hash + Eq + Ord + Send + Sync + 'static,
V: Clone + Send + Sync, V: Clone + Send + Sync,
> { > {
inner: DashMap<K, Arc<Mutex<Option<V>>>>, inner: FxDashMap<K, Arc<Mutex<Option<V>>>>,
} }
impl<K: Clone + Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> Default impl<K: Clone + Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> Default
...@@ -87,7 +89,7 @@ impl<K: Clone + Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> ...@@ -87,7 +89,7 @@ impl<K: Clone + Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync>
{ {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
inner: DashMap::new(), inner: FxDashMap::default(),
} }
} }
...@@ -119,7 +121,7 @@ struct SafeTemporarilyInserted< ...@@ -119,7 +121,7 @@ struct SafeTemporarilyInserted<
K: 'static + Clone + Hash + Eq + Ord + Send + Sync, K: 'static + Clone + Hash + Eq + Ord + Send + Sync,
V: Send + Sync, V: Send + Sync,
> { > {
inner: &'a DashMap<K, V>, inner: &'a FxDashMap<K, V>,
key: &'a K, key: &'a K,
} }
......
use std::{ use std::{fmt::Debug, hash::Hash, num::NonZeroU64, ops::Deref};
fmt::Debug,
hash::{BuildHasherDefault, Hash},
num::NonZeroU64,
ops::Deref,
};
use dashmap::{mapref::entry::Entry, DashMap}; use dashmap::mapref::entry::Entry;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rustc_hash::FxHasher;
use crate::{ use crate::{
id::{FunctionId, TraitTypeId, ValueTypeId}, id::{FunctionId, TraitTypeId, ValueTypeId},
id_factory::IdFactory, id_factory::IdFactory,
native_function::NativeFunction, native_function::NativeFunction,
no_move_vec::NoMoveVec, no_move_vec::NoMoveVec,
TraitType, ValueType, FxDashMap, TraitType, ValueType,
}; };
type FxDashMap<K, V> = DashMap<K, V, BuildHasherDefault<FxHasher>>;
static FUNCTION_ID_FACTORY: IdFactory<FunctionId> = IdFactory::new(1, u32::MAX as u64); static FUNCTION_ID_FACTORY: IdFactory<FunctionId> = IdFactory::new(1, u32::MAX as u64);
static FUNCTIONS_BY_NAME: Lazy<FxDashMap<&'static str, FunctionId>> = Lazy::new(DashMap::default); static FUNCTIONS_BY_NAME: Lazy<FxDashMap<&'static str, FunctionId>> = Lazy::new(FxDashMap::default);
static FUNCTIONS_BY_VALUE: Lazy<FxDashMap<&'static NativeFunction, FunctionId>> = static FUNCTIONS_BY_VALUE: Lazy<FxDashMap<&'static NativeFunction, FunctionId>> =
Lazy::new(DashMap::default); Lazy::new(FxDashMap::default);
static FUNCTIONS: Lazy<NoMoveVec<(&'static NativeFunction, &'static str)>> = static FUNCTIONS: Lazy<NoMoveVec<(&'static NativeFunction, &'static str)>> =
Lazy::new(NoMoveVec::new); Lazy::new(NoMoveVec::new);
static VALUE_TYPE_ID_FACTORY: IdFactory<ValueTypeId> = IdFactory::new(1, u32::MAX as u64); static VALUE_TYPE_ID_FACTORY: IdFactory<ValueTypeId> = IdFactory::new(1, u32::MAX as u64);
static VALUE_TYPES_BY_NAME: Lazy<FxDashMap<&'static str, ValueTypeId>> = static VALUE_TYPES_BY_NAME: Lazy<FxDashMap<&'static str, ValueTypeId>> =
Lazy::new(DashMap::default); Lazy::new(FxDashMap::default);
static VALUE_TYPES_BY_VALUE: Lazy<FxDashMap<&'static ValueType, ValueTypeId>> = static VALUE_TYPES_BY_VALUE: Lazy<FxDashMap<&'static ValueType, ValueTypeId>> =
Lazy::new(DashMap::default); Lazy::new(FxDashMap::default);
static VALUE_TYPES: Lazy<NoMoveVec<(&'static ValueType, &'static str)>> = Lazy::new(NoMoveVec::new); static VALUE_TYPES: Lazy<NoMoveVec<(&'static ValueType, &'static str)>> = Lazy::new(NoMoveVec::new);
static TRAIT_TYPE_ID_FACTORY: IdFactory<TraitTypeId> = IdFactory::new(1, u32::MAX as u64); static TRAIT_TYPE_ID_FACTORY: IdFactory<TraitTypeId> = IdFactory::new(1, u32::MAX as u64);
static TRAIT_TYPES_BY_NAME: Lazy<FxDashMap<&'static str, TraitTypeId>> = static TRAIT_TYPES_BY_NAME: Lazy<FxDashMap<&'static str, TraitTypeId>> =
Lazy::new(DashMap::default); Lazy::new(FxDashMap::default);
static TRAIT_TYPES_BY_VALUE: Lazy<FxDashMap<&'static TraitType, TraitTypeId>> = static TRAIT_TYPES_BY_VALUE: Lazy<FxDashMap<&'static TraitType, TraitTypeId>> =
Lazy::new(DashMap::default); Lazy::new(FxDashMap::default);
static TRAIT_TYPES: Lazy<NoMoveVec<(&'static TraitType, &'static str)>> = Lazy::new(NoMoveVec::new); static TRAIT_TYPES: Lazy<NoMoveVec<(&'static TraitType, &'static str)>> = Lazy::new(NoMoveVec::new);
fn register_thing< fn register_thing<
......
use std::{ use std::sync::{Arc, OnceLock};
hash::BuildHasherDefault,
sync::{Arc, OnceLock},
};
use dashmap::DashMap;
use rustc_hash::FxHasher;
use serde::{ser::SerializeMap, Serialize, Serializer}; use serde::{ser::SerializeMap, Serialize, Serializer};
use crate::{registry, FunctionId}; use crate::{registry, FunctionId, FxDashMap};
/// An API for optionally enabling, updating, and reading aggregated statistics. /// An API for optionally enabling, updating, and reading aggregated statistics.
#[derive(Default)] #[derive(Default)]
...@@ -19,7 +14,7 @@ impl TaskStatisticsApi { ...@@ -19,7 +14,7 @@ impl TaskStatisticsApi {
pub fn enable(&self) -> &Arc<TaskStatistics> { pub fn enable(&self) -> &Arc<TaskStatistics> {
self.inner.get_or_init(|| { self.inner.get_or_init(|| {
Arc::new(TaskStatistics { Arc::new(TaskStatistics {
inner: DashMap::with_hasher(Default::default()), inner: FxDashMap::with_hasher(Default::default()),
}) })
}) })
} }
...@@ -43,7 +38,7 @@ impl TaskStatisticsApi { ...@@ -43,7 +38,7 @@ impl TaskStatisticsApi {
/// A type representing the enabled state of [`TaskStatisticsApi`]. Implements [`serde::Serialize`]. /// A type representing the enabled state of [`TaskStatisticsApi`]. Implements [`serde::Serialize`].
pub struct TaskStatistics { pub struct TaskStatistics {
inner: DashMap<FunctionId, TaskFunctionStatistics, BuildHasherDefault<FxHasher>>, inner: FxDashMap<FunctionId, TaskFunctionStatistics>,
} }
impl TaskStatistics { impl TaskStatistics {
......
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