From d1c36fb46f7d778ae30bb13e6024db3a451d9549 Mon Sep 17 00:00:00 2001 From: Croxx Date: Fri, 31 May 2024 16:15:20 +0800 Subject: [PATCH] fix: check if entry is in indexer first before remove for deposit (#547) * fix: check if entry is in indexer first before remove for deposit Signed-off-by: MrCroxx * chore: add comments Signed-off-by: MrCroxx * chore: fix typo Signed-off-by: MrCroxx --------- Signed-off-by: MrCroxx --- foyer-memory/src/generic.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/foyer-memory/src/generic.rs b/foyer-memory/src/generic.rs index e5f1eec4..3018014d 100644 --- a/foyer-memory/src/generic.rs +++ b/foyer-memory/src/generic.rs @@ -319,7 +319,8 @@ where strict_assert!(handle.base().is_inited()); strict_assert!(!handle.base().has_refs()); - if handle.base().is_deposit() { + // If the entry is deposit (emplace by deposit & never read), remove it from indexer to skip reinsertion. + if handle.base().is_in_indexer() && handle.base().is_deposit() { strict_assert!(!handle.base().is_in_eviction()); self.indexer.remove(handle.base().hash(), handle.key()); strict_assert!(!handle.base().is_in_indexer()); @@ -335,6 +336,7 @@ where let was_in_eviction = handle.base().is_in_eviction(); self.eviction.release(ptr); if ptr.as_ref().base().is_in_eviction() { + // The entry is not yep evicted, do NOT release it. if !was_in_eviction { self.state.metrics.memory_reinsert.increment(1); } @@ -1076,6 +1078,17 @@ mod tests { assert_eq!(cache.get(&42).unwrap().value(), "answer"); } + #[test] + fn test_deposit_replace() { + let cache = lru(100); + let e1 = cache.deposit(42, "wrong answer".to_string()); + let e2 = cache.insert(42, "answer".to_string()); + drop(e1); + drop(e2); + assert_eq!(cache.get(&42).unwrap().value(), "answer"); + assert_eq!(cache.usage(), 6); + } + #[test] fn test_replace() { let cache = fifo(10);