Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions src/linked_slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,13 @@ impl<T> LinkedSlab<T> {
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)
};
Expand Down
58 changes: 37 additions & 21 deletions src/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,28 +581,35 @@ impl<
}

#[inline]
fn search<Q>(&self, hash: u64, k: &Q) -> Option<Token>
fn search<Q>(&self, hash: u64, key: &Q) -> Option<Token>
where
Q: Hash + Equivalent<Key> + ?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<Key> + ?Sized>(
entries: &'a LinkedSlab<Entry<Key, Val, Id, Id::Shared>>,
hash: u64,
key: &'a Q,
ghost: &'a mut Option<Token>,
) -> 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<Q>(&self, hash: u64, k: &Q) -> Option<(Token, &Resident<Key, Val>)>
where
Expand Down Expand Up @@ -754,15 +761,21 @@ impl<
where
Q: Hash + Equivalent<Key> + ?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<Q, F>(&mut self, hash: u64, key: &Q, f: F) -> Option<(Key, Val)>
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
Loading