range-cache 0.1.1

Sparse byte-range caching and async read coalescing for immutable objects such as remote index files
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
use std::{collections::BTreeMap, num::NonZeroUsize, ops::Range};

use bytes::Bytes;
use proptest::prelude::*;
use range_cache::{CacheCapacity, InsertOutcome, Invalidation, RangeCache};

const KEY_COUNT: usize = 3;
const SOURCE_LENGTH: usize = 32;
const BOUNDED_KEY_COUNT: usize = 3;
const SLOT_COUNT: usize = 4;

#[derive(Clone, Copy, Debug)]
enum Operation {
    Insert,
    Get,
    Missing,
    Invalidate,
}

#[derive(Clone, Copy, Debug)]
enum BoundedOperation {
    Insert,
    Get,
    Missing,
    Invalidate,
    Clear,
}

#[derive(Clone, Copy, Debug)]
enum SparseOperation {
    Insert,
    Get,
    Missing,
    Invalidate,
}

fn operation() -> impl Strategy<Value = (Operation, usize, usize, usize, u8)> {
    (
        prop_oneof![
            4 => Just(Operation::Insert),
            2 => Just(Operation::Get),
            2 => Just(Operation::Missing),
            1 => Just(Operation::Invalidate),
        ],
        0..KEY_COUNT,
        0..=SOURCE_LENGTH,
        0..=SOURCE_LENGTH,
        any::<u8>(),
    )
}

fn bounded_operation() -> impl Strategy<Value = (BoundedOperation, usize, usize, usize)> {
    (
        prop_oneof![
            4 => Just(BoundedOperation::Insert),
            4 => Just(BoundedOperation::Get),
            2 => Just(BoundedOperation::Missing),
            2 => Just(BoundedOperation::Invalidate),
            1 => Just(BoundedOperation::Clear),
        ],
        0..BOUNDED_KEY_COUNT,
        0..SLOT_COUNT,
        0..SLOT_COUNT,
    )
}

fn sparse_operation() -> impl Strategy<Value = (SparseOperation, usize, usize, u8)> {
    (
        prop_oneof![
            4 => Just(SparseOperation::Insert),
            2 => Just(SparseOperation::Get),
            2 => Just(SparseOperation::Missing),
            1 => Just(SparseOperation::Invalidate),
        ],
        0..1_000_000_usize,
        1..65_usize,
        any::<u8>(),
    )
}

fn model_missing(bytes: &[Option<u8>], start: usize, end: usize) -> Vec<std::ops::Range<usize>> {
    let mut missing = Vec::new();
    let mut cursor = start;
    while cursor < end {
        if bytes[cursor].is_some() {
            cursor += 1;
            continue;
        }
        let range_start = cursor;
        while cursor < end && bytes[cursor].is_none() {
            cursor += 1;
        }
        missing.push(range_start..cursor);
    }
    missing
}

fn model_range_count(model: &[[Option<u8>; SOURCE_LENGTH]; KEY_COUNT]) -> usize {
    model
        .iter()
        .map(|bytes| {
            bytes
                .iter()
                .enumerate()
                .filter(|(index, byte)| {
                    byte.is_some() && (*index == 0 || bytes[*index - 1].is_none())
                })
                .count()
        })
        .sum()
}

fn sparse_missing(model: &BTreeMap<usize, u8>, range: Range<usize>) -> Vec<Range<usize>> {
    let mut missing = Vec::new();
    let mut cursor = range.start;
    while cursor < range.end {
        if model.contains_key(&cursor) {
            cursor += 1;
            continue;
        }
        let start = cursor;
        while cursor < range.end && !model.contains_key(&cursor) {
            cursor += 1;
        }
        missing.push(start..cursor);
    }
    missing
}

