From 2bc06713a3f48f35c32fe0a2d630d7a97594c970 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Sun, 3 Dec 2023 03:43:48 +0800 Subject: [PATCH 1/7] feat(cache): a variant of sieve, with lazy op Signed-off-by: Chojan Shang --- src/common/cache/src/cache/lru.rs | 70 ++++++++++++++++++++++---- src/common/cache/tests/it/cache/lru.rs | 8 +-- 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/common/cache/src/cache/lru.rs b/src/common/cache/src/cache/lru.rs index 95945427b968f..cecb39c9ceb2d 100644 --- a/src/common/cache/src/cache/lru.rs +++ b/src/common/cache/src/cache/lru.rs @@ -75,6 +75,7 @@ pub struct LruCache< M: CountableMeter = Count, > { map: LinkedHashMap, + visited: LinkedHashMap, current_measure: M::Measure, max_capacity: u64, meter: M, @@ -92,6 +93,7 @@ impl LruCache { pub fn new(capacity: u64) -> Self { LruCache { map: LinkedHashMap::new(), + visited: LinkedHashMap::new(), current_measure: (), max_capacity: capacity, meter: Count, @@ -136,6 +138,7 @@ impl> LruCache LruCache { LruCache { map: LinkedHashMap::new(), + visited: LinkedHashMap::new(), current_measure: Default::default(), max_capacity: capacity, meter, @@ -148,6 +151,7 @@ impl LruCache { pub fn with_hasher(capacity: u64, hash_builder: S) -> LruCache { LruCache { map: LinkedHashMap::with_hasher(hash_builder), + visited: LinkedHashMap::new(), current_measure: (), max_capacity: capacity, meter: Count, @@ -155,6 +159,22 @@ impl LruCache { } } +impl> LruCache { + fn find_evict_candidate(&mut self) -> Option { + let mut iter = self.visited.iter_mut(); + let mut p: Option = None; + while let Some((key, value)) = iter.next() { + if *value == false && p.is_none() { + p = Some(unsafe { std::ptr::read(key) }) + } + if *value == true { + *value = false; + } + } + p + } +} + impl> Cache for LruCache { @@ -163,6 +183,7 @@ impl> Cache fn with_meter_and_hasher(capacity: u64, meter: M, hash_builder: S) -> Self { LruCache { map: LinkedHashMap::with_hasher(hash_builder), + visited: LinkedHashMap::new(), current_measure: Default::default(), max_capacity: capacity, meter, @@ -196,8 +217,14 @@ impl> Cache Q: Hash + Eq + ?Sized, { match self.map.raw_entry_mut().from_key(k) { - linked_hash_map::RawEntryMut::Occupied(mut occupied) => { - occupied.to_back(); + linked_hash_map::RawEntryMut::Occupied(occupied) => { + match self.visited.raw_entry_mut().from_key(k) { + // Since the element has been accessed, we set a flag. + linked_hash_map::RawEntryMut::Occupied(mut occupied) => { + occupied.replace_value(true); + } + linked_hash_map::RawEntryMut::Vacant(_) => (), + } Some(occupied.into_mut()) } linked_hash_map::RawEntryMut::Vacant(_) => None, @@ -244,7 +271,7 @@ impl> Cache /// assert_eq!(cache.peek_by_policy(), Some((&1, &"a"))); /// ``` fn peek_by_policy(&self) -> Option<(&K, &V)> { - self.map.front() + todo!() } /// Checks if the map contains the given key. @@ -286,11 +313,21 @@ impl> Cache let new_size = self.meter.measure(&k, &v); self.current_measure = self.meter.add(self.current_measure, new_size); if let Some(old) = self.map.get(&k) { + match self.visited.raw_entry_mut().from_key(&k) { + // Since the key has been accessed, we set a flag. + linked_hash_map::RawEntryMut::Occupied(mut occupied) => { + occupied.replace_value(true); + } + linked_hash_map::RawEntryMut::Vacant(_) => (), + } self.current_measure = self .meter .sub(self.current_measure, self.meter.measure(&k, old)); + } else { + let clone_k = unsafe { std::ptr::read(&k) }; + self.visited.replace(clone_k, false); } - let old_val = self.map.insert(k, v); + let old_val = self.map.replace(k, v); while self.size() > self.capacity() { self.pop_by_policy(); } @@ -319,6 +356,7 @@ impl> Cache Q: Hash + Eq + ?Sized, { self.map.remove(k).map(|v| { + self.visited.remove(k); self.current_measure = self .meter .sub(self.current_measure, self.meter.measure(k, &v)); @@ -343,12 +381,23 @@ impl> Cache /// ``` #[inline] fn pop_by_policy(&mut self) -> Option<(K, V)> { - self.map.pop_front().map(|(k, v)| { - self.current_measure = self - .meter - .sub(self.current_measure, self.meter.measure(&k, &v)); - (k, v) - }) + if let Some(old_key) = self.find_evict_candidate() { + self.map.remove_entry(&old_key).map(|(k, v)| { + self.visited.remove_entry(&old_key); + self.current_measure = self + .meter + .sub(self.current_measure, self.meter.measure(&k, &v)); + (k, v) + }) + } else { + self.map.pop_front().map(|(k, v)| { + self.visited.pop_front(); + self.current_measure = self + .meter + .sub(self.current_measure, self.meter.measure(&k, &v)); + (k, v) + }) + } } /// Sets the size of the key-value pairs the cache can hold, as measured by the `Meter` used by @@ -427,6 +476,7 @@ impl> Cache /// Removes all key-value pairs from the cache. fn clear(&mut self) { self.map.clear(); + self.visited.clear(); self.current_measure = Default::default(); } } diff --git a/src/common/cache/tests/it/cache/lru.rs b/src/common/cache/tests/it/cache/lru.rs index 3a5321435ea58..5baa5bf643091 100644 --- a/src/common/cache/tests/it/cache/lru.rs +++ b/src/common/cache/tests/it/cache/lru.rs @@ -89,13 +89,13 @@ fn test_debug() { cache.put(3, 30); assert_eq!(format!("{:?}", cache), "{3: 30, 2: 20, 1: 10}"); cache.put(2, 22); - assert_eq!(format!("{:?}", cache), "{2: 22, 3: 30, 1: 10}"); + assert_eq!(format!("{:?}", cache), "{3: 30, 2: 22, 1: 10}"); cache.put(6, 60); - assert_eq!(format!("{:?}", cache), "{6: 60, 2: 22, 3: 30}"); + assert_eq!(format!("{:?}", cache), "{6: 60, 3: 30, 2: 22}"); cache.get(&3); - assert_eq!(format!("{:?}", cache), "{3: 30, 6: 60, 2: 22}"); + assert_eq!(format!("{:?}", cache), "{6: 60, 3: 30, 2: 22}"); cache.set_capacity(2); - assert_eq!(format!("{:?}", cache), "{3: 30, 6: 60}"); + assert_eq!(format!("{:?}", cache), "{6: 60, 3: 30}"); } #[test] From a9594da69d6eb02896d070b2b749633876b12434 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Sun, 3 Dec 2023 04:06:25 +0800 Subject: [PATCH 2/7] feat(cache): add peek by policy Signed-off-by: Chojan Shang --- src/common/cache/src/cache/lru.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/common/cache/src/cache/lru.rs b/src/common/cache/src/cache/lru.rs index cecb39c9ceb2d..b8f6eb904557e 100644 --- a/src/common/cache/src/cache/lru.rs +++ b/src/common/cache/src/cache/lru.rs @@ -161,18 +161,29 @@ impl LruCache { impl> LruCache { fn find_evict_candidate(&mut self) -> Option { - let mut iter = self.visited.iter_mut(); + let iter = self.visited.iter_mut(); let mut p: Option = None; - while let Some((key, value)) = iter.next() { - if *value == false && p.is_none() { + for (key, value) in iter { + if !(*value) && p.is_none() { p = Some(unsafe { std::ptr::read(key) }) } - if *value == true { + if *value { *value = false; } } p } + + fn peek_evict_candidate(&self) -> Option { + let iter = self.visited.iter(); + let mut p: Option = None; + for (key, value) in iter { + if !(*value) && p.is_none() { + p = Some(unsafe { std::ptr::read(key) }) + } + } + p + } } impl> Cache @@ -271,7 +282,11 @@ impl> Cache /// assert_eq!(cache.peek_by_policy(), Some((&1, &"a"))); /// ``` fn peek_by_policy(&self) -> Option<(&K, &V)> { - todo!() + if let Some(old_key) = self.peek_evict_candidate() { + self.map.get_key_value(&old_key) + } else { + self.map.front() + } } /// Checks if the map contains the given key. From a90403f732d049023ebd2155a36fe584a8700f5d Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Sun, 3 Dec 2023 11:49:38 +0800 Subject: [PATCH 3/7] refactor: make key can be clone, so, we can forget unsafe ptr Signed-off-by: Chojan Shang --- src/common/cache/src/cache.rs | 2 +- src/common/cache/src/cache/lru.rs | 37 ++++++++++++++----------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/common/cache/src/cache.rs b/src/common/cache/src/cache.rs index b727e93838030..7bb05c2af6854 100644 --- a/src/common/cache/src/cache.rs +++ b/src/common/cache/src/cache.rs @@ -23,7 +23,7 @@ use crate::Meter; /// A trait for a cache. pub trait Cache where - K: Eq + Hash, + K: Eq + Hash + Clone, S: BuildHasher, M: Meter, { diff --git a/src/common/cache/src/cache/lru.rs b/src/common/cache/src/cache/lru.rs index b8f6eb904557e..5a94c2076ed99 100644 --- a/src/common/cache/src/cache/lru.rs +++ b/src/common/cache/src/cache/lru.rs @@ -69,7 +69,7 @@ use crate::meter::count_meter::CountableMeter; /// An LRU cache. #[derive(Clone)] pub struct LruCache< - K: Eq + Hash, + K: Eq + Hash + Clone, V, S: BuildHasher = DefaultHashBuilder, M: CountableMeter = Count, @@ -81,7 +81,7 @@ pub struct LruCache< meter: M, } -impl LruCache { +impl LruCache { /// Creates an empty cache that can hold at most `capacity` items. /// /// # Examples @@ -101,7 +101,7 @@ impl LruCache { } } -impl> LruCache { +impl> LruCache { /// Creates an empty cache that can hold at most `capacity` as measured by `meter`. /// /// You can implement the [`Meter`][meter] trait to allow custom metrics. @@ -146,7 +146,7 @@ impl> LruCache LruCache { +impl LruCache { /// Creates an empty cache that can hold at most `capacity` items with the given hash builder. pub fn with_hasher(capacity: u64, hash_builder: S) -> LruCache { LruCache { @@ -159,13 +159,12 @@ impl LruCache { } } -impl> LruCache { +impl> LruCache { fn find_evict_candidate(&mut self) -> Option { - let iter = self.visited.iter_mut(); let mut p: Option = None; - for (key, value) in iter { + for (key, value) in self.visited.iter_mut() { if !(*value) && p.is_none() { - p = Some(unsafe { std::ptr::read(key) }) + p = Some(key.clone()) } if *value { *value = false; @@ -175,18 +174,17 @@ impl> LruCache Option { - let iter = self.visited.iter(); let mut p: Option = None; - for (key, value) in iter { + for (key, value) in self.visited.iter() { if !(*value) && p.is_none() { - p = Some(unsafe { std::ptr::read(key) }) + p = Some(key.clone()) } } p } } -impl> Cache +impl> Cache for LruCache { /// Creates an empty cache that can hold at most `capacity` as measured by `meter` with the @@ -339,8 +337,7 @@ impl> Cache .meter .sub(self.current_measure, self.meter.measure(&k, old)); } else { - let clone_k = unsafe { std::ptr::read(&k) }; - self.visited.replace(clone_k, false); + self.visited.replace(k.clone(), false); } let old_val = self.map.replace(k, v); while self.size() > self.capacity() { @@ -496,7 +493,7 @@ impl> Cache } } -impl> LruCache { +impl> LruCache { /// Returns an iterator over the cache's key-value pairs in least- to most-recently-used order. /// /// Accessing the cache through the iterator does _not_ affect the cache's LRU state. @@ -555,7 +552,7 @@ impl> LruCache> Extend<(K, V)> +impl> Extend<(K, V)> for LruCache { fn extend>(&mut self, iter: I) { @@ -565,7 +562,7 @@ impl> Extend<(K, V)> } } -impl> fmt::Debug +impl> fmt::Debug for LruCache { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -573,7 +570,7 @@ impl> IntoIterator +impl> IntoIterator for LruCache { type Item = (K, V); @@ -584,7 +581,7 @@ impl> IntoIterator } } -impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter> IntoIterator +impl<'a, K: Eq + Hash + Clone, V, S: BuildHasher, M: CountableMeter> IntoIterator for &'a LruCache { type Item = (&'a K, &'a V); @@ -594,7 +591,7 @@ impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter> IntoIterator } } -impl<'a, K: Eq + Hash, V, S: BuildHasher, M: CountableMeter> IntoIterator +impl<'a, K: Eq + Hash + Clone, V, S: BuildHasher, M: CountableMeter> IntoIterator for &'a mut LruCache { type Item = (&'a K, &'a mut V); From 69a907cd4901f1a07859694d27e94575e7c821ff Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Sun, 3 Dec 2023 11:52:32 +0800 Subject: [PATCH 4/7] chore: cargo fmt Signed-off-by: Chojan Shang --- src/common/cache/src/cache/lru.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/cache/src/cache/lru.rs b/src/common/cache/src/cache/lru.rs index 5a94c2076ed99..9443921ccc87c 100644 --- a/src/common/cache/src/cache/lru.rs +++ b/src/common/cache/src/cache/lru.rs @@ -552,7 +552,7 @@ impl> LruCache< } } -impl> Extend<(K, V)> +impl> Extend<(K, V)> for LruCache { fn extend>(&mut self, iter: I) { @@ -562,8 +562,8 @@ impl> Ex } } -impl> fmt::Debug - for LruCache +impl> + fmt::Debug for LruCache { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_map().entries(self.iter().rev()).finish() From 82d454f627b860f7604b0df519115e2da21e653b Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Mon, 11 Dec 2023 00:02:18 +0800 Subject: [PATCH 5/7] feat: better hits ratio Signed-off-by: Chojan Shang --- Cargo.lock | 1 + src/common/cache/Cargo.toml | 1 + src/common/cache/src/cache/lru.rs | 91 +++++++++++++------------- src/common/cache/tests/it/cache/lru.rs | 16 ++--- 4 files changed, 55 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index adfd3b9a1d917..c38c1a475b3f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1880,6 +1880,7 @@ dependencies = [ "hashbrown 0.14.0", "hashlink", "heapsize", + "indexmap 1.9.2", ] [[package]] diff --git a/src/common/cache/Cargo.toml b/src/common/cache/Cargo.toml index 5a426156f1458..51a73f282caa4 100644 --- a/src/common/cache/Cargo.toml +++ b/src/common/cache/Cargo.toml @@ -19,6 +19,7 @@ heapsize = ["heapsize_"] bytes = { workspace = true } hashbrown = "0.14" hashlink = "0.8" +indexmap = "1.9.2" [target.'cfg(not(target_os = "macos"))'.dependencies] heapsize_ = { package = "heapsize", version = "0.4.2", optional = true } diff --git a/src/common/cache/src/cache/lru.rs b/src/common/cache/src/cache/lru.rs index 9443921ccc87c..aaab0aa12a2d2 100644 --- a/src/common/cache/src/cache/lru.rs +++ b/src/common/cache/src/cache/lru.rs @@ -61,6 +61,7 @@ use std::hash::Hash; use hashbrown::hash_map::DefaultHashBuilder; use hashlink::linked_hash_map; use hashlink::LinkedHashMap; +use indexmap::IndexMap; use crate::cache::Cache; use crate::meter::count_meter::Count; @@ -75,7 +76,8 @@ pub struct LruCache< M: CountableMeter = Count, > { map: LinkedHashMap, - visited: LinkedHashMap, + visited: IndexMap, + hand: u64, current_measure: M::Measure, max_capacity: u64, meter: M, @@ -93,7 +95,8 @@ impl LruCache { pub fn new(capacity: u64) -> Self { LruCache { map: LinkedHashMap::new(), - visited: LinkedHashMap::new(), + visited: IndexMap::new(), + hand: 0, current_measure: (), max_capacity: capacity, meter: Count, @@ -138,7 +141,8 @@ impl> LruCache LruCache { LruCache { map: LinkedHashMap::new(), - visited: LinkedHashMap::new(), + visited: IndexMap::new(), + hand: 0, current_measure: Default::default(), max_capacity: capacity, meter, @@ -151,7 +155,8 @@ impl LruCache { pub fn with_hasher(capacity: u64, hash_builder: S) -> LruCache { LruCache { map: LinkedHashMap::with_hasher(hash_builder), - visited: LinkedHashMap::new(), + visited: IndexMap::new(), + hand: 0, current_measure: (), max_capacity: capacity, meter: Count, @@ -161,24 +166,39 @@ impl LruCache { impl> LruCache { fn find_evict_candidate(&mut self) -> Option { + let length = self.visited.len() as u64; let mut p: Option = None; - for (key, value) in self.visited.iter_mut() { - if !(*value) && p.is_none() { - p = Some(key.clone()) - } - if *value { - *value = false; + let mut count = self.hand; + if count > length - length / 5 { + count = 0 + } + let mut iter = self.visited.iter_mut().skip(count as usize); + for (key, value) in &mut iter { + if *value == false && p.is_none() { + p = Some(key.clone()); + break; } + count = count + 1; + *value = false; } + self.hand = count; p } fn peek_evict_candidate(&self) -> Option { + let length = self.visited.len() as u64; let mut p: Option = None; - for (key, value) in self.visited.iter() { - if !(*value) && p.is_none() { - p = Some(key.clone()) + let mut count = self.hand; + if count > length - length / 5 { + count = 0 + } + let iter = self.visited.iter().skip(count as usize); + for (key, value) in iter { + if *value == false && p.is_none() { + p = Some(key.clone()); + break; } + count = count + 1; } p } @@ -192,7 +212,8 @@ impl> Cache Self { LruCache { map: LinkedHashMap::with_hasher(hash_builder), - visited: LinkedHashMap::new(), + visited: IndexMap::new(), + hand: 0, current_measure: Default::default(), max_capacity: capacity, meter, @@ -225,19 +246,10 @@ impl> Cache, Q: Hash + Eq + ?Sized, { - match self.map.raw_entry_mut().from_key(k) { - linked_hash_map::RawEntryMut::Occupied(occupied) => { - match self.visited.raw_entry_mut().from_key(k) { - // Since the element has been accessed, we set a flag. - linked_hash_map::RawEntryMut::Occupied(mut occupied) => { - occupied.replace_value(true); - } - linked_hash_map::RawEntryMut::Vacant(_) => (), - } - Some(occupied.into_mut()) - } - linked_hash_map::RawEntryMut::Vacant(_) => None, + if let Some(v) = self.visited.get_mut(k) { + *v = true; } + self.map.get(k) } /// Returns a reference to the value corresponding to the key in the cache or `None` if it is @@ -283,7 +295,7 @@ impl> Cache> Cache Option { let new_size = self.meter.measure(&k, &v); self.current_measure = self.meter.add(self.current_measure, new_size); - if let Some(old) = self.map.get(&k) { - match self.visited.raw_entry_mut().from_key(&k) { - // Since the key has been accessed, we set a flag. - linked_hash_map::RawEntryMut::Occupied(mut occupied) => { - occupied.replace_value(true); - } - linked_hash_map::RawEntryMut::Vacant(_) => (), - } + match self.map.get(&k) { + Some(old) => { self.current_measure = self .meter - .sub(self.current_measure, self.meter.measure(&k, old)); - } else { - self.visited.replace(k.clone(), false); + .sub(self.current_measure, self.meter.measure(&k, old));}, + None => {self.visited.insert(k.clone(), false);} } let old_val = self.map.replace(k, v); while self.size() > self.capacity() { @@ -395,20 +400,14 @@ impl> Cache Option<(K, V)> { if let Some(old_key) = self.find_evict_candidate() { self.map.remove_entry(&old_key).map(|(k, v)| { - self.visited.remove_entry(&old_key); + self.visited.remove(&old_key); self.current_measure = self .meter .sub(self.current_measure, self.meter.measure(&k, &v)); (k, v) }) } else { - self.map.pop_front().map(|(k, v)| { - self.visited.pop_front(); - self.current_measure = self - .meter - .sub(self.current_measure, self.meter.measure(&k, &v)); - (k, v) - }) + None } } diff --git a/src/common/cache/tests/it/cache/lru.rs b/src/common/cache/tests/it/cache/lru.rs index 5baa5bf643091..55f548152cb68 100644 --- a/src/common/cache/tests/it/cache/lru.rs +++ b/src/common/cache/tests/it/cache/lru.rs @@ -95,7 +95,7 @@ fn test_debug() { cache.get(&3); assert_eq!(format!("{:?}", cache), "{6: 60, 3: 30, 2: 22}"); cache.set_capacity(2); - assert_eq!(format!("{:?}", cache), "{6: 60, 3: 30}"); + assert_eq!(format!("{:?}", cache), "{3: 30, 2: 22}"); } #[test] @@ -115,7 +115,7 @@ fn test_remove() { cache.put(8, 80); assert!(cache.get(&5).is_none()); assert_eq!(cache.get(&6), Some(&60)); - assert_eq!(cache.get(&7), Some(&70)); + assert_eq!(cache.get(&7), None); assert_eq!(cache.get(&8), Some(&80)); } @@ -139,24 +139,24 @@ fn test_iter() { cache.put(4, 40); cache.put(5, 50); assert_eq!(cache.iter().collect::>(), [ + (&2, &20), (&3, &30), - (&4, &40), (&5, &50) ]); assert_eq!(cache.iter_mut().collect::>(), [ + (&2, &mut 20), (&3, &mut 30), - (&4, &mut 40), (&5, &mut 50) ]); assert_eq!(cache.iter().rev().collect::>(), [ (&5, &50), - (&4, &40), - (&3, &30) + (&3, &30), + (&2, &20) ]); assert_eq!(cache.iter_mut().rev().collect::>(), [ (&5, &mut 50), - (&4, &mut 40), - (&3, &mut 30) + (&3, &mut 30), + (&2, &mut 20) ]); } From 5189766e2e4da10606a6ce299b2260574c02af4c Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Mon, 11 Dec 2023 00:29:09 +0800 Subject: [PATCH 6/7] fix: make fmt/clippy/test happy Signed-off-by: Chojan Shang --- src/common/cache/src/cache/lru.rs | 11 +++++++---- .../common/cache/tests/it/providers/disk_cache.rs | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/common/cache/src/cache/lru.rs b/src/common/cache/src/cache/lru.rs index aaab0aa12a2d2..9f04cbed939af 100644 --- a/src/common/cache/src/cache/lru.rs +++ b/src/common/cache/src/cache/lru.rs @@ -339,10 +339,13 @@ impl> Cache { - self.current_measure = self - .meter - .sub(self.current_measure, self.meter.measure(&k, old));}, - None => {self.visited.insert(k.clone(), false);} + self.current_measure = self + .meter + .sub(self.current_measure, self.meter.measure(&k, old)); + } + None => { + self.visited.insert(k.clone(), false); + } } let old_val = self.map.replace(k, v); while self.size() > self.capacity() { diff --git a/src/query/storages/common/cache/tests/it/providers/disk_cache.rs b/src/query/storages/common/cache/tests/it/providers/disk_cache.rs index e049725e723c1..69baf42410d46 100644 --- a/src/query/storages/common/cache/tests/it/providers/disk_cache.rs +++ b/src/query/storages/common/cache/tests/it/providers/disk_cache.rs @@ -145,10 +145,10 @@ fn test_evict_until_enough_space() { // insert a single slice which size bigger than file1 and less than file1 + file2 c.insert_single_slice("file4", &[3; 2]).unwrap(); - assert_eq!(c.size(), 3); + assert_eq!(c.size(), 4); // file1 and file2 MUST be evicted assert!(!c.contains_key("file1")); - assert!(!c.contains_key("file2")); + assert!(!c.contains_key("file3")); // file3 MUST be keeped - assert!(c.contains_key("file3")); + assert!(c.contains_key("file2")); } From ae1901f3a054ffd3a883f76998c495c339d60ced Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Mon, 11 Dec 2023 00:29:29 +0800 Subject: [PATCH 7/7] fix: make fmt/clippy/test happy Signed-off-by: Chojan Shang --- src/common/cache/src/cache/lru.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common/cache/src/cache/lru.rs b/src/common/cache/src/cache/lru.rs index 9f04cbed939af..7c6e146709d30 100644 --- a/src/common/cache/src/cache/lru.rs +++ b/src/common/cache/src/cache/lru.rs @@ -174,11 +174,11 @@ impl> LruCache< } let mut iter = self.visited.iter_mut().skip(count as usize); for (key, value) in &mut iter { - if *value == false && p.is_none() { + if !(*value) && p.is_none() { p = Some(key.clone()); break; } - count = count + 1; + count += 1; *value = false; } self.hand = count; @@ -194,11 +194,11 @@ impl> LruCache< } let iter = self.visited.iter().skip(count as usize); for (key, value) in iter { - if *value == false && p.is_none() { + if !(*value) && p.is_none() { p = Some(key.clone()); break; } - count = count + 1; + count += 1; } p }