Skip to content

Commit

Permalink
Update to DashMap 6
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Aug 27, 2024
1 parent ba73120 commit 7c18921
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ bytes = { version = "1.5.0", optional = true }
percent-encoding = { version = "2.3.0", optional = true }
mini-moka = { version = "0.10.2", optional = true }
mime_guess = { version = "2.0.4", optional = true }
dashmap = { version = "5.5.3", features = ["serde"], optional = true }
dashmap = { version = "6.0.1", features = ["serde"], optional = true }
parking_lot = { version = "0.12.1", optional = true }
ed25519-dalek = { version = "2.0.0", optional = true }
typesize = { version = "0.1.2", optional = true, features = ["url", "time", "serde_json", "secrecy", "dashmap", "parking_lot", "details"] }
typesize = { version = "0.1.8", optional = true, features = ["url", "time", "serde_json", "secrecy", "parking_lot", "details"] }
# serde feature only allows for serialisation,
# Serenity workspace crates
command_attr = { version = "0.5.2", path = "./command_attr", optional = true }
Expand Down Expand Up @@ -128,6 +128,8 @@ temp_cache = ["cache", "mini-moka", "typesize?/mini_moka"]
# Removed feature (https://github.com/serenity-rs/serenity/pull/2246)
absolute_ratelimits = []

typesize = ["dep:typesize", "dashmap?/typesize"]

# Backends to pick from:
# - Rustls Backends
rustls_backend = [
Expand All @@ -147,3 +149,7 @@ native_tls_backend = [
[package.metadata.docs.rs]
features = ["full"]
rustdoc-args = ["--cfg", "docsrs"]

[patch.crates-io.dashmap]
git = "https://github.com/GnomedDev/dashmap"
branch = "typesize"
8 changes: 4 additions & 4 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ struct NotSend;
enum CacheRefInner<'a, K, V, T> {
#[cfg(feature = "temp_cache")]
Arc(Arc<V>),
DashRef(Ref<'a, K, V, BuildHasher>),
DashMappedRef(MappedRef<'a, K, T, V, BuildHasher>),
DashRef(Ref<'a, K, V>),
DashMappedRef(MappedRef<'a, K, T, V>),
ReadGuard(parking_lot::RwLockReadGuard<'a, V>),
}

Expand All @@ -82,11 +82,11 @@ impl<'a, K, V, T> CacheRef<'a, K, V, T> {
Self::new(CacheRefInner::Arc(inner.get_inner()))
}

fn from_ref(inner: Ref<'a, K, V, BuildHasher>) -> Self {
fn from_ref(inner: Ref<'a, K, V>) -> Self {
Self::new(CacheRefInner::DashRef(inner))
}

fn from_mapped_ref(inner: MappedRef<'a, K, T, V, BuildHasher>) -> Self {
fn from_mapped_ref(inner: MappedRef<'a, K, T, V>) -> Self {
Self::new(CacheRefInner::DashMappedRef(inner))
}

Expand Down
24 changes: 17 additions & 7 deletions src/cache/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use typesize::TypeSize;
/// A wrapper around Option<DashMap<K, V>> to ease disabling specific cache fields.
pub(crate) struct MaybeMap<K: Eq + Hash, V>(pub(super) Option<DashMap<K, V, BuildHasher>>);
impl<K: Eq + Hash, V> MaybeMap<K, V> {
pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, K, V, BuildHasher>> {
pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, K, V>> {
Option::iter(&self.0).flat_map(DashMap::iter)
}

pub fn get(&self, k: &K) -> Option<Ref<'_, K, V, BuildHasher>> {
pub fn get(&self, k: &K) -> Option<Ref<'_, K, V>> {
self.0.as_ref()?.get(k)
}

pub fn get_mut(&self, k: &K) -> Option<RefMut<'_, K, V, BuildHasher>> {
pub fn get_mut(&self, k: &K) -> Option<RefMut<'_, K, V>> {
self.0.as_ref()?.get_mut(k)
}

Expand Down Expand Up @@ -59,8 +59,10 @@ impl<K: Eq + Hash + TypeSize, V: TypeSize> TypeSize for MaybeMap<K, V> {
self.0.as_ref().map(DashMap::extra_size).unwrap_or_default()
}

fn get_collection_item_count(&self) -> Option<usize> {
self.0.as_ref().and_then(DashMap::get_collection_item_count)
typesize::if_typesize_details! {
fn get_collection_item_count(&self) -> Option<usize> {
self.0.as_ref().and_then(DashMap::get_collection_item_count)
}
}
}

Expand All @@ -69,18 +71,19 @@ impl<K: Eq + Hash + TypeSize, V: TypeSize> TypeSize for MaybeMap<K, V> {
/// map without allowing mutation of internal cache fields, which could cause issues.
pub struct ReadOnlyMapRef<'a, K: Eq + Hash, V>(Option<&'a DashMap<K, V, BuildHasher>>);
impl<'a, K: Eq + Hash, V> ReadOnlyMapRef<'a, K, V> {
pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, K, V, BuildHasher>> {
pub fn iter(&self) -> impl Iterator<Item = RefMulti<'_, K, V>> {
self.0.into_iter().flat_map(DashMap::iter)
}

pub fn get(&self, k: &K) -> Option<Ref<'_, K, V, BuildHasher>> {
pub fn get(&self, k: &K) -> Option<Ref<'_, K, V>> {
self.0?.get(k)
}

pub fn len(&self) -> usize {
self.0.map_or(0, DashMap::len)
}
}

pub struct Hasher(fxhash::FxHasher);
impl std::hash::Hasher for Hasher {
fn finish(&self) -> u64 {
Expand All @@ -91,6 +94,10 @@ impl std::hash::Hasher for Hasher {
self.0.write(bytes);
}
}

#[cfg(feature = "typesize")]
impl typesize::TypeSize for Hasher {}

#[derive(Clone, Default)]
pub struct BuildHasher(fxhash::FxBuildHasher);
impl std::hash::BuildHasher for BuildHasher {
Expand All @@ -101,6 +108,9 @@ impl std::hash::BuildHasher for BuildHasher {
}
}

#[cfg(feature = "typesize")]
impl typesize::TypeSize for BuildHasher {}

/// Wrapper around `SizableArc<T, Owned>`` with support for disabling typesize.
///
/// This denotes an Arc where T's size should be considered when calling `TypeSize::get_size`
Expand Down

0 comments on commit 7c18921

Please sign in to comment.