moka 0.3.1

A fast and concurrent cache library inspired by Caffeine (Java) and Ristretto (Go)
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
// License and Copyright Notice:
//
// Some of the code and doc comments in this module were copied from
// `std::collections::LinkedList` in the Rust standard library.
// https://github.com/rust-lang/rust/blob/master/src/liballoc/collections/linked_list.rs
//
// The original code/comments from LinkedList are dual-licensed under
// the Apache License, Version 2.0 <https://github.com/rust-lang/rust/blob/master/LICENSE-APACHE>
// or the MIT license <https://github.com/rust-lang/rust/blob/master/LICENSE-MIT>
//
// Copyrights of the original code/comments are retained by their contributors.
// For full authorship information, see the version control history of
// https://github.com/rust-lang/rust/ or https://thanks.rust-lang.org

use std::{marker::PhantomData, ptr::NonNull};

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum CacheRegion {
    Window,
    MainProbation,
    MainProtected,
    WriteOrder,
}

#[derive(PartialEq, Eq)]
pub(crate) struct DeqNode<T> {
    pub(crate) region: CacheRegion,
    next: Option<NonNull<DeqNode<T>>>,
    prev: Option<NonNull<DeqNode<T>>>,
    pub(crate) element: T,
}

impl<T> std::fmt::Debug for DeqNode<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DeqNode")
            .field("region", &self.region)
            .field("next", &self.next)
            .field("prev", &self.prev)
            .finish()
    }
}

impl<T> DeqNode<T> {
    pub(crate) fn new(region: CacheRegion, element: T) -> Self {
        Self {
            region,
            next: None,
            prev: None,
            element,
        }
    }
}

pub(crate) struct Deque<T> {
    region: CacheRegion,
    len: usize,
    head: Option<NonNull<DeqNode<T>>>,
    tail: Option<NonNull<DeqNode<T>>>,
    marker: PhantomData<Box<DeqNode<T>>>,
}

impl<T> Drop for Deque<T> {
    fn drop(&mut self) {
        struct DropGuard<'a, T>(&'a mut Deque<T>);

        impl<'a, T> Drop for DropGuard<'a, T> {
            fn drop(&mut self) {
                // Continue the same loop we do below. This only runs when a destructor has
                // panicked. If another one panics this will abort.
                while self.0.pop_front().is_some() {}
            }
        }

        while let Some(node) = self.pop_front() {
            let guard = DropGuard(self);
            drop(node);
            std::mem::forget(guard);
        }
    }
}

// Inner crate public function/methods
impl<T> Deque<T> {
    pub(crate) fn new(region: CacheRegion) -> Self {
        Self {
            region,
            head: None,
            tail: None,
            len: 0,
            marker: PhantomData::default(),
        }
    }

    pub(crate) fn region(&self) -> &CacheRegion {
        &self.region
    }

    pub(crate) fn contains(&self, node: &DeqNode<T>) -> bool {
        self.region == node.region && (node.prev.is_some() || self.is_head(node))
    }

    pub(crate) fn peek_front(&self) -> Option<&DeqNode<T>> {
        // This method takes care not to create mutable references to whole nodes,
        // to maintain validity of aliasing pointers into `element`.
        self.head.as_ref().map(|node| unsafe { node.as_ref() })
    }

    /// Removes and returns the node at the front of the list.
    pub(crate) fn pop_front(&mut self) -> Option<Box<DeqNode<T>>> {
        // This method takes care not to create mutable references to whole nodes,
        // to maintain validity of aliasing pointers into `element`.
        self.head.map(|node| unsafe {
            let mut node = Box::from_raw(node.as_ptr());
            self.head = node.next;

            match self.head {
                None => self.tail = None,
                // Not creating new mutable (unique!) references overlapping `element`.
                Some(head) => (*head.as_ptr()).prev = None,
            }

            self.len -= 1;

            node.prev = None;
            node.next = None;
            node
        })
    }

