From 97822c9e20dcb201bec635120d646ad023d66fdf Mon Sep 17 00:00:00 2001 From: Qin-shihuang <0.0@owo.li> Date: Fri, 13 Dec 2024 10:04:26 +0800 Subject: [PATCH] refactor: use SHA1 as the key in LRU cache --- mercury/src/internal/pack/cache.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/mercury/src/internal/pack/cache.rs b/mercury/src/internal/pack/cache.rs index b53873d35..64c4cd409 100644 --- a/mercury/src/internal/pack/cache.rs +++ b/mercury/src/internal/pack/cache.rs @@ -26,6 +26,12 @@ pub trait _Cache { fn clear(&self); } +impl lru_mem::HeapSize for SHA1 { + fn heap_size(&self) -> usize { + 0 + } +} + pub struct Caches { map_offset: DashMap, // offset to hash hash_set: DashSet, // item in the cache @@ -33,7 +39,7 @@ pub struct Caches { // because "multi-thread IO" clone Arc, so it won't be dropped in the main thread, // and `CacheObjects` will be killed by OS after Process ends abnormally // Solution: use `mimalloc` - lru_cache: Mutex>>, // *lru_cache require the key to implement lru::MemSize trait, so didn't use SHA1 as the key* + lru_cache: Mutex>>, mem_size: Option, tmp_path: PathBuf, pool: Arc, @@ -44,7 +50,7 @@ impl Caches { /// only get object from memory, not from tmp file fn try_get(&self, hash: SHA1) -> Option> { let mut map = self.lru_cache.lock().unwrap(); - map.get(&hash.to_string()).map(|x| x.data.clone()) + map.get(&hash).map(|x| x.data.clone()) } /// !IMPORTANT: because of the process of pack, the file must be written / be writing before, so it won't be dead lock @@ -72,7 +78,7 @@ impl Caches { Some(self.pool.clone()), ); x.set_store_path(Caches::generate_temp_path(&self.tmp_path, hash)); - let _ = map.insert(hash.to_string(), x); // handle the error + let _ = map.insert(hash, x); // handle the error Ok(obj) } @@ -154,7 +160,7 @@ impl _Cache for Caches { Some(self.pool.clone()), ); a_obj.set_store_path(Caches::generate_temp_path(&self.tmp_path, hash)); - let _ = map.insert(hash.to_string(), a_obj); + let _ = map.insert(hash, a_obj); } //order maters as for reading in 'get_by_offset()' self.hash_set.insert(hash);