Skip to content

Commit

Permalink
refactor: simplify clear
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx committed Apr 2, 2024
1 parent 6483b58 commit 7a9a7c8
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/common/src/lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,20 +296,15 @@ where

pub fn clear(&mut self) {
unsafe {
while !self.is_empty() {
let ptr = self.dummy.next.unwrap_unchecked();
let mut map = HashTable::new_in(self.alloc.clone());
std::mem::swap(&mut map, &mut self.map);

for ptr in map.drain() {
self.detach(ptr);
let entry = Box::from_raw_in(ptr.as_ptr(), self.alloc.clone());
let key = entry.key.assume_init();
let hash = self.hash_builder.hash_one(&key);
match self
.map
.entry(hash, |p| p.as_ref().key() == &key, |p| p.as_ref().hash)
{
Entry::Occupied(o) => o.remove(),
Entry::Vacant(_) => unreachable!(),
};
let _ = Box::from_raw_in(ptr.as_ptr(), self.alloc.clone());
}

debug_assert!(self.is_empty());
}
}

Expand Down

0 comments on commit 7a9a7c8

Please sign in to comment.