paged-collections 0.2.0

Paged sparse sets, maps and stable-address containers
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
//! FIFO modeling, ownership and threaded recycling for the concurrent queue.

#![cfg(all(feature = "concurrent", not(loom)))]
#![cfg(all(
    any(target_pointer_width = "32", target_pointer_width = "64"),
    target_has_atomic = "ptr",
    target_has_atomic = "64"
))]

use std::cell::Cell;
use std::collections::VecDeque;
use std::sync::Barrier;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::thread;

use paged_collections::concurrent::ConcurrentQueue;
use paged_collections::paged_deque::PagedDeque;
use proptest::prelude::*;
use proptest::test_runner::TestCaseResult;

#[derive(Clone, Debug)]
enum Op {
    Push(i32),
    Pop,
    Clear,
    Shrink,
    Drain,
    Edit(i32),
    Convert,
}

fn operations() -> impl Strategy<Value = Vec<Op>> {
    prop::collection::vec(
        prop_oneof![
            10 => any::<i32>().prop_map(Op::Push),
            4 => Just(Op::Pop),
            1 => Just(Op::Clear),
            2 => Just(Op::Shrink),
            1 => Just(Op::Drain),
            2 => any::<i32>().prop_map(Op::Edit),
            2 => Just(Op::Convert),
        ],
        0..if cfg!(miri) { 30 } else { 160 },
    )
}

fn check_model<const PAGE: usize>(ops: &[Op]) -> TestCaseResult {
    let mut queue = ConcurrentQueue::<i32, PAGE>::new();
    let mut model = VecDeque::new();
    for op in ops {
        match *op {
            Op::Push(value) => {
                queue.push(value);
                model.push_back(value);
            }
            Op::Pop => prop_assert_eq!(queue.pop(), model.pop_front()),
            Op::Clear => {
                let pages = queue.allocated_pages();
                queue.clear();
                model.clear();
                prop_assert_eq!(queue.allocated_pages(), pages);
            }
            Op::Shrink => {
                let pages = queue.allocated_pages();
                queue.shrink_to_fit();
                prop_assert!(queue.allocated_pages() <= pages);
                if model.is_empty() {
                    prop_assert_eq!(queue.allocated_pages(), 0);
                }
            }
            Op::Drain => {
                let pages = queue.allocated_pages();
                prop_assert!(queue.drain().eq(model.drain(..)));
                prop_assert_eq!(queue.allocated_pages(), pages);
            }
            Op::Edit(delta) => {
                for (index, value) in queue.iter_mut().enumerate() {
                    *value = value.wrapping_add(delta).wrapping_add(index as i32);
                }
                for (index, value) in model.iter_mut().enumerate() {
                    *value = value.wrapping_add(delta).wrapping_add(index as i32);
                }
            }
            Op::Convert => {
                let plain = PagedDeque::from(queue);
                prop_assert!(plain.iter().eq(model.iter()));
                queue = ConcurrentQueue::from(plain);
            }
        }
        prop_assert_eq!(queue.len(), model.len());
        prop_assert_eq!(queue.is_empty(), model.is_empty());
        prop_assert!(
            queue
                .iter_mut()
                .map(|value| *value)
                .eq(model.iter().copied())
        );
    }
    prop_assert!(queue.into_iter().eq(model));
    Ok(())
}

fn config() -> ProptestConfig {
    let defaults = ProptestConfig::default();
    ProptestConfig {
        cases: if cfg!(miri) { 4 } else { 128 },
        failure_persistence: if cfg!(miri) {
            None
        } else {
            defaults.failure_persistence
        },
        ..defaults
    }
}

proptest! {
    #![proptest_config(config())]

    #[test]
    fn matches_vec_deque(ops in operations()) {
        check_model::<1>(&ops)?;
        check_model::<2>(&ops)?;
        check_model::<4>(&ops)?;
    }
}

#[test]
fn capacity_and_shrink_preserve_the_active_chain() {
    let mut queue = ConcurrentQueue::<_, 2>::with_capacity(15);
    assert_eq!(queue.allocated_pages(), 8);
    queue.extend(0..5);
    assert_eq!(queue.pop(), Some(0));
    assert_eq!(queue.pop(), Some(1));
    queue.shrink_to_fit();
    assert_eq!(queue.allocated_pages(), 2);
    assert_eq!(format!("{queue:?}"), "ConcurrentQueue { len: 3 }");

    {
        let mut values = queue.iter_mut();
        let first = values.next().unwrap();
        let second = values.next().unwrap();
        assert_eq!(values.next().map(|value| *value), Some(4));
        assert_eq!(values.next(), None);
        *first += 10;
        *second += 10;
    }

    queue.extend(5..10);
    assert!(queue.drain().eq([12, 13, 4, 5, 6, 7, 8, 9]));
    queue.shrink_to_fit();
    assert_eq!(queue.allocated_pages(), 0);
    queue.push(10);
    assert_eq!(queue.pop(), Some(10));
}

