use crate::ast::Program;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::VecDeque;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[derive(Debug)]
pub struct ASTCache {
cache: HashMap<u64, (String, Program)>,
order: VecDeque<u64>,
max_size: usize,
hits: usize,
misses: usize,
}
impl ASTCache {
pub fn new() -> Self {
Self::with_capacity(100)
}
pub fn with_capacity(max_size: usize) -> Self {
ASTCache {
cache: HashMap::with_capacity(max_size),
order: VecDeque::with_capacity(max_size),
max_size,
hits: 0,
misses: 0,
}
}
fn hash_code(code: &str) -> u64 {
let mut hasher = DefaultHasher::new();
code.hash(&mut hasher);
hasher.finish()
}
fn touch(&mut self, hash: u64) {
if let Some(pos) = self.order.iter().position(|&k| k == hash) {
self.order.remove(pos);
}
self.order.push_back(hash);
}
pub fn get(&mut self, code: &str) -> Option<Program> {
let hash = Self::hash_code(code);
let hit = self
.cache
.get(&hash)
.is_some_and(|(cached_code, _)| cached_code == code);
if hit {
self.hits += 1;
self.touch(hash);
self.cache.get(&hash).map(|(_, program)| program.clone())
} else {
self.misses += 1;
None
}
}
pub fn insert(&mut self, code: &str, program: Program) {
let hash = Self::hash_code(code);
if self.cache.contains_key(&hash) {
self.touch(hash);
} else {
while self.cache.len() >= self.max_size {
if let Some(lru) = self.order.pop_front() {
self.cache.remove(&lru);
} else {
break;
}
}
self.order.push_back(hash);
}
self.cache.insert(hash, (code.to_string(), program));
}
pub fn clear(&mut self) {
self.cache.clear();
self.order.clear();
self.hits = 0;
self.misses = 0;
}
pub fn stats(&self) -> CacheStats {
CacheStats {
size: self.cache.len(),
max_size: self.max_size,
hits: self.hits,
misses: self.misses,
hit_rate: if self.hits + self.misses > 0 {
self.hits as f64 / (self.hits + self.misses) as f64
} else {
0.0
},
}
}
}
impl Default for ASTCache {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheStats {
pub size: usize,
pub max_size: usize,
pub hits: usize,
pub misses: usize,
pub hit_rate: f64,
}
impl std::fmt::Display for CacheStats {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"Cache Stats: size={}/{}, hits={}, misses={}, hit_rate={:.2}%",
self.size,
self.max_size,
self.hits,
self.misses,
self.hit_rate * 100.0
)
}
}