Skip to content

Commit 8012e4a

Browse files
authored
Merge pull request #743 from el-ev/sha1_as_key
refactor(mercury): use SHA1 as key in LRU cache
2 parents 7160251 + 0eb009f commit 8012e4a

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

mercury/src/internal/pack/cache.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ pub trait _Cache {
2626
fn clear(&self);
2727
}
2828

29+
impl lru_mem::HeapSize for SHA1 {
30+
fn heap_size(&self) -> usize {
31+
0
32+
}
33+
}
34+
2935
pub struct Caches {
3036
map_offset: DashMap<usize, SHA1>, // offset to hash
3137
hash_set: DashSet<SHA1>, // item in the cache
3238
// dropping large lru cache will take a long time on Windows without multi-thread IO
3339
// because "multi-thread IO" clone Arc<CacheObject>, so it won't be dropped in the main thread,
3440
// and `CacheObjects` will be killed by OS after Process ends abnormally
3541
// Solution: use `mimalloc`
36-
lru_cache: Mutex<LruCache<String, ArcWrapper<CacheObject>>>, // *lru_cache require the key to implement lru::MemSize trait, so didn't use SHA1 as the key*
42+
lru_cache: Mutex<LruCache<SHA1, ArcWrapper<CacheObject>>>,
3743
mem_size: Option<usize>,
3844
tmp_path: PathBuf,
3945
pool: Arc<ThreadPool>,
@@ -44,7 +50,7 @@ impl Caches {
4450
/// only get object from memory, not from tmp file
4551
fn try_get(&self, hash: SHA1) -> Option<Arc<CacheObject>> {
4652
let mut map = self.lru_cache.lock().unwrap();
47-
map.get(&hash.to_string()).map(|x| x.data.clone())
53+
map.get(&hash).map(|x| x.data.clone())
4854
}
4955

5056
/// !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 {
7278
Some(self.pool.clone()),
7379
);
7480
x.set_store_path(Caches::generate_temp_path(&self.tmp_path, hash));
75-
let _ = map.insert(hash.to_string(), x); // handle the error
81+
let _ = map.insert(hash, x); // handle the error
7682
Ok(obj)
7783
}
7884

@@ -154,7 +160,7 @@ impl _Cache for Caches {
154160
Some(self.pool.clone()),
155161
);
156162
a_obj.set_store_path(Caches::generate_temp_path(&self.tmp_path, hash));
157-
let _ = map.insert(hash.to_string(), a_obj);
163+
let _ = map.insert(hash, a_obj);
158164
}
159165
//order maters as for reading in 'get_by_offset()'
160166
self.hash_set.insert(hash);

0 commit comments

Comments
 (0)