From 523b6a07dabc45205f1d0913448a6e8cf6c77774 Mon Sep 17 00:00:00 2001 From: Christopher Berner Date: Sat, 26 Oct 2024 07:38:55 -0700 Subject: [PATCH] Optimize page cache eviction This speeds up some workloads like ord indexing by ~20% --- src/tree_store/page_store/lru_cache.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/tree_store/page_store/lru_cache.rs b/src/tree_store/page_store/lru_cache.rs index 9e33ea0a..d981fdd0 100644 --- a/src/tree_store/page_store/lru_cache.rs +++ b/src/tree_store/page_store/lru_cache.rs @@ -74,11 +74,14 @@ impl LRUCache { pub(crate) fn pop_lowest_priority(&mut self) -> Option<(u64, T)> { while let Some(key) = self.lru_queue.pop_front() { - if let Some((value, second_chance)) = self.cache.remove(&key) { - if second_chance.load(Ordering::Acquire) { - self.cache.insert(key, (value, AtomicBool::new(false))); + if let Some((_, second_chance)) = self.cache.get(&key) { + if second_chance + .compare_exchange(true, false, Ordering::AcqRel, Ordering::Acquire) + .is_ok() + { self.lru_queue.push_back(key); } else { + let (value, _) = self.cache.remove(&key).unwrap(); return Some((key, value)); } }