Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Equivalence trait #45

Merged
merged 5 commits into from
Jan 15, 2025
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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ keywords = ["concurrent", "hashmap", "atomic", "lock-free"]
exclude = ["assets/*"]

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

Expand All @@ -34,7 +35,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
Loading