#[test]
fn values_are_dropped_exactly_once() {
    struct Value<'a>(&'a Cell<usize>);

    impl Drop for Value<'_> {
        fn drop(&mut self) {
            self.0.set(self.0.get() + 1);
        }
    }

    fn check<const PAGE: usize>() {
        let counts = [const { Cell::new(0) }; 20];
        let mut queue = ConcurrentQueue::<_, PAGE>::new();
        queue.extend(counts[..5].iter().map(Value));
        drop(queue.pop().unwrap());
        queue.clear();
        assert!(counts[..5].iter().all(|count| count.get() == 1));

        queue.extend(counts[5..10].iter().map(Value));
        let mut drain = queue.drain();
        drop(drain.next().unwrap());
        assert_eq!(counts[5].get(), 1);
        drop(drain);
        assert!(counts[5..10].iter().all(|count| count.get() == 1));

        queue.extend(counts[10..15].iter().map(Value));
        drop(queue);
        let mut iter = counts[15..]
            .iter()
            .map(Value)
            .collect::<ConcurrentQueue<_, PAGE>>()
            .into_iter();
        drop(iter.next().unwrap());
        drop(iter);
        assert!(counts.iter().all(|count| count.get() == 1));
    }

    check::<1>();
    check::<2>();
    check::<4>();
}

#[test]
fn a_panicking_drop_still_drops_the_rest() {
    struct Value<'a>(&'a Cell<usize>, bool);

    impl Drop for Value<'_> {
        fn drop(&mut self) {
            self.0.set(self.0.get() + 1);
            if self.1 {
                panic!("value drop");
            }
        }
    }

    fn check<const PAGE: usize>() {
        let count = Cell::new(0);
        let queue = ConcurrentQueue::<_, PAGE>::new();
        queue.push(Value(&count, true));
        for _ in 0..4 {
            queue.push(Value(&count, false));
        }
        let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(queue)));
        assert!(unwound.is_err());
        assert_eq!(count.get(), 5);

        // The owning iterator drops through the same path.
        let count = Cell::new(0);
        let queue = ConcurrentQueue::<_, PAGE>::new();
        queue.push(Value(&count, false));
        queue.push(Value(&count, true));
        queue.push(Value(&count, false));
        let mut iter = queue.into_iter();
        drop(iter.next().unwrap());
        let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(iter)));
        assert!(unwound.is_err());
        assert_eq!(count.get(), 3);
    }

    check::<1>();
    check::<2>();
    check::<4>();
}

#[test]
fn repeated_shrinks_keep_only_needed_pages() {
    let mut queue = ConcurrentQueue::<_, 1>::new();
    queue.push(0);
    for value in 1..if cfg!(miri) { 16 } else { 2_000 } {
        queue.push(value);
        assert_eq!(queue.pop(), Some(value - 1));
        queue.shrink_to_fit();
        assert_eq!(queue.len(), 1);
        assert_eq!(queue.allocated_pages(), 2);
    }
    assert!(queue.drain().eq([if cfg!(miri) { 15 } else { 1_999 }]));
}

#[test]
fn steady_state_reuses_pages() {
    fn check<const PAGE: usize>() {
        let queue = ConcurrentQueue::<_, PAGE>::new();
        for value in 0..if cfg!(miri) { 24 } else { 40_000 } {
            queue.push(value);
            assert_eq!(queue.pop(), Some(value));
        }
        assert!(queue.is_empty());
        assert_eq!(queue.allocated_pages(), 2);
    }

    check::<1>();
    check::<2>();
    check::<4>();
}

