Skip to content

Commit

Permalink
fix tlfu small size (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiling-J authored Sep 2, 2023
1 parent 21382fb commit 9b88433
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ impl LruCore {

#[cfg(test)]
mod tests {
use crate::core::TlfuCore;

use super::LruCore;

#[test]
Expand All @@ -310,4 +312,15 @@ mod tests {
assert_eq!("cdefg", lru.policy.link.display(false, &lru.metadata));
assert_eq!(5, lru.metadata.len());
}

#[test]
fn test_tlfu_core_size_small() {
for size in [1, 2, 3] {
let mut tlfu = TlfuCore::new(size);
for s in ["a", "b", "c", "d", "e", "f", "g", "g", "g"] {
tlfu.set(s, 0);
}
assert_eq!(size, tlfu.metadata.len());
}
}
}
6 changes: 6 additions & 0 deletions src/lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ impl Slru {
}

pub fn insert(&mut self, index: u32, metadata: &mut MetaData) -> Option<u32> {
if self.maxsize == 0 {
return Some(index);
}
if self.protected.len + self.probation.len >= self.maxsize as u32 {
if let Some(evicted) = self.probation.pop_tail(metadata) {
self.probation.insert_front(index, metadata);
Expand All @@ -67,6 +70,9 @@ impl Slru {
}

pub fn victim(&mut self, metadata: &mut MetaData) -> Option<u32> {
if self.maxsize == 0 {
return None;
}
if self.probation.len + self.protected.len < self.maxsize as u32 {
return None;
}
Expand Down

0 comments on commit 9b88433

Please sign in to comment.