Skip to content

Commit

Permalink
use Equivalent trait instead of Borrow
Browse files Browse the repository at this point in the history
  • Loading branch information
arendjr authored Jan 15, 2025
1 parent 8b9c514 commit 0abe12d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 40 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ keywords = ["concurrent", "hashmap", "atomic", "lock-free"]
exclude = ["assets/*"]

[dependencies]
equivalent = "1"
seize = "0.4"
serde = { version = "1", optional = true }

Expand All @@ -35,7 +36,10 @@ inherits = "release"
debug-assertions = true

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(papaya_stress)', 'cfg(papaya_asan)'] }
unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(papaya_stress)',
'cfg(papaya_asan)',
] }

[[bench]]
name = "single_thread"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ mod set;
#[cfg(feature = "serde")]
mod serde_impls;

pub use equivalent::Equivalent;
pub use map::{
Compute, HashMap, HashMapBuilder, HashMapRef, Iter, Keys, OccupiedError, Operation, ResizeMode,
Values,
Expand Down
28 changes: 12 additions & 16 deletions src/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::raw::{self, InsertResult};
use crate::Equivalent;
use seize::{Collector, Guard, LocalGuard, OwnedGuard};

use std::borrow::Borrow;
Expand Down Expand Up @@ -417,8 +418,7 @@ where
#[inline]
pub fn contains_key<Q>(&self, key: &Q, guard: &impl Guard) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
self.get(key, guard).is_some()
}
Expand All @@ -445,8 +445,8 @@ where
#[inline]
pub fn get<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<&'g V>
where
K: Borrow<Q> + 'g,
Q: Hash + Eq + ?Sized,
K: 'g,
Q: Equivalent<K> + Hash + ?Sized,
{
self.raw.check_guard(guard);

Expand Down Expand Up @@ -479,8 +479,7 @@ where
#[inline]
pub fn get_key_value<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<(&'g K, &'g V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
self.raw.check_guard(guard);

Expand Down Expand Up @@ -815,8 +814,8 @@ where
#[inline]
pub fn remove<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<&'g V>
where
K: Borrow<Q> + 'g,
Q: Hash + Eq + ?Sized,
K: 'g,
Q: Equivalent<K> + Hash + ?Sized,
{
self.raw.check_guard(guard);

Expand Down Expand Up @@ -848,8 +847,8 @@ where
#[inline]
pub fn remove_entry<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<(&'g K, &'g V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
K: 'g,
Q: Equivalent<K> + Hash + ?Sized,
{
self.raw.check_guard(guard);

Expand Down Expand Up @@ -1278,8 +1277,7 @@ where
#[inline]
pub fn contains_key<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
self.get(key).is_some()
}
Expand All @@ -1290,8 +1288,7 @@ where
#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
// Safety: `self.guard` was created from our map.
match unsafe { self.map.raw.get(key, &self.guard) } {
Expand All @@ -1306,8 +1303,7 @@ where
#[inline]
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
// Safety: `self.guard` was created from our map.
unsafe { self.map.raw.get(key, &self.guard) }
Expand Down
18 changes: 6 additions & 12 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod alloc;
mod probe;
mod utils;

use std::borrow::Borrow;
use std::hash::{BuildHasher, Hash};
use std::mem::MaybeUninit;
use std::sync::atomic::{fence, AtomicPtr, AtomicU8, AtomicUsize, Ordering};
Expand All @@ -13,6 +12,7 @@ use self::alloc::{RawTable, Table};
use self::probe::Probe;
use self::utils::{untagged, AtomicPtrFetchOps, Counter, Parker, Shared, StrictProvenance, Tagged};
use crate::map::{Compute, Operation, ResizeMode};
use crate::Equivalent;

use seize::{AsLink, Collector, Guard, Link};

Expand Down Expand Up @@ -286,8 +286,7 @@ where
#[inline]
pub unsafe fn get<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<(&'g K, &'g V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
let mut table = self.root(guard);

Expand Down Expand Up @@ -319,7 +318,7 @@ where
}

// Check for a full match.
if unsafe { (*entry.ptr).key.borrow() } == key {
if key.equivalent(unsafe { &(*entry.ptr).key }) {
// The entry was copied to the new table.
//
// In blocking resize mode we do not need to perform self check as all writes block
Expand Down Expand Up @@ -607,14 +606,9 @@ where
//
// The guard must be valid to use with this map.
#[inline]
pub unsafe fn remove<'g, Q: ?Sized>(
&self,
key: &Q,
guard: &'g impl Guard,
) -> Option<(&'g K, &'g V)>
pub unsafe fn remove<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<(&'g K, &'g V)>
where
K: Borrow<Q>,
Q: Hash + Eq,
Q: Equivalent<K> + Hash + ?Sized,
{
let mut table = self.root(guard);

Expand Down Expand Up @@ -661,7 +655,7 @@ where
}

// Check for a full match.
if unsafe { (*entry.ptr).key.borrow() != key } {
if !key.equivalent(unsafe { &(*entry.ptr).key }) {
probe.next(table.mask);
continue 'probe;
}
Expand Down
18 changes: 7 additions & 11 deletions src/set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::raw::{self, InsertResult};
use crate::Equivalent;
use seize::{Collector, Guard, LocalGuard, OwnedGuard};

use crate::map::ResizeMode;
Expand Down Expand Up @@ -366,8 +367,7 @@ where
#[inline]
pub fn contains<Q>(&self, key: &Q, guard: &impl Guard) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
self.get(key, guard).is_some()
}
Expand All @@ -394,8 +394,7 @@ where
#[inline]
pub fn get<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<&'g K>
where
K: Borrow<Q> + 'g,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
self.raw.check_guard(guard);

Expand Down Expand Up @@ -459,10 +458,9 @@ where
/// assert_eq!(set.pin().remove(&1), false);
/// ```
#[inline]
pub fn remove<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> bool
pub fn remove<Q>(&self, key: &Q, guard: &impl Guard) -> bool
where
K: Borrow<Q> + 'g,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
self.raw.check_guard(guard);

Expand Down Expand Up @@ -772,8 +770,7 @@ where
#[inline]
pub fn contains<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
self.get(key).is_some()
}
Expand All @@ -784,8 +781,7 @@ where
#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<&K>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Equivalent<K> + Hash + ?Sized,
{
// Safety: `self.guard` was created from our map.
match unsafe { self.set.raw.get(key, &self.guard) } {
Expand Down

0 comments on commit 0abe12d

Please sign in to comment.