sharded 0.3.0

Safe, fast, and obvious concurrent collections
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! _**Note: This crate is still in early development and undergoing API changes.** Contributions,
//! feature requests, and constructive feedback are warmly welcomed._
//!
//! # sharded   ![Build] ![Crate]
//!
//! [Build]: https://github.com/nkconnor/sharded/workflows/build/badge.svg
//! [Crate]: https://img.shields.io/crates/v/sharded
//!
//! **Sharded provides safe, fast, and obvious concurrent collections in Rust**. This crate splits the
//! underlying collection into `N shards` each with its own lock.
//!
//! For further reading on the strategy, see a [write up on C++'s `parallel-hashmap`](https://greg7mdp.github.io/parallel-hashmap/).
//!
//! ## Features
//!
//! * **Zero unsafe code.** This library uses `#![forbid(unsafe_code)]` and was motivated by
//!     the complexity and amount of memory errors present in many alternatives.
//!
//! * **Tiny footprint.** The core logic is <100 lines of code. The two dependencies are
//!     `hashbrown` and `parking_lot`.
//!
//! * **Really fast.** This implementation may be a more performant choice than some
//!     of the most popular concurrent hashmaps out there. Try it on your workload and let us know.
//!
//! ## See Also
//!
//! - **[contrie](https://crates.io/crates/contrie)** - A concurrent hash-trie map & set.
//! - **[dashmap](https://github.com/xacrimon/dashmap)** - Blazing fast concurrent HashMap for Rust.
//! - **[flurry](https://github.com/jonhoo/flurry)** - A port of Java's `java.util.concurrent.ConcurrentHashMap` to Rust.
//!
//! ## Quick Start
//!
//! ```toml
//! [dependencies]
//! sharded = "0.3"
//! ```
//!
//! ## Examples
//!
//! **Insert and retrieve values**
//!
//! ```
//! # use sharded::ConcurrentHashMap;
//! let users = ConcurrentHashMap::new();
//! users.insert(32, "Henry");
//! assert_eq!(&"Henry", users.get(32).unwrap());
//! ```
//!
//! ## Performance Comparison
//!
//! These measurements were generated using [`jonhoo/bustle`](https://github.com/jonhoo/bustle). To reproduce the charts,
//! see the `benchmarks` directory. Benchmarks can be misleading. It is recommended to benchmark using a real application
//! workload.
//!
//! ### Average Performance by Implementation
//!
//! This ran each implementation over the presets in [`bustle::Mix`](https://docs.rs/bustle/0.4.2/bustle/struct.Mix.html) for 5
//! iterations / random seeds using a Intel® Core™ i9-9820X. Lower numbers are better. Approaches using a single `std::sync` Lock and `chashmap` were discarded for clarity (they are
//! a lot slower). If you know why `chashmap` is so slow in this test, please help here.
//!
//! ![Read Heavy Performance)](benchmarks/avg_performance_read_heavy.png)
//! ![Write Heavy Performance)](benchmarks/avg_performance_write_heavy.png)
//! ![Update Heavy Performance)](benchmarks/avg_performance_update_heavy.png)
//! ![Uniform Performance)](benchmarks/avg_performance_uniform.png)
//!
//! ## Acknowledgements
//!
//! Many thanks to
//!
//! - [Reddit community](https://www.reddit.com/r/rust) for a few pointers and
//! some motivation to take this project further.
//!
//! - [Jon Gjengset](https://github.com/jonhoo) for the live streams and utility crates involved
//!
//! - and countless OSS contributors that made this work possible
//!
//! ## License
//!
//! Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
//! 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
//!
//! Unless you explicitly state otherwise, any contribution intentionally submitted
//! for inclusion in `sharded` by you, as defined in the Apache-2.0 license, shall be
//! dual licensed as above, without any additional terms or conditions.
#![forbid(unsafe_code)]

use hashbrown::raw::{RawIntoIter, RawTable};
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
use std::borrow::Borrow;
use std::convert::TryInto;
use std::hash::{BuildHasher, Hash};
use std::{fmt, fmt::Debug};

use std::collections::hash_map::RandomState;

/// Number of shards
const DEFAULT_SHARD_COUNT: usize = 128;

// From hashbrown
// Ensures that a single closure type across uses of this which, in turn prevents multiple
// instances of any functions like RawTable::reserve from being generated
#[inline]
fn equivalent_key<K, V>(k: &K) -> impl Fn(&(K, V)) -> bool + '_
where
    K: Eq,
{
    move |x| k.eq(x.0.borrow())
}

