From cebd4ad4806dd2f32d9c377fdb399390bf3df95b Mon Sep 17 00:00:00 2001 From: Arthur Silva Date: Sat, 21 Nov 2026 02:24:57 +0100 Subject: [PATCH 1/3] Inline single ghost eviction --- src/shard.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/shard.rs b/src/shard.rs index 50f434f..f50400f 100644 --- a/src/shard.rs +++ b/src/shard.rs @@ -950,17 +950,16 @@ impl< } } - #[inline] + #[inline(always)] fn advance_ghost(&mut self) { debug_assert_ne!(self.num_non_resident, 0); let idx = self.ghost_head.unwrap(); - let (entry, _) = self.entries.get_mut(idx).unwrap(); - let Entry::Ghost(hash) = *entry else { + let (entry, next) = self.entries.remove(idx).unwrap(); + let Entry::Ghost(hash) = entry else { unsafe { unreachable_unchecked() }; }; self.num_non_resident -= 1; self.map_remove(hash, idx); - let (_, next) = self.entries.remove(idx).unwrap(); self.ghost_head = next; } From a796204a5a67a172fea7db53575cdba990287b97 Mon Sep 17 00:00:00 2001 From: Arthur Silva Date: Sat, 21 Nov 2026 10:50:11 +0100 Subject: [PATCH 2/3] Use one link path for slab rings A one-entry ring has its head as its own predecessor. The same updates link the new entry in rings of any length. --- src/linked_slab.rs | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/linked_slab.rs b/src/linked_slab.rs index 728a25d..781033d 100644 --- a/src/linked_slab.rs +++ b/src/linked_slab.rs @@ -149,20 +149,13 @@ impl LinkedSlab { let (prev, next) = if let Some(target_head) = target_head { let head = &mut self.entries[(target_head.get() - 1) as usize]; debug_assert!(head.item.is_some()); - if head.prev == target_head { - // existing list has a single item linking to itself - head.prev = idx; - head.next = idx; - (target_head, target_head) - } else { - let before_head_idx = head.prev; - head.prev = idx; - let before_head = &mut self.entries[(before_head_idx.get() - 1) as usize]; - debug_assert!(before_head.item.is_some()); - debug_assert_eq!(before_head.next, target_head); - before_head.next = idx; - (before_head_idx, target_head) - } + let before_head_idx = head.prev; + head.prev = idx; + let before_head = &mut self.entries[(before_head_idx.get() - 1) as usize]; + debug_assert!(before_head.item.is_some()); + debug_assert_eq!(before_head.next, target_head); + before_head.next = idx; + (before_head_idx, target_head) } else { (idx, idx) }; From 830864ba4408879b24c2c1ec32bd3c7933611716 Mon Sep 17 00:00:00 2001 From: Arthur Silva Date: Sat, 21 Nov 2026 11:01:30 +0100 Subject: [PATCH 3/3] Remove exact cache keys with one hash-table probe Keep the occupied bucket for an exact hit and invalidate its placeholder before removing it. Use the ghost token when the exact-key probe misses. --- src/shard.rs | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/shard.rs b/src/shard.rs index f50400f..8bdeb1b 100644 --- a/src/shard.rs +++ b/src/shard.rs @@ -581,28 +581,35 @@ impl< } #[inline] - fn search(&self, hash: u64, k: &Q) -> Option + fn search(&self, hash: u64, key: &Q) -> Option where Q: Hash + Equivalent + ?Sized, { let mut ghost = None; self.map - .find(hash, |&idx| { - let (entry, _) = self.entries.get(idx).unwrap(); - match entry { - Entry::Resident(Resident { key, .. }) - | Entry::Placeholder(Placeholder { key, .. }) => k.equivalent(key), - Entry::Ghost(ghost_hash) if *ghost_hash == hash => { - ghost = Some(idx); - false - } - _ => false, - } - }) + .find(hash, Self::match_key(&self.entries, hash, key, &mut ghost)) .copied() .or(ghost) } + #[inline] + fn match_key<'a, Q: Equivalent + ?Sized>( + entries: &'a LinkedSlab>, + hash: u64, + key: &'a Q, + ghost: &'a mut Option, + ) -> impl FnMut(&Token) -> bool + 'a { + move |&idx| match entries.get(idx).unwrap().0 { + Entry::Resident(Resident { key: stored, .. }) + | Entry::Placeholder(Placeholder { key: stored, .. }) => key.equivalent(stored), + Entry::Ghost(ghost_hash) if *ghost_hash == hash => { + *ghost = Some(idx); + false + } + _ => false, + } + } + #[inline] fn search_resident(&self, hash: u64, k: &Q) -> Option<(Token, &Resident)> where @@ -754,15 +761,21 @@ impl< where Q: Hash + Equivalent + ?Sized, { - // This could be search_resident, but calling `remove` is likely a - // strong indication that this input isn't important. - let idx = self.search(hash, key)?; + let mut ghost = None; + let Ok(entry) = self + .map + .find_entry(hash, Self::match_key(&self.entries, hash, key, &mut ghost)) + else { + return self.remove_internal(hash, ghost?); + }; + let idx = *entry.get(); if let Some((Entry::Placeholder(p), _)) = self.entries.get(idx) { if let Some(shared) = p.shared.as_ref() { Id::invalidate(shared, wakes); } } - self.remove_internal(hash, idx) + entry.remove(); + self.remove_slot(idx) } pub fn remove_if(&mut self, hash: u64, key: &Q, f: F) -> Option<(Key, Val)> @@ -802,6 +815,10 @@ impl< fn remove_internal(&mut self, hash: u64, idx: Token) -> Option<(Key, Val)> { self.map_remove(hash, idx); + self.remove_slot(idx) + } + + fn remove_slot(&mut self, idx: Token) -> Option<(Key, Val)> { let mut result = None; let (entry, next) = self.entries.remove(idx).unwrap(); let list_head = match entry {