    #[cfg(test)]
    fn peek_back(&self) -> Option<&DeqNode<T>> {
        // This method takes care not to create mutable references to whole nodes,
        // to maintain validity of aliasing pointers into `element`.
        self.tail.as_ref().map(|node| unsafe { node.as_ref() })
    }

    /// Adds the given node to the back of the list.
    pub(crate) fn push_back(&mut self, mut node: Box<DeqNode<T>>) -> NonNull<DeqNode<T>> {
        // This method takes care not to create mutable references to whole nodes,
        // to maintain validity of aliasing pointers into `element`.
        unsafe {
            node.next = None;
            node.prev = self.tail;
            let node = NonNull::new(Box::into_raw(node)).expect("Got a null ptr");

            match self.tail {
                None => self.head = Some(node),
                // Not creating new mutable (unique!) references overlapping `element`.
                Some(tail) => (*tail.as_ptr()).next = Some(node),
            }

            self.tail = Some(node);
            self.len += 1;
            node
        }
    }

    /// Panics:
    pub(crate) unsafe fn move_to_back(&mut self, mut node: NonNull<DeqNode<T>>) {
        if self.is_tail(node.as_ref()) {
            return;
        }

        let node = node.as_mut(); // this one is ours now, we can create an &mut.

        // Not creating new mutable (unique!) references overlapping `element`.
        match node.prev {
            Some(prev) if node.next.is_some() => (*prev.as_ptr()).next = node.next,
            Some(..) => (),
            // This node is the head node.
            None => self.head = node.next,
        };

        // This node is not the tail node.
        if let Some(next) = node.next.take() {
            (*next.as_ptr()).prev = node.prev;

            let mut node = NonNull::from(node);
            match self.tail {
                // Not creating new mutable (unique!) references overlapping `element`.
                Some(tail) => {
                    node.as_mut().prev = Some(tail);
                    (*tail.as_ptr()).next = Some(node)
                }
                None => unreachable!(),
            }
            self.tail = Some(node);
        }
    }

    /// Unlinks the specified node from the current list.
    ///
    /// This method takes care not to create mutable references to `element`, to
    /// maintain validity of aliasing pointers.
    ///
    /// Panics:
    pub(crate) unsafe fn unlink(&mut self, mut node: NonNull<DeqNode<T>>) {
        assert_eq!(self.region, node.as_ref().region);

        let node = node.as_mut(); // this one is ours now, we can create an &mut.

        // Not creating new mutable (unique!) references overlapping `element`.
        match node.prev {
            Some(prev) => (*prev.as_ptr()).next = node.next,
            // this node is the head node
            None => self.head = node.next,
        };

        match node.next {
            Some(next) => (*next.as_ptr()).prev = node.prev,
            // this node is the tail node
            None => self.tail = node.prev,
        };

        node.prev = None;
        node.next = None;

        self.len -= 1;
    }
}

// Private function/methods
impl<T> Deque<T> {
    fn is_head(&self, node: &DeqNode<T>) -> bool {
        if let Some(head) = self.head {
            std::ptr::eq(unsafe { head.as_ref() }, node)
        } else {
            false
        }
    }

    fn is_tail(&self, node: &DeqNode<T>) -> bool {
        if let Some(tail) = self.tail {
            std::ptr::eq(unsafe { tail.as_ref() }, node)
        } else {
            false
        }
    }

    #[cfg(test)]
    fn len(&self) -> usize {
        self.len
    }
}

#[cfg(test)]
mod tests {
    use super::{CacheRegion::MainProbation, DeqNode, Deque};