// From hashbrown
#[inline]
fn make_hash<K, S>(hash_builder: &S, val: &K) -> u64
where
    K: Hash,
    S: BuildHasher,
{
    hash_builder.hash_one(val)
}

// From hashbrown
// Ensures that a single closure type across uses of this which, in turn prevents multiple
// instances of any functions like RawTable::reserve from being generated
#[inline]
fn make_hasher<K, V, S>(hash_builder: &S) -> impl Fn(&(K, V)) -> u64 + '_
where
    K: Hash,
    S: BuildHasher,
{
    move |val| make_hash::<K, S>(hash_builder, &val.0)
}

/// A concurrent lock-based `HashMap` based on `hashbrown` and `parking_lot`.
pub struct ConcurrentHashMap<K, V, S = RandomState, const N: usize = DEFAULT_SHARD_COUNT> {
    hash_builder: S,
    shards: [RwLock<Shard<K, V, S>>; N],
}

impl<K, V> ConcurrentHashMap<K, V, RandomState, DEFAULT_SHARD_COUNT> {
    /// Creates an empty `ConcurrentHashMap`.
    ///
    /// The hash map is initially created with a capacity of 0, so it will not allocate until it
    /// is first inserted into.
    ///
    /// # Examples
    ///
    /// ```
    /// use sharded::ConcurrentHashMap;
    /// let mut map: ConcurrentHashMap<&str, i32> = ConcurrentHashMap::new();
    /// ```
    #[must_use]
    pub fn new() -> ConcurrentHashMap<K, V, RandomState> {
        Default::default()
    }

    /// Creates an empty `ConcurrentHashMap` with the specified capacity.
    ///
    /// # Examples
    ///
    /// ```
    /// use sharded::ConcurrentHashMap;
    /// let mut map: ConcurrentHashMap<&str, i32> = ConcurrentHashMap::with_capacity(100_000);
    /// ```
    #[inline]
    #[must_use]
    pub fn with_capacity(
        capacity: usize,
    ) -> ConcurrentHashMap<K, V, RandomState, DEFAULT_SHARD_COUNT> {
        ConcurrentHashMap::<_, _, _, DEFAULT_SHARD_COUNT>::with_capacity_and_hasher(
            capacity,
            RandomState::default(),
        )
    }
}

impl<K, V, S: BuildHasher, const N: usize> ConcurrentHashMap<K, V, S, N> {
    /// Creates an empty `ConcurrentHashMap` which will use the given hash builder to hash
    /// keys.
    ///
    /// The created map has the default initial capacity.
    ///
    /// Warning: `hash_builder` is normally randomly generated, and
    /// is designed to allow HashMaps to be resistant to attacks that
    /// cause many collisions and very poor performance. Setting it
    /// manually using this function can expose a DoS attack vector.
    ///
    /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
    /// the HashMap to be useful, see its documentation for details.
    ///
    /// # Examples
    ///
    /// ```
    /// use sharded::ConcurrentHashMap;
    /// use std::collections::hash_map::RandomState;
    ///
    /// let mut map = ConcurrentHashMap::with_hasher(RandomState::new());
    /// map.insert(1, 2);
    /// ```
    #[inline]
    pub fn with_hasher(hash_builder: S) -> ConcurrentHashMap<K, V, S, N>
    where
        S: Clone,
    {
        ConcurrentHashMap::<_, _, _, N>::with_capacity_and_hasher(0, hash_builder)
    }