#[test]
fn multiple_producers_and_consumers() {
    fn check<const PAGE: usize>() {
        let producers = if cfg!(miri) { 2 } else { 4 };
        let consumers = if cfg!(miri) { 2 } else { 4 };
        let per_producer = if cfg!(miri) { 12 } else { 2_000 };
        let queue = ConcurrentQueue::<_, PAGE>::new();
        let start = Barrier::new(producers + consumers);
        let done = AtomicBool::new(false);
        // completed[p] is one past the highest sequence of producer p that a
        // finished pop returned. A pop that starts after another pop finished
        // must return a later value of that producer, whichever consumer ran
        // it: this catches FIFO reversals across consumers.
        let completed: Vec<_> = (0..producers).map(|_| AtomicUsize::new(0)).collect();

        let observed = thread::scope(|scope| {
            let writers: Vec<_> = (0..producers)
                .map(|id| {
                    let (queue, start) = (&queue, &start);
                    scope.spawn(move || {
                        start.wait();
                        for sequence in 0..per_producer {
                            queue.push((id, sequence));
                        }
                    })
                })
                .collect();
            let readers: Vec<_> = (0..consumers)
                .map(|_| {
                    scope.spawn(|| {
                        start.wait();
                        let mut values = Vec::new();
                        let mut floor = vec![0; producers];
                        loop {
                            let finished = done.load(Ordering::Acquire);
                            for (low, done) in floor.iter_mut().zip(&completed) {
                                *low = done.load(Ordering::Acquire);
                            }
                            if let Some(value) = queue.pop() {
                                let (id, sequence) = value;
                                assert!(
                                    sequence >= floor[id],
                                    "pop returned {value:?} after a finished pop of \
                                     producer {id} sequence {}",
                                    floor[id] - 1
                                );
                                completed[id].fetch_max(sequence + 1, Ordering::AcqRel);
                                values.push(value);
                            } else if finished {
                                return values;
                            } else {
                                thread::yield_now();
                            }
                        }
                    })
                })
                .collect();

            for writer in writers {
                writer.join().unwrap();
            }
            done.store(true, Ordering::Release);
            readers
                .into_iter()
                .map(|reader| reader.join().unwrap())
                .collect::<Vec<_>>()
        });

        let mut seen = vec![vec![false; per_producer]; producers];
        for values in observed {
            let mut last = vec![None; producers];
            for (id, sequence) in values {
                assert!(last[id].is_none_or(|previous| previous < sequence));
                last[id] = Some(sequence);
                assert!(!std::mem::replace(&mut seen[id][sequence], true));
            }
        }
        assert!(seen.into_iter().flatten().all(|found| found));
        assert_eq!(queue.pop(), None);
    }

    check::<1>();
    check::<4>();
}

#[test]
fn store_buffering_cannot_hide_a_finished_push() {
    // Litmus test for the SeqCst tail CAS in `push` and the SeqCst fence
    // before the tail load in `pop`. Loom weakens SeqCst read-modify-writes,
    // so this ordering is only checked on real hardware. On x86 the locked
    // CAS is a full barrier anyway, so this mainly guards weaker targets.
    let rounds = if cfg!(miri) { 20 } else { 20_000 };
    let mut queue = ConcurrentQueue::<u8, 4>::new();
    let flag = AtomicBool::new(false);
    let start = Barrier::new(2);
    for _ in 0..rounds {
        let (saw_flag, popped) = thread::scope(|scope| {
            let pusher = scope.spawn(|| {
                start.wait();
                queue.push(1);
                flag.load(Ordering::SeqCst)
            });
            start.wait();
            flag.store(true, Ordering::SeqCst);
            let popped = queue.pop();
            let saw_flag = pusher.join().unwrap();
            (saw_flag, popped)
        });
        assert!(
            saw_flag || popped.is_some(),
            "the pusher missed the flag and the pop missed the push"
        );
        queue.clear();
        flag.store(false, Ordering::Relaxed);
    }
}

#[test]
fn zero_sized_values_and_send_without_sync() {
    #[repr(align(64))]
    struct Zst;

    let mut queue = ConcurrentQueue::<Zst, 2>::default();
    queue.extend([Zst, Zst, Zst, Zst, Zst]);
    assert_eq!(queue.iter_mut().count(), 5);
    assert_eq!(queue.drain().count(), 5);
    assert!(queue.is_empty());

    let queue = ConcurrentQueue::<Cell<u32>, 1>::new();
    thread::scope(|scope| {
        scope.spawn(|| queue.push(Cell::new(42))).join().unwrap();
        assert_eq!(queue.pop().unwrap().into_inner(), 42);
    });
}

#[cfg(feature = "serde")]
#[test]
fn serde_round_trips_through_paged_deque() {
    #[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
    struct Value(u32);

    let plain: PagedDeque<_, 2> = [Value(1), Value(2), Value(3)].into_iter().collect();
    let encoded = serde_json::to_string(&plain).unwrap();
    let queue: ConcurrentQueue<Value, 2> = serde_json::from_str(&encoded).unwrap();
    assert_eq!(queue.len(), 3);
    assert_eq!(
        serde_json::to_string(&PagedDeque::from(queue)).unwrap(),
        encoded
    );
}