arctic-wt 0.1.5

Lock-free adaptive radix tree
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
mod membarrier;
pub mod prefix;
mod thread;
pub(crate) use prefix::Prefix;
mod key;
pub use key::Key;

use core::cell::UnsafeCell;
use core::marker::PhantomData;
use core::num::NonZeroU64;
use core::sync::atomic::AtomicBool;
use core::sync::atomic::AtomicU64;
use core::sync::atomic::Ordering;

use crate::concurrent::Smr;
use crate::concurrent::Value;
use crate::concurrent::smr;
use crate::stat;
use crate::sync::Atomic;

#[repr(C, align(64))]
#[derive(Default)]
struct Cache<T>(T);

/// Hazard key backend for safe memory reclamation.
///
/// <div class="warning">
///
/// **This backend currently has the following limitations**:
/// - Only one hazard can be installed at a time per thread, so calling `get`
///   while holding a reference from a previous `get`, for example, will panic.
/// - Only supports up to 256 threads.
///
/// </div>
///
/// Hazard keys are a new SMR scheme that use an operation's key to
/// approximate a set of [hazard pointers](https://en.wikipedia.org/wiki/Hazard_pointer).
///
/// To briefly illustrate the idea: every node and value in a tree can be associated
/// with a key prefix. For example, given the following tree:
///
/// ```text
///     N0 [ a | b ]
///        /    |
///       /     | c
///      /      |
///  N1 [f]  N2 [ d | e ]
///     /        /   |
///    /        /    | g
///   /        /     |
/// (V0)     (V1)   (V2)
/// ```
///
/// We have the following key prefixes:
///
/// | Id | Type  | Prefix |
/// |----|-------|-------|
/// | N0 | Node  |       |
/// | N1 | Node  | a     |
/// | N2 | Node  | bc    |
/// | V0 | Value | af    |
/// | V1 | Value | bcd   |
/// | V2 | Value | bceg  |
///
/// Second, note that each operation is also associated with
/// a key prefix. This can be a full key for point operations like
/// [`ConcurrentMap::get`][crate::concurrent::Map::get], or a key prefix for prefix
/// operations like [`ConcurrentMap::prefix`][crate::concurrent::Map::prefix].
///
/// Then the core insight is that a trie operation will never access
/// nodes or values whose key prefixes do not overlap with its own.
/// We use guards to ensure that a hazard key is installed
/// for the lifetime of an operation.
/// Guards protect all nodes and values with overlapping key prefixes from
/// reclamation.
///
/// In our example tree...
///
/// ```text
///     N0 [ a | b ]
///        /    |
///       /     | c
///      /      |
///  N1 [f]  N2 [ d | e ]
///     /        /   |
///    /        /    | g
///   /        /     |
/// (V0)     (V1)   (V2)
/// ```
///
/// A guard with key prefix `bceg` would protect
/// nodes N0 + N2 and value V2 from reclamation.
/// A guard with key prefix `b` would protect nodes N0 + N2
/// and values V1 + V2 from reclamation.
pub struct Hazard<K: Key, V: Value>(Box<Global<K, V>>);

impl<K: Key, V: Value> Default for Hazard<K, V> {
    fn default() -> Self {
        Self(Box::default())
    }
}

struct Global<K: Key, V: Value> {
    garbage: AtomicU64,

    // FIXME: jagged/triangular array
    hazards: [Cache<Atomic<K::Prefix>>; thread::MAX],
    locals: [UnsafeCell<Local<K::Prefix, V>>; thread::MAX],
    membarrier: AtomicBool,
    reclaim_threshold: usize,
    value: PhantomData<V>,
}

unsafe impl<K: Key, V: Value> Send for Global<K, V> {}
unsafe impl<K: Key, V: Value> Sync for Global<K, V> {}

impl<K: Key, V: Value> Default for Global<K, V> {
    fn default() -> Self {
        Self {
            garbage: AtomicU64::new(0),
            hazards: core::array::from_fn(|_| {
                Cache(Atomic::new_packed(
                    <<K::Prefix as ribbit::Pack>::Packed as Prefix>::HAZARD_NULL,
                ))
            }),
            locals: core::array::from_fn(|_| {
                UnsafeCell::new(Local {
                    garbage: 0,
                    cycle: 0,
                    snapshot: Vec::new(),
                    retired: Vec::new(),
                    _value: PhantomData,
                })
            }),
            membarrier: AtomicBool::new(false),
            reclaim_threshold: 64,
            value: PhantomData,
        }
    }
}