    /// Creates an empty `ConcurrentHashMap` with the specified capacity, using `hash_builder`
    /// to hash the keys.
    ///
    /// The hash map will be able to hold approximately `capacity` elements without
    /// reallocating. If `capacity` is 0, the hash map will not allocate.
    ///
    /// Warning: `hash_builder` is normally randomly generated, and
    /// is designed to allow HashMaps to be resistant to attacks that
    /// cause many collisions and very poor performance. Setting it
    /// manually using this function can expose a DoS attack vector.
    ///
    /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
    /// the HashMap to be useful, see its documentation for details.
    ///
    /// # Examples
    ///
    /// ```
    /// use sharded::ConcurrentHashMap;
    /// use std::collections::hash_map::RandomState;
    ///
    /// let s = RandomState::new();
    /// let mut map = ConcurrentHashMap::with_capacity_and_hasher(10, s);
    /// map.insert(1, 2);
    /// ```
    pub fn with_capacity_and_hasher(
        capacity: usize,
        hash_builder: S,
    ) -> ConcurrentHashMap<K, V, S, N>
    where
        S: Clone,
    {
        // per shard capacity
        let capacity = (capacity + DEFAULT_SHARD_COUNT - 1) / DEFAULT_SHARD_COUNT;

        let shards: Vec<RwLock<Shard<K, V, S>>> =
            std::iter::repeat(|| RawTable::with_capacity(capacity))
                .map(|f| f())
                .take(DEFAULT_SHARD_COUNT)
                .map(|inner| {
                    RwLock::new(Shard {
                        inner,
                        hash_builder: hash_builder.clone(),
                    })
                })
                .collect::<Vec<_>>();

        match shards.try_into() {
            Ok(shards) => ConcurrentHashMap {
                hash_builder,
                shards,
            },
            // .unwrap() requires Debug
            // this never panics because the iter takes exactly DEFAULT_SHARD_COUNT
            Err(_) => panic!("unable to build inner"),
        }
    }

    /// Returns the approximate number of elements the map can hold without reallocating.
    ///
    /// **Locks** - Acquires a read lock on one of `N` shards.
    ///
    /// # Examples
    ///
    /// ```
    /// use sharded::ConcurrentHashMap;
    /// let map: ConcurrentHashMap<i32, i32> = ConcurrentHashMap::with_capacity(100);
    /// assert!(map.capacity() >= 100);
    /// ```
    #[inline]
    pub fn capacity(&self) -> usize {
        self.shards.first().unwrap().read().inner.capacity()
    }

    /// Returns a guarded reference for the value corresponding to the
    /// provided key.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// # Examples
    ///
    /// ```
    /// use sharded::ConcurrentHashMap;
    ///
    /// let mut map = ConcurrentHashMap::new();
    /// map.insert(1, "a");
    /// assert_eq!(map.get(&1), Some(&"a"));
    /// assert_eq!(map.get(&2), None);
    /// ```
    #[inline]
    pub fn get<'a>(&'a self, key: &'a K) -> Option<MappedRwLockReadGuard<'_, V>>
    where
        K: Hash + Eq,
    {
        let hash = make_hash::<K, _>(&self.hash_builder, key);

        let i = hash as usize % N;

        let shard = match self.shards.get(i) {
            Some(lock) => lock.read(),
            None => panic!("index out of bounds"),
        };

        RwLockReadGuard::try_map(shard, |shard| {
            match shard.inner.get(hash, equivalent_key(key)) {
                Some((_, v)) => Some(v),
                _ => None,
            }
        })
        .ok()
    }

    /// Insert a key value pair into the Map. Returns the existing
    /// value at the provided key if there was one.
    #[inline]
    pub fn insert(&self, k: K, v: V) -> Option<V>
    where
        K: Hash + Eq,
    {
        let hash = make_hash::<K, _>(&self.hash_builder, &k);

        let i = hash as usize % N;

        let mut shard = match self.shards.get(i) {
            Some(lock) => lock.write(),
            None => panic!("index out of bounds"),
        };

        shard.insert(hash, k, v)
    }
}

impl<K, V, S, const N: usize> Default for ConcurrentHashMap<K, V, S, N>
where
    S: Default + BuildHasher + Clone,
{
    /// Creates an empty `ConcurrentHashMap<K, V, S, N>`, with the `Default` value for the hasher
    /// and 128 for the Shard Count.
    #[inline]
    fn default() -> ConcurrentHashMap<K, V, S, N> {
        if N == 0 {
            panic!("number of shards must be > 0")
        }
        ConcurrentHashMap::<K, V, S, N>::with_hasher(Default::default())
    }
}

/// An owning iterator over the entries of a `ConcurrentHashMap`.
///
/// This `struct` is created by the [`into_iter`] method on [`ConcurrentHashMap`]
/// (provided by the [`IntoIterator`] trait). See its documentation for more.
///
/// [`into_iter`]: IntoIterator::into_iter
/// [`IntoIterator`]: crate::iter::IntoIterator
///
/// # Example
///
/// ```
/// use sharded::ConcurrentHashMap;
///
/// let map = ConcurrentHashMap::from([
///     ("a", 1),
/// ]);
/// let iter = map.into_iter();
/// ```
pub struct IntoIter<K: 'static, V: 'static> {
    iter: RawIntoIter<(K, V)>,
    shards: Vec<Shard<K, V>>,
}