fn sparse_range_count(model: &BTreeMap<usize, u8>) -> usize {
    model
        .keys()
        .scan(None, |previous, &offset| {
            let starts_range = previous.is_none_or(|previous| previous + 1 != offset);
            *previous = Some(offset);
            Some(starts_range)
        })
        .filter(|starts_range| *starts_range)
        .count()
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(256))]

    #[test]
    fn randomized_operations_match_reference_coverage(
        operations in prop::collection::vec(operation(), 1..200),
    ) {
        let cache = RangeCache::new(CacheCapacity::Unbounded);
        let mut model = [[None; SOURCE_LENGTH]; KEY_COUNT];

        for (operation, key, first, second, value) in operations {
            let start = first.min(second);
            let end = first.max(second);
            match operation {
                Operation::Insert => {
                    let already_covered = model[key][start..end].iter().all(Option::is_some);
                    let payload = Bytes::from(
                        (start..end)
                            .map(|offset| {
                                value.wrapping_add(
                                    u8::try_from(offset).expect("model offset fits in u8"),
                                )
                            })
                            .collect::<Vec<_>>(),
                    );
                    let outcome = cache.insert(key, start..end, payload).expect("valid insert");
                    prop_assert_eq!(
                        outcome,
                        if already_covered {
                            InsertOutcome::AlreadyCovered
                        } else {
                            InsertOutcome::Inserted
                        }
                    );
                    if !already_covered {
                        for (offset, byte) in model[key][start..end].iter_mut().enumerate() {
                            *byte = Some(value.wrapping_add(
                                u8::try_from(start + offset).expect("model offset fits in u8"),
                            ));
                        }
                    }
                }
                Operation::Get => {
                    let expected = model[key][start..end]
                        .iter()
                        .copied()
                        .collect::<Option<Vec<_>>>()
                        .map(Bytes::from);
                    prop_assert_eq!(cache.get(&key, start..end).expect("valid range"), expected);
                }
                Operation::Missing => {
                    prop_assert_eq!(
                        cache.missing_ranges(&key, start..end).expect("valid range"),
                        model_missing(&model[key], start, end)
                    );
                }
                Operation::Invalidate => {
                    let _ = cache.invalidate(&key);
                    model[key].fill(None);
                }
            }

            let snapshot = cache.snapshot();
            let resident_bytes = model
                .iter()
                .flatten()
                .filter(|byte| byte.is_some())
                .count();
            prop_assert_eq!(snapshot.resident_bytes, resident_bytes);
            prop_assert_eq!(
                snapshot.keys,
                model.iter().filter(|bytes| bytes.iter().any(Option::is_some)).count()
            );
            prop_assert_eq!(snapshot.ranges, model_range_count(&model));
        }
    }

    #[test]
    fn bounded_cache_matches_reference_lru(
        operations in prop::collection::vec(bounded_operation(), 1..200),
    ) {
        let cache = RangeCache::new(CacheCapacity::Bounded(
            NonZeroUsize::new(8).expect("test capacity is non-zero"),
        ));
        let mut access_by_range = [[None; SLOT_COUNT]; BOUNDED_KEY_COUNT];
        let mut next_access = 0_u64;
        let mut hits = 0_u64;
        let mut partial_hits = 0_u64;
        let mut misses = 0_u64;
        let mut insertions = 0_u64;
        let mut evictions = 0_u64;

        for (operation, key, first, second) in operations {
            let first_slot = first.min(second);
            let last_slot = first.max(second);
            let range = first_slot * 4..last_slot * 4 + 2;
            match operation {
                BoundedOperation::Insert => {
                    let slot = first;
                    let block_start = slot * 4;
                    let payload = Bytes::from(vec![
                        u8::try_from(key).expect("key fits in u8"),
                        u8::try_from(slot).expect("slot fits in u8"),
                    ]);
                    let outcome = cache
                        .insert(key, block_start..block_start + 2, payload)
                        .expect("valid insert");
                    if access_by_range[key][slot].is_some() {
                        prop_assert_eq!(outcome, InsertOutcome::AlreadyCovered);
                    } else {
                        prop_assert_eq!(outcome, InsertOutcome::Inserted);
                        access_by_range[key][slot] = Some(next_access);
                        next_access += 1;
                        insertions += 1;

                        if access_by_range.iter().flatten().flatten().count() > 4 {
                            let (oldest_key, oldest_slot, _) = access_by_range
                                .iter()
                                .enumerate()
                                .flat_map(|(key, ranges)| {
                                    ranges.iter().enumerate().filter_map(
                                        move |(slot, access)| {
                                            access.map(|access| (key, slot, access))
                                        },
                                    )
                                })
                                .min_by_key(|(_, _, access)| *access)
                                .expect("an over-capacity model has an oldest entry");
                            access_by_range[oldest_key][oldest_slot] = None;
                            evictions += 1;
                        }
                    }
                }
                BoundedOperation::Get => {
                    let resident_slots = (first_slot..=last_slot)
                        .filter(|&slot| access_by_range[key][slot].is_some())
                        .collect::<Vec<_>>();
                    let expected = (first_slot == last_slot
                        && access_by_range[key][first_slot].is_some())
                    .then(|| {
                        Bytes::from(vec![
                            u8::try_from(key).expect("key fits in u8"),
                            u8::try_from(first_slot).expect("slot fits in u8"),
                        ])
                    });
                    let is_hit = expected.is_some();
                    prop_assert_eq!(cache.get(&key, range).expect("valid range"), expected);
                    if is_hit {
                        access_by_range[key][first_slot] = Some(next_access);
                        next_access += 1;
                        hits += 1;
                    } else if resident_slots.is_empty() {
                        misses += 1;
                    } else {
                        for slot in resident_slots {
                            access_by_range[key][slot] = Some(next_access);
                            next_access += 1;
                        }
                        partial_hits += 1;
                    }
                }
                BoundedOperation::Missing => {
                    let mut expected = Vec::new();
                    let mut cursor = range.start;
                    for (slot, access) in access_by_range[key]
                        .iter()
                        .enumerate()
                        .take(last_slot + 1)
                        .skip(first_slot)
                    {
                        let start = slot * 4;
                        if access.is_none() {
                            continue;
                        }
                        if cursor < start {
                            expected.push(cursor..start);
                        }
                        cursor = start + 2;
                    }
                    if cursor < range.end {
                        expected.push(cursor..range.end);
                    }
                    prop_assert_eq!(
                        cache.missing_ranges(&key, range).expect("valid range"),
                        expected
                    );
                }
                BoundedOperation::Invalidate => {
                    let ranges = access_by_range[key].iter().flatten().count();
                    let expected = Invalidation {
                        ranges,
                        bytes: ranges * 2,
                    };
                    prop_assert_eq!(cache.invalidate(&key), expected);
                    access_by_range[key].fill(None);
                }
                BoundedOperation::Clear => {
                    let ranges = access_by_range.iter().flatten().flatten().count();
                    prop_assert_eq!(
                        cache.clear(),
                        Invalidation {
                            ranges,
                            bytes: ranges * 2,
                        }
                    );
                    access_by_range.fill([None; SLOT_COUNT]);
                }
            }

            let ranges = access_by_range.iter().flatten().flatten().count();
            let snapshot = cache.snapshot();
            prop_assert_eq!(snapshot.resident_bytes, ranges * 2);
            prop_assert_eq!(
                snapshot.keys,
                access_by_range
                    .iter()
                    .filter(|ranges| ranges.iter().any(Option::is_some))
                    .count()
            );
            prop_assert_eq!(snapshot.ranges, ranges);
            prop_assert_eq!(snapshot.hits, hits);
            prop_assert_eq!(snapshot.partial_hits, partial_hits);
            prop_assert_eq!(snapshot.misses, misses);
            prop_assert_eq!(snapshot.insertions, insertions);
            prop_assert_eq!(snapshot.admissions_rejected_too_large, 0);
            prop_assert_eq!(snapshot.evictions, evictions);
        }
    }

    #[test]
    fn sparse_ranges_match_reference_coverage(
        operations in prop::collection::vec(sparse_operation(), 1..100),
    ) {
        let cache = RangeCache::new(CacheCapacity::Unbounded);
        let mut model = BTreeMap::new();

        for (operation, start, length, value) in operations {
            let end = start + length;
            match operation {
                SparseOperation::Insert => {
                    let already_covered = (start..end).all(|offset| model.contains_key(&offset));
                    let outcome = cache
                        .insert((), start..end, Bytes::from(vec![value; length]))
                        .expect("valid insert");
                    prop_assert_eq!(
                        outcome,
                        if already_covered {
                            InsertOutcome::AlreadyCovered
                        } else {
                            InsertOutcome::Inserted
                        }
                    );
                    if !already_covered {
                        model.extend((start..end).map(|offset| (offset, value)));
                    }
                }
                SparseOperation::Get => {
                    let expected = (start..end)
                        .map(|offset| model.get(&offset).copied())
                        .collect::<Option<Vec<_>>>()
                        .map(Bytes::from);
                    prop_assert_eq!(cache.get(&(), start..end).expect("valid range"), expected);
                }
                SparseOperation::Missing => {
                    prop_assert_eq!(
                        cache.missing_ranges(&(), start..end).expect("valid range"),
                        sparse_missing(&model, start..end)
                    );
                }
                SparseOperation::Invalidate => {
                    let ranges = sparse_range_count(&model);
                    prop_assert_eq!(
                        cache.invalidate(&()),
                        Invalidation {
                            ranges,
                            bytes: model.len(),
                        }
                    );
                    model.clear();
                }
            }

            let snapshot = cache.snapshot();
            prop_assert_eq!(snapshot.resident_bytes, model.len());
            prop_assert_eq!(snapshot.keys, usize::from(!model.is_empty()));
            prop_assert_eq!(snapshot.ranges, sparse_range_count(&model));
        }
    }
}