impl<K: Key, V: Value> Hazard<K, V> {
    /// Configure the number of retired allocations that can accumulate per-thread
    /// before the thread loads all hazard pointers and attempts to reclaim them.
    #[inline]
    #[must_use]
    pub fn with_reclaim_threshold(mut self, reclaim_threshold: usize) -> Self {
        self.0.reclaim_threshold = reclaim_threshold;
        self
    }

    /// Enable or disable [`membarrier`](https://man7.org/linux/man-pages/man2/membarrier.2.html) optimization.
    #[inline]
    pub fn set_membarrier(&mut self, enable: bool) {
        *self.0.membarrier.get_mut() = enable
    }

    /// Enable [`membarrier`](https://man7.org/linux/man-pages/man2/membarrier.2.html) optimization.
    #[inline]
    pub fn enable_membarrier(&self) {
        self.0.membarrier.store(true, Ordering::Relaxed)
    }

    /// Eagerly reclaim all retired allocations.
    pub fn reclaim(&mut self) {
        self.0.reclaim()
    }
}

impl<K: Key, V: Value> Global<K, V> {
    fn reclaim(&mut self) {
        self.locals
            .iter_mut()
            .take(thread::count())
            .map(|local| local.get_mut())
            .flat_map(|local| local.retired.drain(..))
            .for_each(|(prefix, raw)| {
                stat::increment(stat::Counter::FreeReclaim);
                deallocate::<K::Prefix, V>(prefix, raw);
            })
    }
}

impl<K: Key, V: Value> Drop for Global<K, V> {
    fn drop(&mut self) {
        self.reclaim();
    }
}

impl<K: Key, V: Value> Smr<K, V> for Hazard<K, V> {
    type Guard<'g>
        = Guard<'g, K, V>
    where
        V: 'g,
        Self: 'g;

    #[inline]
    fn guard<'g>(&'g self, key: K::Read<'_>) -> Self::Guard<'g>
    where
        V: 'g,
    {
        let id = usize::from(thread::Id::current());
        let membarrier = self.0.membarrier.load(Ordering::Relaxed);
        let hazard = &self.0.hazards[id].0;
        let local = &self.0.locals[id];

        assert!(!hazard.load_packed(Ordering::Relaxed).is_active());
        hazard.store_packed(K::hazard(key), membarrier::fast_store_ordering(membarrier));
        membarrier::fast_barrier(membarrier);

        Guard {
            hazard,
            local,
            global: &self.0,
        }
    }

    fn garbage(&self) -> u32 {
        self.0.garbage.load(Ordering::Relaxed) as u32
    }
}

#[repr(align(64))]
struct Local<P: ribbit::Pack<Packed: Prefix>, V> {
    garbage: i32,
    cycle: usize,
    snapshot: Vec<ribbit::Packed<P>>,
    retired: Vec<(ribbit::Packed<P>, u64)>,
    _value: PhantomData<V>,
}

impl<K: Key, V: Value> Global<K, V> {
    #[cold]
    fn flush(global: &Global<K, V>, local: &mut Local<K::Prefix, V>) {
        stat::max(stat::Max::RetireCache, local.retired.len() as u64);

        membarrier::slow(global.membarrier.load(Ordering::Relaxed));

        local.snapshot.extend(
            global.hazards[..thread::count().next_multiple_of(4)]
                .iter()
                .map(|hazard| hazard.0.load_packed(Ordering::Relaxed)),
        );

        let mut freed = 0;
        let (chunks, leftover) = local.snapshot.as_chunks::<4>();
        validate!(leftover.is_empty());

        local.retired.retain_mut(|(prefix, raw)| {
            if chunks.iter().any(|chunk| prefix.is_conflict(chunk)) {
                stat::increment(stat::Counter::HazardMatch);
                if cfg!(feature = "stat") {
                    *prefix = prefix.with_age(prefix.age().saturating_add(1));
                }
                return true;
            }

            if cfg!(feature = "stat") {
                stat::record(stat::Record::ReclaimDepth, prefix.bytes() as u64);
            }
            freed += 1;

            validate!(prefix.is_value() ^ prefix.is_node());

            if cfg!(feature = "stat")
                && let Some(record) = match prefix.bytes() {
                    0 => Some(stat::Record::ReclaimAge0),
                    1 => Some(stat::Record::ReclaimAge1),
                    2 => Some(stat::Record::ReclaimAge2),
                    3 => Some(stat::Record::ReclaimAge3),
                    _ => None,
                }
            {
                stat::record(record, prefix.age() as u64 + 1);
            }

            stat::increment(stat::Counter::FreeRetire);
            deallocate::<K::Prefix, V>(*prefix, *raw);
            false
        });

        if cfg!(feature = "stat-garbage") {
            local.garbage -= freed;

            if local.garbage <= -(global.reclaim_threshold as i32) {
                global
                    .garbage
                    .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |garbage| {
                        let old_count = garbage >> 32;
                        let old_max = garbage as u32;

                        let new_count = old_count - ((-local.garbage) as u64);
                        Some(new_count << 32 | (old_max as u64))
                    })
                    .unwrap();
                local.garbage = 0;
            }
        }

