fulgurance 0.4.1

A blazing-fast, adaptive prefetching and caching library for Rust
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
541
542
543
use std::collections::HashMap;
use std::hash::Hash;
use std::marker::PhantomData;

use crate::{CachePolicy, PrefetchStrategy};
use crate::prefetch::{PrefetchType, NoPrefetch};
use super::{BenchmarkablePolicy, PolicyType};

/// Clock with Adaptive Replacement (CAR) cache
///
/// CAR combines the Clock algorithm with ARC’s adaptive mechanism.
/// Like ARC, it maintains four lists (two actual cache lists + two
/// ghost lists) but uses Clock replacement instead of strict LRU.
/// This provides adaptive behavior with lower overhead.
pub struct CarCache<K, V>
where
    K: Hash + Eq + Clone,
    V: Clone,
{
    // T1: Recent entries (Clock-managed)
    t1: Vec<Option<CarEntry<K, V>>>,
    t1_map: HashMap<K, usize>,
    t1_hand: usize,
    t1_size: usize,

    // T2: Frequent entries (Clock-managed)
    t2: Vec<Option<CarEntry<K, V>>>,
    t2_map: HashMap<K, usize>,
    t2_hand: usize,
    t2_size: usize,

    // Ghost buffers store only keys, used for adaptation
    b1: HashMap<K, ()>, // Ghost buffer for T1 evictions
    b2: HashMap<K, ()>, // Ghost buffer for T2 evictions

    // Adaptation parameter (target size of T1)
    p: usize,

    // Capacity and current size
    capacity: usize,
    current_size: usize,

    // Integrated prefetch support
    prefetch_strategy: Box<dyn PrefetchStrategy<K>>,
    prefetch_buffer: HashMap<K, V>,
    prefetch_buffer_size: usize,
    prefetch_stats: super::lru::PrefetchStats,

    _marker: PhantomData<(K, V)>,
}

