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) }; diff --git a/src/shard.rs b/src/shard.rs index 50f434f..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 { @@ -950,17 +967,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; }