    #[test]
    #[allow(clippy::cognitive_complexity)]
    fn basics() {
        let mut deque: Deque<String> = Deque::new(MainProbation);
        assert_eq!(deque.len(), 0);
        assert!(deque.peek_front().is_none());
        assert!(deque.peek_back().is_none());

        // push_back(node1)
        let node1 = DeqNode::new(MainProbation, "a".to_string());
        assert!(!deque.contains(&node1));
        let node1 = Box::new(node1);
        let node1_ptr = deque.push_back(node1);
        assert_eq!(deque.len(), 1);

        // peek_front() -> node1
        let head_a = deque.peek_front().unwrap();
        assert!(deque.contains(&head_a));
        assert!(deque.is_head(&head_a));
        assert!(deque.is_tail(&head_a));
        assert_eq!(head_a.element, "a".to_string());

        // move_to_back(node1)
        unsafe { deque.move_to_back(node1_ptr.clone()) };
        assert_eq!(deque.len(), 1);

        // peek_front() -> node1
        let head_b = deque.peek_front().unwrap();
        assert!(deque.contains(&head_b));
        assert!(deque.is_head(&head_b));
        assert!(deque.is_tail(&head_b));
        assert!(std::ptr::eq(head_b, unsafe { node1_ptr.as_ref() }));
        assert!(head_b.prev.is_none());
        assert!(head_b.next.is_none());

        // peek_back() -> node1
        let tail_a = deque.peek_back().unwrap();
        assert!(deque.contains(&tail_a));
        assert!(deque.is_head(&tail_a));
        assert!(deque.is_tail(&tail_a));
        assert!(std::ptr::eq(tail_a, unsafe { node1_ptr.as_ref() }));
        assert!(tail_a.prev.is_none());
        assert!(tail_a.next.is_none());

        // push_back(node2)
        let node2 = DeqNode::new(MainProbation, "b".to_string());
        assert!(!deque.contains(&node2));
        let node2_ptr = deque.push_back(Box::new(node2));
        assert_eq!(deque.len(), 2);

        // peek_front() -> node1
        let head_c = deque.peek_front().unwrap();
        assert!(deque.contains(&head_c));
        assert!(deque.is_head(&head_c));
        assert!(!deque.is_tail(&head_c));
        assert!(std::ptr::eq(head_c, unsafe { node1_ptr.as_ref() }));
        assert!(head_c.prev.is_none());
        assert!(std::ptr::eq(
            unsafe { head_c.next.unwrap().as_ref() },
            unsafe { node2_ptr.as_ref() }
        ));

        // move_to_back(node2)
        unsafe { deque.move_to_back(node2_ptr.clone()) };
        assert_eq!(deque.len(), 2);

        // peek_front() -> node1
        let head_d = deque.peek_front().unwrap();
        assert!(deque.contains(&head_d));
        assert!(deque.is_head(&head_d));
        assert!(!deque.is_tail(&head_d));
        assert!(std::ptr::eq(head_d, unsafe { node1_ptr.as_ref() }));
        assert!(head_d.prev.is_none());
        assert!(std::ptr::eq(
            unsafe { head_d.next.unwrap().as_ref() },
            unsafe { node2_ptr.as_ref() }
        ));

        // peek_back() -> node2
        let tail_b = deque.peek_back().unwrap();
        assert!(deque.contains(&tail_b));
        assert!(!deque.is_head(&tail_b));
        assert!(deque.is_tail(&tail_b));
        assert!(std::ptr::eq(tail_b, unsafe { node2_ptr.as_ref() }));
        assert!(std::ptr::eq(
            unsafe { tail_b.prev.unwrap().as_ref() },
            unsafe { node1_ptr.as_ref() }
        ));
        assert_eq!(tail_b.element, "b".to_string());
        assert!(tail_b.next.is_none());

        // move_to_back(node1)
        unsafe { deque.move_to_back(node1_ptr.clone()) };
        assert_eq!(deque.len(), 2);

        // peek_front() -> node2
        let head_e = deque.peek_front().unwrap();
        assert!(deque.contains(&head_e));
        assert!(deque.is_head(&head_e));
        assert!(!deque.is_tail(&head_e));
        assert!(std::ptr::eq(head_e, unsafe { node2_ptr.as_ref() }));
        assert!(head_e.prev.is_none());
        assert!(std::ptr::eq(
            unsafe { head_e.next.unwrap().as_ref() },
            unsafe { node1_ptr.as_ref() }
        ));

        // peek_back() -> node1
        let tail_c = deque.peek_back().unwrap();
        assert!(deque.contains(&tail_c));
        assert!(!deque.is_head(&tail_c));
        assert!(deque.is_tail(&tail_c));
        assert!(std::ptr::eq(tail_c, unsafe { node1_ptr.as_ref() }));
        assert!(std::ptr::eq(
            unsafe { tail_c.prev.unwrap().as_ref() },
            unsafe { node2_ptr.as_ref() }
        ));
        assert!(tail_c.next.is_none());

        // push_back(node3)
        let node3 = DeqNode::new(MainProbation, "c".to_string());
        assert!(!deque.contains(&node3));
        let node3_ptr = deque.push_back(Box::new(node3));
        assert_eq!(deque.len(), 3);

        // peek_front() -> node2
        let head_f = deque.peek_front().unwrap();
        assert!(deque.contains(&head_f));
        assert!(deque.is_head(&head_f));
        assert!(!deque.is_tail(&head_f));
        assert!(std::ptr::eq(head_f, unsafe { node2_ptr.as_ref() }));
        assert!(head_f.prev.is_none());
        assert!(std::ptr::eq(
            unsafe { head_f.next.unwrap().as_ref() },
            unsafe { node1_ptr.as_ref() }
        ));

        // peek_back() -> node3
        let tail_d = deque.peek_back().unwrap();
        assert!(std::ptr::eq(tail_d, unsafe { node3_ptr.as_ref() }));
        assert_eq!(tail_d.element, "c".to_string());
        assert!(deque.contains(&tail_d));
        assert!(!deque.is_head(&tail_d));
        assert!(deque.is_tail(&tail_d));
        assert!(std::ptr::eq(tail_d, unsafe { node3_ptr.as_ref() }));
        assert!(std::ptr::eq(
            unsafe { tail_d.prev.unwrap().as_ref() },
            unsafe { node1_ptr.as_ref() }
        ));
        assert!(tail_d.next.is_none());

        // move_to_back(node1)
        unsafe { deque.move_to_back(node1_ptr.clone()) };
        assert_eq!(deque.len(), 3);

        // peek_front() -> node2
        let head_g = deque.peek_front().unwrap();
        assert!(deque.contains(&head_g));
        assert!(deque.is_head(&head_g));
        assert!(!deque.is_tail(&head_g));
        assert!(std::ptr::eq(head_g, unsafe { node2_ptr.as_ref() }));
        assert!(head_g.prev.is_none());
        assert!(std::ptr::eq(
            unsafe { head_g.next.unwrap().as_ref() },
            unsafe { node3_ptr.as_ref() }
        ));

        // peek_back() -> node1
        let tail_e = deque.peek_back().unwrap();
        assert!(deque.contains(&tail_e));
        assert!(!deque.is_head(&tail_e));
        assert!(deque.is_tail(&tail_e));
        assert!(std::ptr::eq(tail_e, unsafe { node1_ptr.as_ref() }));
        assert!(std::ptr::eq(
            unsafe { tail_e.prev.unwrap().as_ref() },
            unsafe { node3_ptr.as_ref() }
        ));
        assert!(tail_e.next.is_none());

        // unlink(node3)
        unsafe { deque.unlink(node3_ptr) };
        assert_eq!(deque.len(), 2);
        let node3_ref = unsafe { node3_ptr.as_ref() };
        assert!(!deque.contains(node3_ref));
        assert!(node3_ref.next.is_none());
        assert!(node3_ref.next.is_none());

        // This does not work as expected because NonNull implements Copy trait.
        // See clippy(clippy::drop_copy) at
        // https://rust-lang.github.io/rust-clippy/master/index.html#drop_copy
        // std::mem::drop(node3_ptr);

        // peek_front() -> node2
        let head_h = deque.peek_front().unwrap();
        assert!(deque.contains(&head_h));
        assert!(deque.is_head(&head_h));
        assert!(!deque.is_tail(&head_h));
        assert!(std::ptr::eq(head_h, unsafe { node2_ptr.as_ref() }));
        assert!(head_h.prev.is_none());
        assert!(std::ptr::eq(
            unsafe { head_h.next.unwrap().as_ref() },
            unsafe { node1_ptr.as_ref() }
        ));

        // peek_back() -> node1
        let tail_f = deque.peek_back().unwrap();
        assert!(deque.contains(&tail_f));
        assert!(!deque.is_head(&tail_f));
        assert!(deque.is_tail(&tail_f));
        assert!(std::ptr::eq(tail_f, unsafe { node1_ptr.as_ref() }));
        assert!(std::ptr::eq(
            unsafe { tail_f.prev.unwrap().as_ref() },
            unsafe { node2_ptr.as_ref() }
        ));
        assert!(tail_f.next.is_none());

        // unlink(node2)
        unsafe { deque.unlink(node2_ptr) };
        assert_eq!(deque.len(), 1);
        let node2_ref = unsafe { node2_ptr.as_ref() };
        assert!(!deque.contains(node2_ref));
        assert!(node2_ref.next.is_none());
        assert!(node2_ref.next.is_none());
        // std::mem::drop(node2_ptr);

        // peek_front() -> node1
        let head_g = deque.peek_front().unwrap();
        assert!(deque.contains(&head_g));
        assert!(deque.is_head(&head_g));
        assert!(deque.is_tail(&head_g));
        assert!(std::ptr::eq(head_g, unsafe { node1_ptr.as_ref() }));
        assert!(head_g.prev.is_none());
        assert!(head_g.next.is_none());

        // peek_back() -> node1
        let tail_g = deque.peek_back().unwrap();
        assert!(deque.contains(&tail_g));
        assert!(deque.is_head(&tail_g));
        assert!(deque.is_tail(&tail_g));
        assert!(std::ptr::eq(tail_g, unsafe { node1_ptr.as_ref() }));
        assert!(tail_g.next.is_none());
        assert!(tail_g.next.is_none());

        // unlink(node1)
        unsafe { deque.unlink(node1_ptr) };
        assert_eq!(deque.len(), 0);
        let node1_ref = unsafe { node1_ptr.as_ref() };
        assert!(!deque.contains(node1_ref));
        assert!(node1_ref.next.is_none());
        assert!(node1_ref.next.is_none());
        // std::mem::drop(node1_ptr);

        // peek_front() -> node1
        let head_h = deque.peek_front();
        assert!(head_h.is_none());

        // peek_back() -> node1
        let tail_e = deque.peek_back();
        assert!(tail_e.is_none());
    }

    #[test]
    fn drop() {
        use std::{cell::RefCell, rc::Rc};

        struct X(u32, Rc<RefCell<Vec<u32>>>);

        impl Drop for X {
            fn drop(&mut self) {
                self.1.borrow_mut().push(self.0)
            }
        }

        let mut deque: Deque<X> = Deque::new(MainProbation);
        let dropped = Rc::new(RefCell::new(Vec::default()));

        let node1 = DeqNode::new(MainProbation, X(1, Rc::clone(&dropped)));
        let node2 = DeqNode::new(MainProbation, X(2, Rc::clone(&dropped)));
        let node3 = DeqNode::new(MainProbation, X(3, Rc::clone(&dropped)));
        let node4 = DeqNode::new(MainProbation, X(4, Rc::clone(&dropped)));
        deque.push_back(Box::new(node1));
        deque.push_back(Box::new(node2));
        deque.push_back(Box::new(node3));
        deque.push_back(Box::new(node4));
        assert_eq!(deque.len(), 4);

        std::mem::drop(deque);

        assert_eq!(*dropped.borrow(), &[1, 2, 3, 4]);
    }
}