        local.snapshot.clear();
        stat::record(stat::Record::Flush, freed as u64);
    }
}

/// Guard for [`Hazard`] SMR backend.
pub struct Guard<'g, K: Key, V: Value> {
    hazard: &'g Atomic<K::Prefix>,
    local: &'g UnsafeCell<Local<K::Prefix, V>>,
    global: &'g Global<K, V>,
}

impl<'g, K: Key, V: Value> smr::Guard<V> for Guard<'g, K, V> {
    unsafe fn retire_node(&mut self, _bits: usize, node: NonZeroU64) {
        stat::increment(stat::Counter::Retire);

        let prefix = self
            .hazard
            .load_packed(Ordering::Relaxed)
            .into_prefix(false, Some(_bits));

        let local = unsafe { &mut *self.local.get() };

        if cfg!(feature = "stat-garbage") {
            local.garbage += 1;

            if local.garbage >= self.global.reclaim_threshold as i32 {
                self.global
                    .garbage
                    .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |garbage| {
                        let old_count = garbage >> 32;
                        let old_max = garbage as u32;

                        let new_count = old_count + local.garbage as u64;
                        let new_max = old_max.max(new_count as u32);
                        Some(new_count << 32 | (new_max as u64))
                    })
                    .unwrap();
                local.garbage = 0;
            }
        }

        local.retired.push((prefix, node.get()));
    }

    unsafe fn retire_value(&mut self, value: u64) {
        stat::increment(stat::Counter::Retire);

        let prefix = self
            .hazard
            .load_packed(Ordering::Relaxed)
            .into_prefix(true, None);

        let local = unsafe { &mut *self.local.get() };

        if cfg!(feature = "stat-garbage") {
            local.garbage += 1;

            if local.garbage >= self.global.reclaim_threshold as i32 {
                self.global
                    .garbage
                    .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |garbage| {
                        let old_count = garbage >> 32;
                        let old_max = garbage as u32;

                        let new_count = old_count + local.garbage as u64;
                        let new_max = old_max.max(new_count as u32);
                        Some(new_count << 32 | (new_max as u64))
                    })
                    .unwrap();
                local.garbage = 0;
            }
        }

        local.retired.push((prefix, value));
    }
}

impl<'g, K: Key, V: Value> Drop for Guard<'g, K, V> {
    fn drop(&mut self) {
        self.hazard
            .store_packed(ribbit::Packed::<K::Prefix>::HAZARD_NULL, Ordering::Relaxed);

        let local = unsafe { &mut *self.local.get() };
        if local.retired.len() < self.global.reclaim_threshold {
            local.cycle = 0;
            return;
        }

        if local.cycle == 0 {
            Global::flush(self.global, local)
        }

        // FIXME: introduce separate configuration
        local.cycle = if local.cycle == self.global.reclaim_threshold {
            0
        } else {
            local.cycle + 1
        };
    }
}

fn deallocate<P: ribbit::Pack<Packed: Prefix>, V: Value>(prefix: ribbit::Packed<P>, raw: u64) {
    if prefix.is_node() {
        unsafe {
            let ptr = NonZeroU64::new(raw).unwrap();
            smr::deallocate_node(ptr)
        }
    } else {
        unsafe { smr::deallocate_value::<V>(raw) }
    }
}