Skip to content

Commit

Permalink
Update to DashMap 6 (#2961)
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Sep 9, 2024
1 parent ce05277 commit fa7de5d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,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.1.0", features = ["serde"], optional = true }
parking_lot = { version = "0.12.1"}
ed25519-dalek = { version = "2.0.0", optional = true }
typesize = { version = "0.1.6", optional = true, features = ["url", "time", "serde_json", "secrecy", "dashmap", "parking_lot", "nonmax", "extract_map_01", "details"] }
typesize = { version = "0.1.6", optional = true, features = ["url", "time", "serde_json", "secrecy", "parking_lot", "nonmax", "extract_map_01"] }
# serde feature only allows for serialisation,
# Serenity workspace crates
serenity-voice-model = { version = "0.2.0", path = "./voice-model", optional = true }
Expand Down Expand Up @@ -115,7 +115,7 @@ full = ["default", "collector", "voice", "voice_model", "interactions_endpoint"]
# Enables temporary caching in functions that retrieve data via the HTTP API.
temp_cache = ["cache", "mini-moka", "typesize?/mini_moka"]

typesize = ["dep:typesize", "small-fixed-array/typesize", "bool_to_bitflags/typesize"]
typesize = ["dep:typesize", "dashmap/typesize", "small-fixed-array/typesize", "bool_to_bitflags/typesize"]

# Enables compile-time heavy instrument macros from tracing
tracing_instrument = ["tracing/attributes"]
Expand Down
8 changes: 4 additions & 4 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,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 @@ -79,11 +79,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
23 changes: 16 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,11 +71,11 @@ 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)
}

Expand All @@ -91,6 +93,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 +107,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 fa7de5d

Please sign in to comment.