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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
//! **This module is experimental**
//!
//! This module provides threadsafe versions of FrozenMap and FrozenVec,
//! ideal for use as a cache.
//!
//! These lock internally, however locks only last as long as the method calls
//!
use stable_deref_trait::StableDeref;
use std::alloc::Layout;
use std::borrow::Borrow;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::hash::Hash;
use std::iter::{FromIterator, IntoIterator};
use std::mem::MaybeUninit;
use std::ops::Index;
use std::sync::atomic::AtomicPtr;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::RwLock;
/// Append-only threadsafe version of `std::collections::HashMap` where
/// insertion does not require mutable access
pub struct FrozenMap<K, V> {
map: RwLock<HashMap<K, V>>,
}
impl<K, V> Default for FrozenMap<K, V> {
fn default() -> Self {
Self {
map: Default::default(),
}
}
}
impl<K: Eq + Hash, V: StableDeref> FrozenMap<K, V> {
// these should never return &K or &V
// these should never delete any entries
pub fn new() -> Self {
Self::default()
}
/// If the key exists in the map, returns a reference
/// to the corresponding value, otherwise inserts a
/// new entry in the map for that key and returns a
/// reference to the given value.
///
/// Existing values are never overwritten.
///
/// 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 elsa::sync::FrozenMap;
///
/// let map = FrozenMap::new();
/// assert_eq!(map.insert(1, Box::new("a")), &"a");
/// assert_eq!(map.insert(1, Box::new("b")), &"a");
/// ```
pub fn insert(&self, k: K, v: V) -> &V::Target {
let mut map = self.map.write().unwrap();
let ret = unsafe {
let inserted = &**map.entry(k).or_insert(v);
&*(inserted as *const _)
};
ret
}
/// If the key exists in the map, returns a reference to the corresponding
/// value, otherwise inserts a new entry in the map for that key and the
/// value returned by the creation function, and returns a reference to the
/// generated value.
///
/// Existing values are never overwritten.
///
/// 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.
///
/// **Note** that the write lock is held for the duration of this function’s
/// execution, even while the value creation function is executing (if
/// needed). This will block any concurrent `get` or `insert` calls.
///
/// # Examples
///
/// ```
/// use elsa::sync::FrozenMap;
///
/// let map = FrozenMap::new();
/// assert_eq!(map.insert_with(1, || Box::new("a")), &"a");
/// assert_eq!(map.insert_with(1, || unreachable!()), &"a");
/// ```
pub fn insert_with(&self, k: K, f: impl FnOnce() -> V) -> &V::Target {
let mut map = self.map.write().unwrap();
let ret = unsafe {
let inserted = &**map.entry(k).or_insert_with(f);
&*(inserted as *const _)
};
ret
}
/// If the key exists in the map, returns a reference to the corresponding
/// value, otherwise inserts a new entry in the map for that key and the
/// value returned by the creation function, and returns a reference to the
/// generated value.
///
/// Existing values are never overwritten.
///
/// 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.
///
/// **Note** that the write lock is held for the duration of this function’s
/// execution, even while the value creation function is executing (if
/// needed). This will block any concurrent `get` or `insert` calls.
///
/// # Examples
///
/// ```
/// use elsa::sync::FrozenMap;
///
/// let map = FrozenMap::new();
/// assert_eq!(map.insert_with_key(1, |_| Box::new("a")), &"a");
/// assert_eq!(map.insert_with_key(1, |_| unreachable!()), &"a");
/// ```
pub fn insert_with_key(&self, k: K, f: impl FnOnce(&K) -> V) -> &V::Target {
let mut map = self.map.write().unwrap();
let ret = unsafe {
let inserted = &**map.entry(k).or_insert_with_key(f);
&*(inserted as *const _)
};
ret
}
/// Returns a reference to the value corresponding to the 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 elsa::sync::FrozenMap;
///
/// let map = FrozenMap::new();
/// map.insert(1, Box::new("a"));
/// assert_eq!(map.get(&1), Some(&"a"));
/// assert_eq!(map.get(&2), None);
/// ```
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V::Target>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
let map = self.map.read().unwrap();
let ret = unsafe { map.get(k).map(|x| &*(&**x as *const V::Target)) };
ret
}
/// Applies a function to the owner of the value corresponding to the key (if any).
///
/// 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 elsa::sync::FrozenMap;
///
/// let map = FrozenMap::new();
/// map.insert(1, Box::new("a"));
/// assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a")));
/// assert_eq!(map.map_get(&2, Clone::clone), None);
/// ```
pub fn map_get<Q: ?Sized, T, F>(&self, k: &Q, f: F) -> Option<T>
where
K: Borrow<Q>,
Q: Hash + Eq,
F: FnOnce(&V) -> T,
{
let map = self.map.read().unwrap();
let ret = map.get(k).map(f);
ret
}
/// # Examples
///
/// ```
/// use elsa::sync::FrozenMap;
///
/// let map = FrozenMap::new();
/// assert_eq!(map.len(), 0);
/// map.insert(1, Box::new("a"));
/// assert_eq!(map.len(), 1);
/// ```
pub fn len(&self) -> usize {
let map = self.map.read().unwrap();
map.len()
}
/// # Examples
///
/// ```
/// use elsa::sync::FrozenMap;
///
/// let map = FrozenMap::new();
/// assert_eq!(map.is_empty(), true);
/// map.insert(1, Box::new("a"));
/// assert_eq!(map.is_empty(), false);
/// ```
pub fn is_empty(&self) -> bool {
let map = self.map.read().unwrap();
map.is_empty()
}
// TODO add more
}
/// Append-only threadsafe version of `std::vec::Vec` where
/// insertion does not require mutable access
pub struct FrozenVec<T> {
vec: RwLock<Vec<T>>,
}
impl<T> Default for FrozenVec<T> {
fn default() -> Self {
Self {
vec: Default::default(),
}
}
}
impl<T: StableDeref> FrozenVec<T> {
pub fn new() -> Self {
Default::default()
}
// these should never return &T
// these should never delete any entries
pub fn push(&self, val: T) {
let mut vec = self.vec.write().unwrap();
vec.push(val);
}
/// Push, immediately getting a reference to the element
pub fn push_get(&self, val: T) -> &T::Target {
let mut vec = self.vec.write().unwrap();
vec.push(val);
unsafe { &*(&**vec.get_unchecked(vec.len() - 1) as *const T::Target) }
}
/// Push, immediately getting a an index of the element
///
/// Index can then be used with the `get` method
///
/// # Examples
///
/// ```
/// use elsa::sync::FrozenVec;
///
/// let map = FrozenVec::new();
/// let idx = map.push_get_index(String::from("a"));
/// assert_eq!(map.get(idx), Some("a"));
/// assert_eq!(idx, 0);
/// assert_eq!(map.push_get_index(String::from("b")), 1);
/// ```
pub fn push_get_index(&self, val: T) -> usize {
let mut vec = self.vec.write().unwrap();
let index = vec.len();
vec.push(val);
return index;
}
pub fn get(&self, index: usize) -> Option<&T::Target> {
let vec = self.vec.read().unwrap();
unsafe { vec.get(index).map(|x| &*(&**x as *const T::Target)) }
}
// TODO add more
}
/// Append-only threadsafe version of `std::vec::Vec` where
/// insertion does not require mutable access.
/// Does not have locks, only allows `Copy` types and will
/// spinlock on contention. The spinlocks are really rare as
/// they only happen on reallocation due to a push going over
/// the capacity.
pub struct LockFreeFrozenVec<T: Copy> {
data: AtomicPtr<T>,
len: AtomicUsize,
cap: AtomicUsize,
}
impl<T: Copy> Drop for LockFreeFrozenVec<T> {
fn drop(&mut self) {
let cap = *self.cap.get_mut();
let layout = self.layout(cap);
unsafe {
std::alloc::dealloc((*self.data.get_mut()).cast(), layout);
}
}
}
impl<T: Copy> Default for LockFreeFrozenVec<T> {
fn default() -> Self {
Self {
// FIXME: use `std::ptr::invalid_mut()` once that is stable.
data: AtomicPtr::new(std::mem::align_of::<T>() as *mut T),
len: AtomicUsize::new(0),
cap: AtomicUsize::new(0),
}
}
}
impl<T: Copy> LockFreeFrozenVec<T> {
pub fn new() -> Self {
Default::default()
}
pub fn with_capacity(cap: usize) -> Self {
Self {
data: AtomicPtr::new(
Box::into_raw(vec![MaybeUninit::<T>::uninit(); cap].into_boxed_slice()).cast(),
),
len: AtomicUsize::new(0),
cap: AtomicUsize::new(cap),
}
}
fn lock<U>(&self, f: impl FnOnce(&mut *mut T) -> U) -> U {
let mut ptr;
loop {
ptr = self.data.swap(std::ptr::null_mut(), Ordering::Acquire);
if !ptr.is_null() {
// Wheeeee spinlock
break;
}
}
let ret = f(&mut ptr);
self.data.store(ptr, Ordering::Release);
ret
}
fn layout(&self, cap: usize) -> Layout {
let num_bytes = std::mem::size_of::<T>() * cap;
let align = std::mem::align_of::<T>();
Layout::from_size_align(num_bytes, align).unwrap()
}
// these should never return &T
// these should never delete any entries
const NOT_ZST: () = if std::mem::size_of::<T>() == 0 {
panic!("`LockFreeFrozenVec` cannot be used with ZSTs");
};
pub fn push(&self, val: T) -> usize {
// This statement actually does something: it evaluates a constant.
#[allow(path_statements)]
{
Self::NOT_ZST
}
self.lock(|ptr| {
// These values must be consistent with the pointer we got.
let len = self.len.load(Ordering::Acquire);
let cap = self.cap.load(Ordering::Acquire);
if len >= cap {
if cap == 0 {
// No memory allocated yet
let layout = self.layout(128);
// SAFETY: `LockFreeFrozenVec` statically rejects zsts
unsafe {
*ptr = std::alloc::alloc(layout).cast::<T>();
}
// This is written before the end of the `lock` closure, so no one will observe this
// until the data pointer has been updated anyway.
self.cap.store(128, Ordering::Release);
} else {
// Out of memory, realloc with double the capacity
let layout = self.layout(cap);
let new_size = layout.size() * 2;
// SAFETY: `LockFreeFrozenVec` statically rejects zsts and the input `ptr` has always been
// allocated at the size stated in `cap`.
unsafe {
*ptr = std::alloc::realloc((*ptr).cast(), layout, new_size).cast::<T>();
}
// This is written before the end of the `lock` closure, so no one will observe this
// until the data pointer has been updated anyway.
self.cap.store(cap * 2, Ordering::Release);
}
assert!(!ptr.is_null());
}
unsafe {
ptr.add(len).write(val);
}
// This is written before updating the data pointer. Other `push` calls cannot observe this,
// because they are blocked on aquiring the data pointer before they ever read the `len`.
// `get` may read the length before actually aquiring the data pointer lock, but that is fine,
// as once it is able to aquire the lock, there will be actually the right number of elements
// stored.
self.len.store(len + 1, Ordering::Release);
len
})
}
pub fn get(&self, index: usize) -> Option<T> {
// The length can only grow, so just doing the length check
// independently of the `lock` and read is fine. Worst case we
// read an old length value and end up returning `None` even if
// another thread already inserted the value.
let len = self.len.load(Ordering::Relaxed);
if index >= len {
return None;
}
self.lock(|ptr| Some(unsafe { ptr.add(index).read() }))
}
}
#[test]
fn test_non_lockfree() {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
struct Moo(i32);
for vec in [
LockFreeFrozenVec::new(),
LockFreeFrozenVec::with_capacity(1),
LockFreeFrozenVec::with_capacity(2),
LockFreeFrozenVec::with_capacity(1000),
] {
assert_eq!(vec.get(1), None);
vec.push(Moo(1));
let i = vec.push(Moo(2));
vec.push(Moo(3));
assert_eq!(vec.get(i), Some(Moo(2)));
std::thread::scope(|s| {
s.spawn(|| {
for i in 0..1000 {
vec.push(Moo(i));
}
});
s.spawn(|| {
for i in 0..1000 {
vec.push(Moo(i));
}
});
for i in 0..2000 {
while vec.get(i).is_none() {}
}
});
}
}
/// Append-only threadsafe version of `std::collections::BTreeMap` where
/// insertion does not require mutable access
#[derive(Debug)]
pub struct FrozenBTreeMap<K, V>(RwLock<BTreeMap<K, V>>);
impl<K: Clone + Ord, V: StableDeref> FrozenBTreeMap<K, V> {
pub fn new() -> Self {
Self(RwLock::new(BTreeMap::new()))
}
// these should never return &K or &V
// these should never delete any entries
/// Returns a reference to the value corresponding to the key.
///
/// The key may be any borrowed form of the map's key type, but
/// [`Ord`] on the borrowed form *must* match those for
/// the key type.
///
/// # Examples
///
/// ```
/// use elsa::sync::FrozenBTreeMap;
///
/// let map = FrozenBTreeMap::new();
/// map.insert(1, Box::new("a"));
/// assert_eq!(map.get(&1), Some(&"a"));
/// assert_eq!(map.get(&2), None);
/// ```
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V::Target>
where
K: Borrow<Q>,
Q: Ord,
{
let map = self.0.read().unwrap();
let ret = unsafe { map.get(k).map(|x| &*(&**x as *const V::Target)) };
ret
}
/// Insert a new value into the map. Does nothing if the key is already occupied.
///
/// # Examples
///
/// ```
/// use elsa::sync::FrozenBTreeMap;
///
/// let map = FrozenBTreeMap::new();
/// map.insert(1, Box::new("a"));
/// assert_eq!(map.get(&1), Some(&"a"));
/// ```
pub fn insert(&self, k: K, v: V) -> &V::Target {
let mut map = self.0.write().unwrap();
let ret = unsafe {
let inserted = &**map.entry(k).or_insert(v);
&*(inserted as *const _)
};
ret
}
/// Applies a function to the owner of the value corresponding to the key (if any).
///
/// The key may be any borrowed form of the map's key type, but
/// [`Ord`] on the borrowed form *must* match those for
/// the key type.
///
/// # Examples
///
/// ```
/// use elsa::sync::FrozenBTreeMap;
///
/// let map = FrozenBTreeMap::new();
/// map.insert(1, Box::new("a"));
/// assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a")));
/// assert_eq!(map.map_get(&2, Clone::clone), None);
/// ```
pub fn map_get<Q: ?Sized, T, F>(&self, k: &Q, f: F) -> Option<T>
where
K: Borrow<Q>,
Q: Ord,
F: FnOnce(&V) -> T,
{
let map = self.0.read().unwrap();
let ret = map.get(k).map(f);
ret
}
/// # Examples
///
/// ```
/// use elsa::sync::FrozenBTreeMap;
///
/// let map = FrozenBTreeMap::new();
/// assert_eq!(map.len(), 0);
/// map.insert(1, Box::new("a"));
/// assert_eq!(map.len(), 1);
/// ```
pub fn len(&self) -> usize {
let map = self.0.read().unwrap();
map.len()
}
/// # Examples
///
/// ```
/// use elsa::sync::FrozenBTreeMap;
///
/// let map = FrozenBTreeMap::new();
/// assert_eq!(map.is_empty(), true);
/// map.insert(1, Box::new("a"));
/// assert_eq!(map.is_empty(), false);
/// ```
pub fn is_empty(&self) -> bool {
let map = self.0.read().unwrap();
map.is_empty()
}
}
impl<K: Clone + Ord, V: StableDeref> From<BTreeMap<K, V>> for FrozenBTreeMap<K, V> {
fn from(map: BTreeMap<K, V>) -> Self {
Self(RwLock::new(map))
}
}
impl<Q: ?Sized, K, V> Index<&Q> for FrozenBTreeMap<K, V>
where
Q: Ord,
K: Clone + Ord + Borrow<Q>,
V: StableDeref,
{
type Output = V::Target;
/// # Examples
///
/// ```
/// use elsa::sync::FrozenBTreeMap;
///
/// let map = FrozenBTreeMap::new();
/// map.insert(1, Box::new("a"));
/// assert_eq!(map[&1], "a");
/// ```
fn index(&self, idx: &Q) -> &V::Target {
self.get(idx)
.expect("attempted to index FrozenBTreeMap with unknown key")
}
}
impl<K: Clone + Ord, V: StableDeref> FromIterator<(K, V)> for FrozenBTreeMap<K, V> {
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (K, V)>,
{
let map: BTreeMap<_, _> = iter.into_iter().collect();
map.into()
}
}
impl<K: Clone + Ord, V: StableDeref> Default for FrozenBTreeMap<K, V> {
fn default() -> Self {
Self::new()
}
}
|