forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharded.rs
316 lines (266 loc) · 8.76 KB
/
sharded.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use crate::fx::{FxHashMap, FxHasher};
use crate::sync::{DynSync, LockLike};
use parking_lot::{Mutex, MutexGuard};
use std::borrow::Borrow;
use std::cell::{RefCell, RefMut};
use std::collections::hash_map::RawEntryMut;
use std::hash::{Hash, Hasher};
use std::mem;
pub trait Shard {
type Impl<T>: ShardImpl<T>;
}
pub trait ShardImpl<T> {
type Lock: LockLike<T>;
fn new(value: impl FnMut() -> T) -> Self;
fn get_shard_by_value<K: Hash + ?Sized>(&self, _val: &K) -> &Self::Lock;
fn get_shard_by_hash(&self, _hash: u64) -> &Self::Lock;
fn lock_shards(&self) -> Vec<<Self::Lock as LockLike<T>>::LockGuard<'_>>;
fn try_lock_shards(&self) -> Option<Vec<<Self::Lock as LockLike<T>>::LockGuard<'_>>>;
}
#[derive(Default)]
pub struct SingleShard;
impl Shard for SingleShard {
type Impl<T> = SingleShardImpl<T>;
}
/// An array of cache-line aligned inner locked structures with convenience methods.
pub struct SingleShardImpl<T> {
shard: RefCell<T>,
}
impl<T: Default> Default for SingleShardImpl<T> {
#[inline]
fn default() -> Self {
Self { shard: RefCell::new(T::default()) }
}
}
impl<T> ShardImpl<T> for SingleShardImpl<T> {
type Lock = RefCell<T>;
#[inline]
fn new(mut value: impl FnMut() -> T) -> Self {
SingleShardImpl { shard: RefCell::new(value()) }
}
#[inline]
fn get_shard_by_value<K: Hash + ?Sized>(&self, _val: &K) -> &RefCell<T> {
&self.shard
}
#[inline]
fn get_shard_by_hash(&self, _hash: u64) -> &RefCell<T> {
&self.shard
}
fn lock_shards(&self) -> Vec<RefMut<'_, T>> {
vec![self.shard.lock()]
}
fn try_lock_shards(&self) -> Option<Vec<RefMut<'_, T>>> {
Some(vec![self.shard.try_lock()?])
}
}
const SHARD_BITS: usize = 5;
pub const SHARDS: usize = 1 << SHARD_BITS;
#[derive(Default)]
pub struct Sharded;
impl Shard for Sharded {
type Impl<T> = ShardedImpl<T>;
}
#[derive(Default)]
#[repr(align(64))]
pub struct CacheAligned<T>(pub T);
pub struct ShardedImpl<T> {
shards: [CacheAligned<Mutex<T>>; SHARDS],
}
impl<T: Default> Default for ShardedImpl<T> {
#[inline]
fn default() -> Self {
Self::new(T::default)
}
}
impl<T> ShardImpl<T> for ShardedImpl<T> {
type Lock = Mutex<T>;
#[inline]
fn new(mut value: impl FnMut() -> T) -> Self {
ShardedImpl { shards: [(); SHARDS].map(|()| CacheAligned(Mutex::new(value()))) }
}
/// The shard is selected by hashing `val` with `FxHasher`.
#[inline]
fn get_shard_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Mutex<T> {
self.get_shard_by_hash(make_hash(val))
}
#[inline]
fn get_shard_by_hash(&self, hash: u64) -> &Mutex<T> {
&self.shards[get_shard_index_by_hash(hash)].0
}
fn lock_shards(&self) -> Vec<MutexGuard<'_, T>> {
(0..SHARDS).map(|i| self.shards[i].0.lock()).collect()
}
fn try_lock_shards(&self) -> Option<Vec<MutexGuard<'_, T>>> {
(0..SHARDS).map(|i| self.shards[i].0.try_lock()).collect()
}
}
pub struct DynSharded<T> {
single_thread: bool,
single_shard: RefCell<T>,
parallel_shard: ShardedImpl<T>,
}
#[cfg(parallel_compiler)]
unsafe impl<T> DynSync for DynSharded<T> {}
impl<T: Default> Default for DynSharded<T> {
#[inline]
fn default() -> Self {
let single_thread = !crate::sync::is_dyn_thread_safe();
DynSharded {
single_thread,
single_shard: RefCell::new(T::default()),
parallel_shard: ShardedImpl::default(),
}
}
}
impl<T: Default> DynSharded<T> {
pub fn new(mut value: impl FnMut() -> T) -> Self {
if !crate::sync::is_dyn_thread_safe() {
DynSharded {
single_thread: true,
single_shard: RefCell::new(value()),
parallel_shard: ShardedImpl::default(),
}
} else {
DynSharded {
single_thread: false,
single_shard: RefCell::new(T::default()),
parallel_shard: ShardedImpl::new(value),
}
}
}
/// The shard is selected by hashing `val` with `FxHasher`.
#[inline]
pub fn with_get_shard_by_value<K: Hash + ?Sized, F: FnOnce(&mut T) -> R, R>(
&self,
val: &K,
f: F,
) -> R {
if self.single_thread {
let mut lock = self.single_shard.borrow_mut();
f(&mut *lock)
} else {
let mut lock = self.parallel_shard.get_shard_by_value(val).lock();
f(&mut *lock)
}
}
#[inline]
pub fn with_get_shard_by_hash<F: FnOnce(&mut T) -> R, R>(&self, hash: u64, f: F) -> R {
if self.single_thread {
let mut lock = self.single_shard.borrow_mut();
f(&mut *lock)
} else {
let mut lock = self.parallel_shard.get_shard_by_hash(hash).lock();
f(&mut *lock)
}
}
#[inline]
pub fn with_lock_shards<F: FnMut(&mut T) -> R, R>(&self, mut f: F) -> Vec<R> {
if self.single_thread {
let mut lock = self.single_shard.borrow_mut();
vec![f(&mut *lock)]
} else {
(0..SHARDS).map(|i| f(&mut *self.parallel_shard.shards[i].0.lock())).collect()
}
}
#[inline]
pub fn with_try_lock_shards<F: FnMut(&mut T) -> R, R>(&self, mut f: F) -> Option<Vec<R>> {
if self.single_thread {
let mut lock = self.single_shard.try_borrow_mut().ok()?;
Some(vec![f(&mut *lock)])
} else {
(0..SHARDS)
.map(|i| {
let mut shard = self.parallel_shard.shards[i].0.try_lock()?;
Some(f(&mut *shard))
})
.collect()
}
}
#[inline]
pub fn get_lock_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Mutex<T> {
self.parallel_shard.get_shard_by_value(val)
}
#[inline]
pub fn get_borrow_by_value<K: Hash + ?Sized>(&self, _val: &K) -> &RefCell<T> {
&self.single_shard
}
}
pub type ShardedHashMap<K, V> = DynSharded<FxHashMap<K, V>>;
impl<K: Eq, V> ShardedHashMap<K, V> {
pub fn len(&self) -> usize {
self.with_lock_shards(|shard| shard.len()).into_iter().sum()
}
}
impl<K: Eq + Hash + Copy> ShardedHashMap<K, ()> {
#[inline]
pub fn intern_ref<Q: ?Sized>(&self, value: &Q, make: impl FnOnce() -> K) -> K
where
K: Borrow<Q>,
Q: Hash + Eq,
{
let hash = make_hash(value);
self.with_get_shard_by_hash(hash, |shard| {
let entry = shard.raw_entry_mut().from_key_hashed_nocheck(hash, value);
match entry {
RawEntryMut::Occupied(e) => *e.key(),
RawEntryMut::Vacant(e) => {
let v = make();
e.insert_hashed_nocheck(hash, v, ());
v
}
}
})
}
#[inline]
pub fn intern<Q>(&self, value: Q, make: impl FnOnce(Q) -> K) -> K
where
K: Borrow<Q>,
Q: Hash + Eq,
{
let hash = make_hash(&value);
self.with_get_shard_by_hash(hash, |shard| {
let entry = shard.raw_entry_mut().from_key_hashed_nocheck(hash, &value);
match entry {
RawEntryMut::Occupied(e) => *e.key(),
RawEntryMut::Vacant(e) => {
let v = make(value);
e.insert_hashed_nocheck(hash, v, ());
v
}
}
})
}
}
pub trait IntoPointer {
/// Returns a pointer which outlives `self`.
fn into_pointer(&self) -> *const ();
}
impl<K: Eq + Hash + Copy + IntoPointer> ShardedHashMap<K, ()> {
pub fn contains_pointer_to<T: Hash + IntoPointer>(&self, value: &T) -> bool {
let hash = make_hash(&value);
self.with_get_shard_by_hash(hash, |shard| {
let value = value.into_pointer();
shard.raw_entry().from_hash(hash, |entry| entry.into_pointer() == value).is_some()
})
}
}
#[inline]
pub fn make_hash<K: Hash + ?Sized>(val: &K) -> u64 {
let mut state = FxHasher::default();
val.hash(&mut state);
state.finish()
}
/// Get a shard with a pre-computed hash value. If `get_shard_by_value` is
/// ever used in combination with `get_shard_by_hash` on a single `Sharded`
/// instance, then `hash` must be computed with `FxHasher`. Otherwise,
/// `hash` can be computed with any hasher, so long as that hasher is used
/// consistently for each `Sharded` instance.
#[inline]
#[allow(clippy::modulo_one)]
pub fn get_shard_index_by_hash(hash: u64) -> usize {
let hash_len = mem::size_of::<usize>();
// Ignore the top 7 bits as hashbrown uses these and get the next SHARD_BITS highest bits.
// hashbrown also uses the lowest bits, so we can't use those
let bits = (hash >> (hash_len * 8 - 7 - SHARD_BITS)) as usize;
bits % SHARDS
}