Skip to content

Commit

Permalink
bench
Browse files Browse the repository at this point in the history
  • Loading branch information
tickbh committed Jun 14, 2024
1 parent c602ac4 commit dcba7d3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
67 changes: 41 additions & 26 deletions examples/bench_lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,62 @@ use std::time::Instant;
use algorithm::{ArcCache, LfuCache, LruCache, LruKCache};

macro_rules! do_test_bench {
($name: expr, $cache: expr, $num: expr, $evict: expr, $data1: expr, $data2: expr) => {
($name: expr, $cache: expr, $num: expr, $evict: expr, $data: expr) => {
let mut cost = vec![];
let now = Instant::now();
for i in 0..$num {
$cache.insert($data1[i], $data1[i]);
let mut all = 0;
let mut hit = 0;
for v in $data {
if v.1 == 0 {
all += 1;
if $cache.get(&v.0).is_some() {
hit += 1;
}
} else {
$cache.insert(v.0, v.1);
}
}
cost.push(now.elapsed().as_micros());
println!("{}\t{}\t{:.2}%", $name, cost.iter().map(|v| v.to_string()).collect::<Vec<_>>().join("\t"), hit as f64 * 100.0 / all as f64);
};
}

let now = Instant::now();
for i in 0..$num {
$cache.get(&$data1[i]);
}
cost.push(now.elapsed().as_micros());

let now = Instant::now();
for i in 0..$num {
$cache.get(&$data2[i]);
fn build_order_data(num: usize) -> Vec<(usize, usize)> {
let mut data = vec![];
for i in 0..num {
data.push((i, i + 1));
data.push((i, 0));
}
data
}

fn build_freq_data(num: usize) -> Vec<(usize, usize)> {
let mut data = vec![];
for i in 0..num {
data.push((i, i + 1));
data.push((i+1, i + 2));
for _ in 0..5 {
data.push((rand::random::<usize>() % (i / 2 + 1), 0));
}
cost.push(now.elapsed().as_micros());
println!("{} 耗时:{}", $name, cost.iter().map(|v| v.to_string()).collect::<Vec<_>>().join("\t"));
};
}
data
}

fn do_bench(num: usize, times: usize) {
fn do_bench(num: usize) {
let evict = num * 2;
let mut data1 = (0..num).collect::<Vec<_>>();
let mut data2 = vec![];
for _ in 0..evict {
data2.push(rand::random::<usize>() % evict);
}
let mut lru = LruCache::<usize, usize, RandomState>::new(num);
let mut lruk = LruKCache::<usize, usize, RandomState>::new(num);
let mut lfu = LfuCache::<usize, usize, RandomState>::new(num);
let mut arc = ArcCache::<usize, usize, RandomState>::new(num);
do_test_bench!("LruCache", lru, num, evict, &data1, &data2);
do_test_bench!("LruKCache", lruk, num, evict, &data1, &data2);
do_test_bench!("LfuCache", lfu, num, evict, &data1, &data2);
do_test_bench!("ArcCache", arc, num, evict, &data1, &data2);
println!("名字\t插入\t读取\t");
let order_data = build_freq_data(evict);
do_test_bench!("LruCache", lru, num, evict, &order_data);
do_test_bench!("LruKCache", lruk, num, evict, &order_data);
do_test_bench!("LfuCache", lfu, num, evict, &order_data);
do_test_bench!("ArcCache", arc, num, evict, &order_data);
// println!("耗时:{}", set_timer);
}

fn main() {
do_bench(1e5 as usize, 5);
do_bench(1e4 as usize);
}
26 changes: 17 additions & 9 deletions src/cache/lfu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<K, V, S> LfuCache<K, V, S> {
/// }
/// ```
pub fn set_default_count(&mut self, default_count: usize) {
self.default_count = default_count;
self.default_count = default_count.saturating_sub(1);
}

pub fn get_default_count(&self) -> usize {
Expand Down Expand Up @@ -364,6 +364,18 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
}
}

fn try_fix_entry(&mut self, entry: *mut LfuEntry<K, V>) {
unsafe {
if get_freq_by_times((*entry).counter) != get_freq_by_times((*entry).counter + 1) {
self.visit_count += 1;
(*entry).counter += 1;
} else {
self.detach(entry);
self.attach(entry);
}
}
}

/// 从队列中节点剥离
fn detach(&mut self, entry: *mut LfuEntry<K, V>) {
unsafe {
Expand Down Expand Up @@ -624,8 +636,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
match self.map.get(KeyWrapper::from_ref(k)) {
Some(l) => {
let node = l.as_ptr();
self.detach(node);
self.attach(node);
self.try_fix_entry(node);
unsafe { Some((&*(*node).key.as_ptr(), &*(*node).val.as_ptr())) }
}
None => None,
Expand Down Expand Up @@ -660,9 +671,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
match self.map.get(KeyWrapper::from_ref(k)) {
Some(l) => {
let node = l.as_ptr();

self.detach(node);
self.attach(node);
self.try_fix_entry(node);
unsafe { Some((&*(*node).key.as_ptr(), &mut *(*node).val.as_mut_ptr())) }
}
None => None,
Expand Down Expand Up @@ -692,8 +701,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
unsafe {
mem::swap(&mut *(*entry_ptr).val.as_mut_ptr(), &mut v);
}
self.detach(entry_ptr);
self.attach(entry_ptr);
self.try_fix_entry(entry_ptr);

Some((k, v, true))
}
Expand Down Expand Up @@ -768,7 +776,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LfuCache<K, V, S> {
NonNull::new_unchecked(Box::into_raw(Box::new(LfuEntry::new_counter(
k,
v,
self.default_count.saturating_sub(1),
self.default_count,
))))
})
}
Expand Down

0 comments on commit dcba7d3

Please sign in to comment.