pub struct IntoValues<K: 'static, V: 'static> {
    iter: IntoIter<K, V>,
}

impl<K, V> Iterator for IntoIter<K, V> {
    type Item = (K, V);

    #[inline]
    fn next(&mut self) -> Option<(K, V)> {
        match self.iter.next() {
            Some(item) => Some(item),
            None => match self.shards.pop() {
                Some(s) => {
                    self.iter = s.inner.into_iter();
                    self.next()
                }
                None => None,
            },
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.iter.size_hint().0, None)
    }
}

impl<K, V> Iterator for IntoValues<K, V> {
    type Item = V;

    #[inline]
    fn next(&mut self) -> Option<V> {
        self.iter.next().map(|(_, v)| v)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.iter.size_hint().0, None)
    }
}

/// A single shard in the map
#[derive(Clone)]
pub(crate) struct Shard<K, V, S = RandomState> {
    hash_builder: S,
    inner: RawTable<(K, V)>,
}

impl<K, V> Debug for Shard<K, V>
where
    K: Debug,
    V: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("shard").finish()
    }
}

#[allow(dead_code)]
impl<K, V, S> Shard<K, V, S>
where
    S: BuildHasher,
{
    /// Number of items in the shard
    #[inline]
    pub(crate) fn len(&self) -> usize {
        self.inner.len()
    }

    /// Is `len == 0`
    #[inline]
    pub(crate) fn is_empty(&self) -> bool {
        self.inner.len() == 0
    }

    /// Remove the key, returning the value at that position if it existed
    #[inline]
    pub(crate) fn remove(&mut self, hash: u64, key: K) -> Option<V>
    where
        K: Hash + Eq,
    {
        #[allow(clippy::manual_map)] // reduce compiler IR, I think!
        match self.inner.remove_entry(hash, equivalent_key(&key)) {
            Some((_, v)) => Some(v),
            None => None,
        }
    }

    /// Get mutable value for the provided key
    #[inline]
    pub(crate) fn get_mut(&mut self, hash: u64, key: &K) -> Option<&mut V>
    where
        K: Hash + Eq,
    {
        match self.inner.get_mut(hash, equivalent_key(key)) {
            Some(&mut (_, ref mut v)) => Some(v),
            None => None,
        }
    }

    /// Insert the key value pair
    #[inline]
    pub(crate) fn insert(&mut self, hash: u64, key: K, v: V) -> Option<V>
    where
        K: Hash + Eq,
    {
        if let Some((_, item)) = self.inner.get_mut(hash, equivalent_key(&key)) {
            Some(std::mem::replace(item, v))
        } else {
            self.inner
                .insert(hash, (key, v), make_hasher::<K, V, S>(&self.hash_builder));
            None
        }
    }

    /// Get the value for the key if it exists
    #[inline]
    pub(crate) fn get(&self, hash: u64, key: &K) -> Option<&V>
    where
        K: Hash + Eq,
    {
        match self.inner.get(hash, equivalent_key(key)) {
            Some((_, v)) => Some(v),
            None => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::time::Duration;

    #[test]
    fn test_insert_values() {
        let map = ConcurrentHashMap::new();
        {
            map.insert("k", "v");
        }
        assert_eq!(*map.get(&"k").unwrap(), "v");
    }

    #[test]
    fn test_other_deadlock() {
        let map_1 = Arc::new(ConcurrentHashMap::<i32, String>::default());
        let map_2 = map_1.clone();

        for i in 0..1000 {
            map_1.insert(i, "foobar".to_string());
        }

        let _writer = std::thread::spawn(move || loop {
            println!("writer iteration");
            for i in 0..1000 {
                map_1.insert(i, "foobaz".to_string());
            }
        });

        let _reader = std::thread::spawn(move || loop {
            println!("reader iteration");
            for i in 0..1000 {
                let j = i32::min(i + 100, 1000);
                let rng: Vec<i32> = (i..j).collect();
                let _v: Vec<_> = rng.iter().map(|k| map_2.get(k)).collect();
            }
        });

        std::thread::sleep(Duration::from_secs(10));
    }
}