use std::collections::{HashMap, VecDeque};
use std::hash::Hash;
use std::ptr::NonNull;
use std::marker::PhantomData;
use crate::{CachePolicy, PrefetchStrategy};
use crate::prefetch::{PrefetchType, NoPrefetch};
use super::{BenchmarkablePolicy, PolicyType};
pub struct TwoQCache<K, V>
where
K: Hash + Eq + Clone,
V: Clone,
{
a1: VecDeque<K>,
a1_map: HashMap<K, NonNull<Node<K, V>>>,
am_map: HashMap<K, NonNull<Node<K, V>>>,
am_head: Option<NonNull<Node<K, V>>>,
am_tail: Option<NonNull<Node<K, V>>>,
a1out: VecDeque<K>,
capacity: usize,
a1_capacity: usize, a1out_capacity: usize, am_capacity: usize,
a1_size: usize,
am_size: usize,
prefetch_strategy: Box<dyn PrefetchStrategy<K>>,
prefetch_buffer: HashMap<K, V>,
prefetch_buffer_size: usize,
prefetch_stats: super::lru::PrefetchStats,
_marker: PhantomData<Box<Node<K, V>>>,
}
struct Node<K, V> {
key: K,
value: V,
prev: Option<NonNull<Node<K, V>>>,
next: Option<NonNull<Node<K, V>>>,
}
impl<K, V> Node<K, V> {
fn new(key: K, value: V) -> Self {
Self {
key,
value,
prev: None,
next: None,
}
}
}
impl<K, V> TwoQCache<K, V>
where
K: Hash + Eq + Clone,
V: Clone,
{
pub fn new(capacity: usize) -> Self {
Self::with_custom_prefetch(capacity, Box::new(NoPrefetch))
}
pub fn with_custom_prefetch(
capacity: usize,
prefetch_strategy: Box<dyn PrefetchStrategy<K>>
) -> Self {
assert!(capacity > 0, "2Q cache capacity must be greater than 0");
let a1_capacity = (capacity / 4).max(1);
let a1out_capacity = (capacity / 2).max(1);
let am_capacity = capacity - a1_capacity;
Self {
a1: VecDeque::new(),
a1_map: HashMap::new(),
am_map: HashMap::new(),
am_head: None,
am_tail: None,
a1out: VecDeque::new(),
capacity,
a1_capacity,
a1out_capacity,
am_capacity,
a1_size: 0,
am_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();
}
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.a1_map.contains_key(&predicted_key) &&
!self.am_map.contains_key(&predicted_key) &&
!self.prefetch_buffer.contains_key(&predicted_key) {
}
}
self.trim_prefetch_buffer();
}
fn trim_prefetch_buffer(&mut self) {
while self.prefetch_buffer.len() > self.prefetch_buffer_size {
if let Some(key) = self.prefetch_buffer.keys().next().cloned() {
self.prefetch_buffer.remove(&key);
} else {
break;
}
}
}
unsafe fn move_am_to_front(&mut self, node_ptr: NonNull<Node<K, V>>) {
if self.am_head == Some(node_ptr) {
return;
}
unsafe { self.remove_am_from_list(node_ptr) };
unsafe { self.add_am_to_front(node_ptr) };
}
unsafe fn remove_am_from_list(&mut self, node_ptr: NonNull<Node<K, V>>) {
let node = unsafe { node_ptr.as_ref() };
if let Some(mut prev) = node.prev {
unsafe { prev.as_mut() }.next = node.next;
} else {
self.am_head = node.next;
}
if let Some(mut next) = node.next {
unsafe { next.as_mut() }.prev = node.prev;
} else {
self.am_tail = node.prev;
}
}
unsafe fn add_am_to_front(&mut self, mut node_ptr: NonNull<Node<K, V>>) {
let node = unsafe { node_ptr.as_mut() };
node.prev = None;
node.next = self.am_head;
if let Some(mut old_head) = self.am_head {
unsafe { old_head.as_mut() }.prev = Some(node_ptr);
} else {
self.am_tail = Some(node_ptr);
}
self.am_head = Some(node_ptr);
}
fn evict_am_lru(&mut self) -> Option<K> {
if let Some(tail_ptr) = self.am_tail {
unsafe {
let tail_node = Box::from_raw(tail_ptr.as_ptr());
let key = tail_node.key.clone();
self.am_map.remove(&key);
self.am_tail = tail_node.prev;
if let Some(mut new_tail) = self.am_tail {
new_tail.as_mut().next = None;
} else {
self.am_head = None;
}
self.am_size -= 1;
Some(key)
}
} else {
None
}
}
fn reclaim(&mut self) {
if self.a1_size >= self.a1_capacity {
if let Some(evicted_key) = self.a1.pop_front() {
if let Some(node_ptr) = self.a1_map.remove(&evicted_key) {
unsafe {
let _node = Box::from_raw(node_ptr.as_ptr());
}
self.a1_size -= 1;
self.a1out.push_back(evicted_key);
if self.a1out.len() > self.a1out_capacity {
self.a1out.pop_front();
}
}
}
}
if self.am_size >= self.am_capacity {
self.evict_am_lru();
}
}
}
impl<K, V> CachePolicy<K, V> for TwoQCache<K, V>
where
K: Hash + Eq + Clone,
V: Clone,
{
fn get(&mut self, key: &K) -> Option<&V> {
if let Some(_) = self.prefetch_buffer.get(key) {
if let Some(value) = self.prefetch_buffer.remove(key) {
self.prefetch_stats.cache_hits_from_prefetch += 1;
self.insert(key.clone(), value);
return self.get(key);
}
}
if let Some(&node_ptr) = self.a1_map.get(key) {
unsafe {
self.perform_prefetch(key);
return Some(&node_ptr.as_ref().value);
}
}
if let Some(&node_ptr) = self.am_map.get(key) {
unsafe {
self.move_am_to_front(node_ptr);
self.perform_prefetch(key);
return Some(&node_ptr.as_ref().value);
}
}
None
}
fn insert(&mut self, key: K, value: V) {
self.prefetch_buffer.remove(&key);
if let Some(&node_ptr) = self.a1_map.get(&key) {
unsafe {
(*node_ptr.as_ptr()).value = value;
return;
}
}
if let Some(&node_ptr) = self.am_map.get(&key) {
unsafe {
(*node_ptr.as_ptr()).value = value;
self.move_am_to_front(node_ptr);
return;
}
}
if let Some(pos) = self.a1out.iter().position(|x| x == &key) {
self.a1out.remove(pos);
if self.am_size >= self.am_capacity {
self.evict_am_lru();
}
let new_node = Box::new(Node::new(key.clone(), value));
let node_ptr = unsafe { NonNull::new_unchecked(Box::into_raw(new_node)) };
self.am_map.insert(key, node_ptr);
unsafe { self.add_am_to_front(node_ptr); }
self.am_size += 1;
return;
}
self.reclaim();
let new_node = Box::new(Node::new(key.clone(), value));
let node_ptr = unsafe { NonNull::new_unchecked(Box::into_raw(new_node)) };
self.a1_map.insert(key.clone(), node_ptr);
self.a1.push_back(key);
self.a1_size += 1;
}
fn remove(&mut self, key: &K) -> Option<V> {
if let Some(value) = self.prefetch_buffer.remove(key) {
return Some(value);
}
if let Some(node_ptr) = self.a1_map.remove(key) {
unsafe {
let node = Box::from_raw(node_ptr.as_ptr());
if let Some(pos) = self.a1.iter().position(|x| x == key) {
self.a1.remove(pos);
}
self.a1_size -= 1;
return Some(node.value);
}
}
if let Some(node_ptr) = self.am_map.remove(key) {
unsafe {
self.remove_am_from_list(node_ptr);
let node = Box::from_raw(node_ptr.as_ptr());
self.am_size -= 1;
return Some(node.value);
}
}
if let Some(pos) = self.a1out.iter().position(|x| x == key) {
self.a1out.remove(pos);
}
None
}
fn len(&self) -> usize {
self.a1_size + self.am_size
}
fn capacity(&self) -> usize {
self.capacity
}
fn clear(&mut self) {
for (_, node_ptr) in self.a1_map.drain() {
unsafe {
let _node = Box::from_raw(node_ptr.as_ptr());
}
}
for (_, node_ptr) in self.am_map.drain() {
unsafe {
let _node = Box::from_raw(node_ptr.as_ptr());
}
}
self.a1.clear();
self.a1out.clear();
self.am_head = None;
self.am_tail = None;
self.a1_size = 0;
self.am_size = 0;
self.prefetch_buffer.clear();
}
}
impl<K, V> BenchmarkablePolicy<K, V> for TwoQCache<K, V>
where
K: Hash + Eq + Clone,
V: Clone,
{
fn policy_type(&self) -> PolicyType {
PolicyType::TwoQ
}
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 TwoQCache<K, V>
where
K: Hash + Eq + Clone,
V: Clone,
{
fn drop(&mut self) {
self.clear();
}
}
impl TwoQCache<i32, String> {
pub fn with_prefetch_i32(capacity: usize, prefetch_type: PrefetchType) -> Self {
assert!(capacity > 0, "2Q cache capacity must be greater than 0");
let prefetch_strategy = crate::prefetch::create_prefetch_strategy_i32(prefetch_type);
Self::with_custom_prefetch(capacity, prefetch_strategy)
}
}
impl TwoQCache<i64, String> {
pub fn with_prefetch_i64(capacity: usize, prefetch_type: PrefetchType) -> Self {
assert!(capacity > 0, "2Q cache capacity must be greater than 0");
let prefetch_strategy = crate::prefetch::create_prefetch_strategy_i64(prefetch_type);
Self::with_custom_prefetch(capacity, prefetch_strategy)
}
}
impl TwoQCache<usize, String> {
pub fn with_prefetch_usize(capacity: usize, prefetch_type: PrefetchType) -> Self {
assert!(capacity > 0, "2Q cache capacity must be greater than 0");
let prefetch_strategy = crate::prefetch::create_prefetch_strategy_usize(prefetch_type);
Self::with_custom_prefetch(capacity, prefetch_strategy)
}
}
unsafe impl<K, V> Send for TwoQCache<K, V>
where
K: Hash + Eq + Clone + Send,
V: Clone + Send,
{
}
unsafe impl<K, V> Sync for TwoQCache<K, V>
where
K: Hash + Eq + Clone + Sync,
V: Clone + Sync,
{
}