Skip to content

Commit bab2c39

Browse files
committed
refactor(turbopack): Define and use an FxDashMap alias everywhere
1 parent 6e80bfb commit bab2c39

File tree

9 files changed

+43
-58
lines changed

9 files changed

+43
-58
lines changed

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::{
1919

2020
use anyhow::{bail, Result};
2121
use auto_hash_map::{AutoMap, AutoSet};
22-
use dashmap::DashMap;
2322
use parking_lot::{Condvar, Mutex};
2423
use rustc_hash::FxHasher;
2524
use smallvec::smallvec;
@@ -33,7 +32,7 @@ use turbo_tasks::{
3332
registry,
3433
task_statistics::TaskStatisticsApi,
3534
util::IdFactoryWithReuse,
36-
CellId, FunctionId, RawVc, ReadConsistency, SessionId, TaskId, TraitTypeId,
35+
CellId, FunctionId, FxDashMap, RawVc, ReadConsistency, SessionId, TaskId, TraitTypeId,
3736
TurboTasksBackendApi, ValueTypeId, TRANSIENT_TASK_BIT,
3837
};
3938

@@ -152,7 +151,7 @@ struct TurboTasksBackendInner<B: BackingStorage> {
152151

153152
persisted_task_cache_log: Option<TaskCacheLog>,
154153
task_cache: BiMap<Arc<CachedTaskType>, TaskId>,
155-
transient_tasks: DashMap<TaskId, Arc<TransientTask>, BuildHasherDefault<FxHasher>>,
154+
transient_tasks: FxDashMap<TaskId, Arc<TransientTask>>,
156155

157156
persisted_storage_data_log: Option<PersistedStorageLog>,
158157
persisted_storage_meta_log: Option<PersistedStorageLog>,
@@ -214,7 +213,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
214213
),
215214
persisted_task_cache_log: need_log.then(|| Sharded::new(shard_amount)),
216215
task_cache: BiMap::new(),
217-
transient_tasks: DashMap::default(),
216+
transient_tasks: FxDashMap::default(),
218217
persisted_storage_data_log: need_log.then(|| PersistedStorageLog::new(shard_amount)),
219218
persisted_storage_meta_log: need_log.then(|| PersistedStorageLog::new(shard_amount)),
220219
storage: Storage::new(),

turbopack/crates/turbo-tasks-backend/src/backend/storage.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use std::{
2-
hash::{BuildHasherDefault, Hash},
2+
hash::Hash,
33
ops::{Deref, DerefMut},
44
thread::available_parallelism,
55
};
66

7-
use dashmap::DashMap;
8-
use rustc_hash::FxHasher;
9-
use turbo_tasks::TaskId;
7+
use turbo_tasks::{FxDashMap, TaskId};
108

119
use crate::{
1210
backend::dynamic_storage::DynamicStorage,
@@ -502,15 +500,15 @@ impl InnerStorage {
502500
}
503501

504502
pub struct Storage {
505-
map: DashMap<TaskId, Box<InnerStorage>, BuildHasherDefault<FxHasher>>,
503+
map: FxDashMap<TaskId, Box<InnerStorage>>,
506504
}
507505

508506
impl Storage {
509507
pub fn new() -> Self {
510508
let shard_amount =
511509
(available_parallelism().map_or(4, |v| v.get()) * 64).next_power_of_two();
512510
Self {
513-
map: DashMap::with_capacity_and_hasher_and_shard_amount(
511+
map: FxDashMap::with_capacity_and_hasher_and_shard_amount(
514512
1024 * 1024,
515513
Default::default(),
516514
shard_amount,

turbopack/crates/turbo-tasks-backend/src/database/startup_cache.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::{
1010

1111
use anyhow::{Ok, Result};
1212
use byteorder::WriteBytesExt;
13-
use dashmap::DashMap;
1413
use rustc_hash::{FxHashMap, FxHasher};
14+
use turbo_tasks::FxDashMap;
1515

1616
use crate::database::{
1717
by_key_space::ByKeySpace,
@@ -39,7 +39,7 @@ impl<T: KeyValueDatabase> Borrow<[u8]> for ValueBuffer<'_, T> {
3939
}
4040
}
4141

42-
type Cache = ByKeySpace<DashMap<Vec<u8>, Option<Vec<u8>>, BuildHasherDefault<FxHasher>>>;
42+
type Cache = ByKeySpace<FxDashMap<Vec<u8>, Option<Vec<u8>>>>;
4343

4444
pub struct StartupCacheLayer<T: KeyValueDatabase> {
4545
database: T,
@@ -81,7 +81,7 @@ impl<T: KeyValueDatabase> StartupCacheLayer<T> {
8181
fresh_db,
8282
cache_size: AtomicUsize::new(0),
8383
cache: ByKeySpace::new(|key_space| {
84-
DashMap::with_capacity_and_hasher(
84+
FxDashMap::with_capacity_and_hasher(
8585
match key_space {
8686
KeySpace::Infra => 8,
8787
KeySpace::TaskMeta => 1024 * 1024,

turbopack/crates/turbo-tasks-backend/src/utils/bi_map.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
use std::{
2-
borrow::Borrow,
3-
hash::{BuildHasherDefault, Hash},
4-
};
1+
use std::{borrow::Borrow, hash::Hash};
52

6-
use dashmap::{mapref::entry::Entry, DashMap};
7-
use rustc_hash::FxHasher;
3+
use dashmap::mapref::entry::Entry;
4+
use turbo_tasks::FxDashMap;
85

9-
/// A bidirectional [`DashMap`] that allows lookup by key or value.
6+
/// A bidirectional [`FxDashMap`] that allows lookup by key or value.
107
///
118
/// As keys and values are stored twice, they should be small types, such as
129
/// [`Arc`][`std::sync::Arc`].
1310
pub struct BiMap<K, V> {
14-
forward: DashMap<K, V, BuildHasherDefault<FxHasher>>,
15-
reverse: DashMap<V, K, BuildHasherDefault<FxHasher>>,
11+
forward: FxDashMap<K, V>,
12+
reverse: FxDashMap<V, K>,
1613
}
1714

1815
impl<K, V> BiMap<K, V>
@@ -22,8 +19,8 @@ where
2219
{
2320
pub fn new() -> Self {
2421
Self {
25-
forward: DashMap::default(),
26-
reverse: DashMap::default(),
22+
forward: FxDashMap::default(),
23+
reverse: FxDashMap::default(),
2724
}
2825
}
2926

turbopack/crates/turbo-tasks-backend/src/utils/dash_map_multi.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ mod tests {
202202
use std::thread::scope;
203203

204204
use rand::prelude::SliceRandom;
205+
use turbo_tasks::FxDashMap;
205206

206207
use super::*;
207208

@@ -210,7 +211,7 @@ mod tests {
210211
const N: usize = 100000;
211212
const THREADS: usize = 20;
212213

213-
let map = DashMap::with_shard_amount(4);
214+
let map = FxDashMap::with_hasher_and_shard_amount(Default::default(), 4);
214215
let indicies = (0..THREADS)
215216
.map(|_| {
216217
let mut vec = (0..N).collect::<Vec<_>>();

turbopack/crates/turbo-tasks/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub use vc::{
131131

132132
pub type FxIndexSet<T> = indexmap::IndexSet<T, BuildHasherDefault<FxHasher>>;
133133
pub type FxIndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasherDefault<FxHasher>>;
134+
pub type FxDashMap<K, V> = dashmap::DashMap<K, V, BuildHasherDefault<FxHasher>>;
134135

135136
// Copied from indexmap! and indexset!
136137
#[macro_export]

turbopack/crates/turbo-tasks/src/once_map.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ use std::{
33
sync::{Arc, Mutex},
44
};
55

6-
use dashmap::{mapref::entry::Entry, DashMap};
6+
use dashmap::mapref::entry::Entry;
7+
8+
use crate::FxDashMap;
79

810
pub struct OnceConcurrentlyMap<K: Hash + Eq + Ord + Send + Sync + 'static, V: Clone + Send + Sync> {
9-
inner: DashMap<&'static K, Arc<Mutex<Option<V>>>>,
11+
inner: FxDashMap<&'static K, Arc<Mutex<Option<V>>>>,
1012
}
1113

1214
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
2022
impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> OnceConcurrentlyMap<K, V> {
2123
pub fn new() -> Self {
2224
Self {
23-
inner: DashMap::new(),
25+
inner: FxDashMap::default(),
2426
}
2527
}
2628

@@ -48,7 +50,7 @@ impl<K: Hash + Eq + Ord + Send + Sync, V: Clone + Send + Sync> OnceConcurrentlyM
4850
}
4951

5052
struct TemporarilyInserted<'a, K: 'static + Hash + Eq + Ord + Send + Sync, V: Send + Sync> {
51-
inner: &'a DashMap<&'static K, V>,
53+
inner: &'a FxDashMap<&'static K, V>,
5254
key: &'a K,
5355
}
5456

@@ -71,7 +73,7 @@ pub struct SafeOnceConcurrentlyMap<
7173
K: Clone + Hash + Eq + Ord + Send + Sync + 'static,
7274
V: Clone + Send + Sync,
7375
> {
74-
inner: DashMap<K, Arc<Mutex<Option<V>>>>,
76+
inner: FxDashMap<K, Arc<Mutex<Option<V>>>>,
7577
}
7678

7779
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>
8789
{
8890
pub fn new() -> Self {
8991
Self {
90-
inner: DashMap::new(),
92+
inner: FxDashMap::default(),
9193
}
9294
}
9395

@@ -119,7 +121,7 @@ struct SafeTemporarilyInserted<
119121
K: 'static + Clone + Hash + Eq + Ord + Send + Sync,
120122
V: Send + Sync,
121123
> {
122-
inner: &'a DashMap<K, V>,
124+
inner: &'a FxDashMap<K, V>,
123125
key: &'a K,
124126
}
125127

turbopack/crates/turbo-tasks/src/registry.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
1-
use std::{
2-
fmt::Debug,
3-
hash::{BuildHasherDefault, Hash},
4-
num::NonZeroU64,
5-
ops::Deref,
6-
};
1+
use std::{fmt::Debug, hash::Hash, num::NonZeroU64, ops::Deref};
72

8-
use dashmap::{mapref::entry::Entry, DashMap};
3+
use dashmap::mapref::entry::Entry;
94
use once_cell::sync::Lazy;
10-
use rustc_hash::FxHasher;
115

126
use crate::{
137
id::{FunctionId, TraitTypeId, ValueTypeId},
148
id_factory::IdFactory,
159
native_function::NativeFunction,
1610
no_move_vec::NoMoveVec,
17-
TraitType, ValueType,
11+
FxDashMap, TraitType, ValueType,
1812
};
1913

20-
type FxDashMap<K, V> = DashMap<K, V, BuildHasherDefault<FxHasher>>;
21-
2214
static FUNCTION_ID_FACTORY: IdFactory<FunctionId> = IdFactory::new(1, u32::MAX as u64);
23-
static FUNCTIONS_BY_NAME: Lazy<FxDashMap<&'static str, FunctionId>> = Lazy::new(DashMap::default);
15+
static FUNCTIONS_BY_NAME: Lazy<FxDashMap<&'static str, FunctionId>> = Lazy::new(FxDashMap::default);
2416
static FUNCTIONS_BY_VALUE: Lazy<FxDashMap<&'static NativeFunction, FunctionId>> =
25-
Lazy::new(DashMap::default);
17+
Lazy::new(FxDashMap::default);
2618
static FUNCTIONS: Lazy<NoMoveVec<(&'static NativeFunction, &'static str)>> =
2719
Lazy::new(NoMoveVec::new);
2820

2921
static VALUE_TYPE_ID_FACTORY: IdFactory<ValueTypeId> = IdFactory::new(1, u32::MAX as u64);
3022
static VALUE_TYPES_BY_NAME: Lazy<FxDashMap<&'static str, ValueTypeId>> =
31-
Lazy::new(DashMap::default);
23+
Lazy::new(FxDashMap::default);
3224
static VALUE_TYPES_BY_VALUE: Lazy<FxDashMap<&'static ValueType, ValueTypeId>> =
33-
Lazy::new(DashMap::default);
25+
Lazy::new(FxDashMap::default);
3426
static VALUE_TYPES: Lazy<NoMoveVec<(&'static ValueType, &'static str)>> = Lazy::new(NoMoveVec::new);
3527

3628
static TRAIT_TYPE_ID_FACTORY: IdFactory<TraitTypeId> = IdFactory::new(1, u32::MAX as u64);
3729
static TRAIT_TYPES_BY_NAME: Lazy<FxDashMap<&'static str, TraitTypeId>> =
38-
Lazy::new(DashMap::default);
30+
Lazy::new(FxDashMap::default);
3931
static TRAIT_TYPES_BY_VALUE: Lazy<FxDashMap<&'static TraitType, TraitTypeId>> =
40-
Lazy::new(DashMap::default);
32+
Lazy::new(FxDashMap::default);
4133
static TRAIT_TYPES: Lazy<NoMoveVec<(&'static TraitType, &'static str)>> = Lazy::new(NoMoveVec::new);
4234

4335
fn register_thing<

turbopack/crates/turbo-tasks/src/task_statistics.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
use std::{
2-
hash::BuildHasherDefault,
3-
sync::{Arc, OnceLock},
4-
};
1+
use std::sync::{Arc, OnceLock};
52

6-
use dashmap::DashMap;
7-
use rustc_hash::FxHasher;
83
use serde::{ser::SerializeMap, Serialize, Serializer};
94

10-
use crate::{registry, FunctionId};
5+
use crate::{registry, FunctionId, FxDashMap};
116

127
/// An API for optionally enabling, updating, and reading aggregated statistics.
138
#[derive(Default)]
@@ -19,7 +14,7 @@ impl TaskStatisticsApi {
1914
pub fn enable(&self) -> &Arc<TaskStatistics> {
2015
self.inner.get_or_init(|| {
2116
Arc::new(TaskStatistics {
22-
inner: DashMap::with_hasher(Default::default()),
17+
inner: FxDashMap::with_hasher(Default::default()),
2318
})
2419
})
2520
}
@@ -43,7 +38,7 @@ impl TaskStatisticsApi {
4338

4439
/// A type representing the enabled state of [`TaskStatisticsApi`]. Implements [`serde::Serialize`].
4540
pub struct TaskStatistics {
46-
inner: DashMap<FunctionId, TaskFunctionStatistics, BuildHasherDefault<FxHasher>>,
41+
inner: FxDashMap<FunctionId, TaskFunctionStatistics>,
4742
}
4843

4944
impl TaskStatistics {

0 commit comments

Comments
 (0)