/// An entry managed by the CAR cache
#[derive(Clone)]
struct CarEntry<K, V> {
    key: K,
    value: V,
    reference_bit: bool, // Clock reference bit
    #[allow(dead_code)]
    list_type: ListType, // Indicates whether in T1 or T2
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum ListType {
    T1,
    T2,
}

impl<K, V> CarEntry<K, V> {
    fn new(key: K, value: V, list_type: ListType) -> Self {
        Self {
            key,
            value,
            reference_bit: true,
            list_type,
        }
    }
}

impl<K, V> CarCache<K, V>
where
    K: Hash + Eq + Clone,
    V: Clone,
{
    /// Creates a new CAR cache with no prefetch
    pub fn new(capacity: usize) -> Self {
        Self::with_custom_prefetch(capacity, Box::new(NoPrefetch))
    }

    /// Creates a new CAR cache with given prefetch strategy
    pub fn with_custom_prefetch(
        capacity: usize,
        prefetch_strategy: Box<dyn PrefetchStrategy<K>>,
    ) -> Self {
        assert!(capacity > 0, "CAR cache capacity must be greater than 0");

        let t1_capacity = capacity;
        let t2_capacity = capacity;

        Self {
            t1: vec![None; t1_capacity],
            t1_map: HashMap::new(),
            t1_hand: 0,
            t1_size: 0,

            t2: vec![None; t2_capacity],
            t2_map: HashMap::new(),
            t2_hand: 0,
            t2_size: 0,

            b1: HashMap::new(),
            b2: HashMap::new(),

            p: 0,
            capacity,
            current_size: 0,

            prefetch_strategy,
            prefetch_buffer: HashMap::new(),
            prefetch_buffer_size: (capacity / 4).max(1),
            prefetch_stats: super::lru::PrefetchStats::default(),

            _marker: PhantomData,
        }
    }

    pub fn prefetch_stats(&self) -> &super::lru::PrefetchStats {
        &self.prefetch_stats
    }

    pub fn reset_prefetch_stats(&mut self) {
        self.prefetch_stats = super::lru::PrefetchStats::default();
        self.prefetch_strategy.reset();
    }

    /// Execute prefetch prediction after an access
    fn perform_prefetch(&mut self, accessed_key: &K) {
        self.prefetch_strategy.update_access_pattern(accessed_key);
        let predictions = self.prefetch_strategy.predict_next(accessed_key);

        for predicted_key in predictions {
            self.prefetch_stats.predictions_made += 1;
            if !self.t1_map.contains_key(&predicted_key)
                && !self.t2_map.contains_key(&predicted_key)
                    && !self.prefetch_buffer.contains_key(&predicted_key)
            {
                // Prediction stub (not actually loading entry)
            }
        }
        self.trim_prefetch_buffer();
    }

    fn trim_prefetch_buffer(&mut self) {
        while self.prefetch_buffer.len() > self.prefetch_buffer_size {
            if let Some(k) = self.prefetch_buffer.keys().next().cloned() {
                self.prefetch_buffer.remove(&k);
            } else {
                break;
            }
        }
    }

    /// Update adaptation parameter `p`
    fn update_p(&mut self, delta: i32) {
        if delta > 0 {
            self.p = (self.p + delta as usize).min(self.capacity);
        } else {
            self.p = self.p.saturating_sub((-delta) as usize);
        }
    }

    /// Advance T1 hand (Clock algorithm)
    fn advance_t1_hand(&mut self) -> Option<usize> {
        if self.t1_size == 0 {
            return None;
        }
        let start_pos = self.t1_hand;
        loop {
            let cur = self.t1_hand;
            self.t1_hand = (self.t1_hand + 1) % self.t1.len();

            if let Some(ref mut entry) = self.t1[cur] {
                if entry.reference_bit {
                    entry.reference_bit = false;
                } else {
                    return Some(cur);
                }
            }
            if self.t1_hand == start_pos {
                break;
            }
        }
        None
    }

    /// Advance T2 hand (Clock algorithm)
    fn advance_t2_hand(&mut self) -> Option<usize> {
        if self.t2_size == 0 {
            return None;
        }
        let start_pos = self.t2_hand;
        loop {
            let cur = self.t2_hand;
            self.t2_hand = (self.t2_hand + 1) % self.t2.len();

            if let Some(ref mut entry) = self.t2[cur] {
                if entry.reference_bit {
                    entry.reference_bit = false;
                } else {
                    return Some(cur);
                }
            }
            if self.t2_hand == start_pos {
                break;
            }
        }
        None
    }

    /// Find empty slots for T1 or T2
    fn find_empty_t1_slot(&self) -> Option<usize> {
        self.t1.iter().position(|s| s.is_none())
    }
    fn find_empty_t2_slot(&self) -> Option<usize> {
        self.t2.iter().position(|s| s.is_none())
    }

    /// Replacement procedure (eviction) for CAR
    fn replace(&mut self, in_b2: bool) -> bool {
        if self.t1_size >= 1 && ((in_b2 && self.t1_size == self.p) || self.t1_size > self.p) {
            if let Some(victim) = self.advance_t1_hand() {
                if let Some(entry) = self.t1[victim].take() {
                    self.t1_map.remove(&entry.key);
                    self.b1.insert(entry.key, ());
                    self.t1_size -= 1;
                    self.current_size -= 1;
                    return true;
                }
            }
        } else {
            if let Some(victim) = self.advance_t2_hand() {
                if let Some(entry) = self.t2[victim].take() {
                    self.t2_map.remove(&entry.key);
                    self.b2.insert(entry.key, ());
                    self.t2_size -= 1;
                    self.current_size -= 1;
                    return true;
                }
            }
        }
        false
    }

    /// Trim ghost buffers to at most capacity
    fn trim_ghost_buffers(&mut self) {
        let max = self.capacity;
        while self.b1.len() > max {
            if let Some(k) = self.b1.keys().next().cloned() {
                self.b1.remove(&k);
            } else {
                break;
            }
        }
        while self.b2.len() > max {
            if let Some(k) = self.b2.keys().next().cloned() {
                self.b2.remove(&k);
            } else {
                break;
            }
        }
    }
}

impl<K, V> CachePolicy<K, V> for CarCache<K, V>
where
    K: Hash + Eq + Clone,
    V: Clone,
{
    fn get(&mut self, key: &K) -> Option<&V> {
        // Prefetch buffer
        if let Some(_) = self.prefetch_buffer.get(key) {
            if let Some(val) = self.prefetch_buffer.remove(key) {
                self.prefetch_stats.cache_hits_from_prefetch += 1;
                self.insert(key.clone(), val);
                return self.get(key);
            }
        }

        // T1
        if let Some(&idx) = self.t1_map.get(key) {
            if let Some(entry) = self.t1[idx].take() {
                // Promote to T2
                self.t1_map.remove(key);
                self.t1_size -= 1;

                // Always allocate in T2
                let mut new_entry = CarEntry::new(entry.key.clone(), entry.value, ListType::T2);
                new_entry.reference_bit = true;

                if let Some(slot) = self.find_empty_t2_slot() {
                    self.t2[slot] = Some(new_entry);
                    self.t2_map.insert(key.clone(), slot);
                    self.t2_size += 1;
                    self.perform_prefetch(key);
                    return self.t2[slot].as_ref().map(|e| &e.value);
                } else if let Some(victim) = self.advance_t2_hand() {
                    if let Some(old) = self.t2[victim].take() {
                        self.t2_map.remove(&old.key);
                        self.b2.insert(old.key, ());
                        self.t2_size -= 1;
                        self.current_size -= 1;
                    }
                    self.t2[victim] = Some(new_entry);
                    self.t2_map.insert(key.clone(), victim);
                    self.t2_size += 1;
                    self.current_size += 1;
                    self.perform_prefetch(key);
                    return self.t2[victim].as_ref().map(|e| &e.value);
                }
            }
        }

        // T2
        if let Some(&idx) = self.t2_map.get(key) {
            // Borrow-scope trick to avoid conflict
            let value_ptr: *const V;
            {
                if let Some(ref mut entry) = self.t2[idx] {
                    entry.reference_bit = true;
                    value_ptr = &entry.value;
                } else {
                    return None;
                }
            }
            self.perform_prefetch(key);
            return Some(unsafe { &*value_ptr });
        }

        None
    }

    fn insert(&mut self, key: K, value: V) {
        self.prefetch_buffer.remove(&key);

        // Case 1: Already exists
        if let Some(&idx) = self.t1_map.get(&key) {
            if let Some(ref mut entry) = self.t1[idx] {
                entry.value = value;
                entry.reference_bit = true;
            }
            return;
        }
        if let Some(&idx) = self.t2_map.get(&key) {
            if let Some(ref mut entry) = self.t2[idx] {
                entry.value = value;
                entry.reference_bit = true;
            }
            return;
        }

        // Case 2: History hits (B1 or B2)
        if self.b1.contains_key(&key) {
            let delta = (self.b2.len() as f32 / self.b1.len().max(1) as f32).ceil() as i32;
            self.update_p(delta);
            if self.current_size >= self.capacity {
                self.replace(false);
            }
            self.b1.remove(&key);

            let new_entry = CarEntry::new(key.clone(), value, ListType::T2);
            if let Some(slot) = self.find_empty_t2_slot() {
                self.t2[slot] = Some(new_entry);
                self.t2_map.insert(key, slot);
                self.t2_size += 1;
                self.current_size += 1;
            }
            self.trim_ghost_buffers();
            return;
        }

        if self.b2.contains_key(&key) {
            let delta = (self.b1.len() as f32 / self.b2.len().max(1) as f32).ceil() as i32;
            self.update_p(-delta);
            if self.current_size >= self.capacity {
                self.replace(true);
            }
            self.b2.remove(&key);

            let new_entry = CarEntry::new(key.clone(), value, ListType::T2);
            if let Some(slot) = self.find_empty_t2_slot() {
                self.t2[slot] = Some(new_entry);
                self.t2_map.insert(key, slot);
                self.t2_size += 1;
                self.current_size += 1;
            }
            self.trim_ghost_buffers();
            return;
        }

        // Case 3: New entry
        let total_cache = self.t1_size + self.t2_size;
        if total_cache < self.capacity {
            if total_cache + self.b1.len() + self.b2.len() >= self.capacity {
                if self.b1.len() > self.b2.len() {
                    if let Some(k) = self.b1.keys().next().cloned() {
                        self.b1.remove(&k);
                    }
                } else if let Some(k) = self.b2.keys().next().cloned() {
                    self.b2.remove(&k);
                }
            }

            let new_entry = CarEntry::new(key.clone(), value, ListType::T1);
            if let Some(slot) = self.find_empty_t1_slot() {
                self.t1[slot] = Some(new_entry);
                self.t1_map.insert(key, slot);
                self.t1_size += 1;
                self.current_size += 1;
            }
        } else {
            self.replace(false);
            let new_entry = CarEntry::new(key.clone(), value, ListType::T1);
            if let Some(slot) = self.find_empty_t1_slot() {
                self.t1[slot] = Some(new_entry);
                self.t1_map.insert(key, slot);
                self.t1_size += 1;
                self.current_size += 1;
            }
        }
        self.trim_ghost_buffers();
    }

    fn remove(&mut self, key: &K) -> Option<V> {
        if let Some(val) = self.prefetch_buffer.remove(key) {
            return Some(val);
        }
        if let Some(idx) = self.t1_map.remove(key) {
            if let Some(entry) = self.t1[idx].take() {
                self.t1_size -= 1;
                self.current_size -= 1;
                return Some(entry.value);
            }
        }
        if let Some(idx) = self.t2_map.remove(key) {
            if let Some(entry) = self.t2[idx].take() {
                self.t2_size -= 1;
                self.current_size -= 1;
                return Some(entry.value);
            }
        }
        self.b1.remove(key);
        self.b2.remove(key);
        None
    }

    fn len(&self) -> usize {
        self.current_size
    }

    fn capacity(&self) -> usize {
        self.capacity
    }

    fn clear(&mut self) {
        for s in &mut self.t1 {
            *s = None;
        }
        for s in &mut self.t2 {
            *s = None;
        }
        self.t1_map.clear();
        self.t2_map.clear();
        self.b1.clear();
        self.b2.clear();
        self.t1_hand = 0;
        self.t2_hand = 0;
        self.t1_size = 0;
        self.t2_size = 0;
        self.current_size = 0;
        self.p = 0;
        self.prefetch_buffer.clear();
    }
}

impl<K, V> BenchmarkablePolicy<K, V> for CarCache<K, V>
where
    K: Hash + Eq + Clone,
    V: Clone,
{
    fn policy_type(&self) -> PolicyType {
        PolicyType::Car
    }

    fn benchmark_name(&self) -> String {
        format!("{}_cap_{}_prefetch", self.policy_type().name(), self.capacity())
    }

    fn reset_for_benchmark(&mut self) {
        self.clear();
        self.reset_prefetch_stats();
    }
}

impl<K, V> Drop for CarCache<K, V>
where
    K: Hash + Eq + Clone,
    V: Clone,
{
    fn drop(&mut self) {
        self.clear();
    }
}

// Specialized constructors
impl CarCache<i32, String> {
    pub fn with_prefetch_i32(capacity: usize, prefetch_type: PrefetchType) -> Self {
        let strat = crate::prefetch::create_prefetch_strategy_i32(prefetch_type);
        Self::with_custom_prefetch(capacity, strat)
    }
}
impl CarCache<i64, String> {
    pub fn with_prefetch_i64(capacity: usize, prefetch_type: PrefetchType) -> Self {
        let strat = crate::prefetch::create_prefetch_strategy_i64(prefetch_type);
        Self::with_custom_prefetch(capacity, strat)
    }
}
impl CarCache<usize, String> {
    pub fn with_prefetch_usize(capacity: usize, prefetch_type: PrefetchType) -> Self {
        let strat = crate::prefetch::create_prefetch_strategy_usize(prefetch_type);
        Self::with_custom_prefetch(capacity, strat)
    }
}

unsafe impl<K, V> Send for CarCache<K, V>
where
    K: Hash + Eq + Clone + Send,
    V: Clone + Send,
{}
unsafe impl<K, V> Sync for CarCache<K, V>
where
    K: Hash + Eq + Clone + Sync,
    V: Clone + Sync,
{}