diff --git a/Cargo.toml b/Cargo.toml index 0fb928b5..5a731a1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,17 +44,13 @@ unstable-debug-counters = ["future", "once_cell"] crossbeam-channel = "0.5.5" crossbeam-epoch = "0.9.9" crossbeam-utils = "0.8" +moka-arc = { git = "https://gitlab.com/moka-labs/moka-gh440-remove-triomphe/moka-arc.git" } parking_lot = "0.12" smallvec = "1.8" tagptr = "0.2" thiserror = "1.0" uuid = { version = "1.1", features = ["v4"] } -# Opt-out serde and stable_deref_trait features -# https://github.com/Manishearth/triomphe/pull/5 -# 0.1.12 requires Rust 1.76 -triomphe = { version = ">=0.1.3, <0.1.12", default-features = false } - # Optional dependencies (enabled by default) quanta = { version = "0.12.2", optional = true } diff --git a/src/common/concurrent.rs b/src/common/concurrent.rs index 2f7e6d8e..95686327 100644 --- a/src/common/concurrent.rs +++ b/src/common/concurrent.rs @@ -3,7 +3,7 @@ use crate::common::{deque::DeqNode, time::Instant}; use parking_lot::Mutex; use std::{fmt, ptr::NonNull, sync::Arc}; use tagptr::TagNonNull; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; pub(crate) mod constants; pub(crate) mod deques; @@ -64,13 +64,13 @@ impl Clone for KeyHash { } pub(crate) struct KeyHashDate { - entry_info: TrioArc>, + entry_info: MiniArc>, } impl KeyHashDate { - pub(crate) fn new(entry_info: &TrioArc>) -> Self { + pub(crate) fn new(entry_info: &MiniArc>) -> Self { Self { - entry_info: TrioArc::clone(entry_info), + entry_info: MiniArc::clone(entry_info), } } @@ -101,11 +101,11 @@ impl KeyHashDate { pub(crate) struct KvEntry { pub(crate) key: Arc, - pub(crate) entry: TrioArc>, + pub(crate) entry: MiniArc>, } impl KvEntry { - pub(crate) fn new(key: Arc, entry: TrioArc>) -> Self { + pub(crate) fn new(key: Arc, entry: MiniArc>) -> Self { Self { key, entry } } } @@ -114,7 +114,7 @@ impl Clone for KvEntry { fn clone(&self) -> Self { Self { key: Arc::clone(&self.key), - entry: TrioArc::clone(&self.entry), + entry: MiniArc::clone(&self.entry), } } } @@ -177,33 +177,33 @@ impl DeqNodes { pub(crate) struct ValueEntry { pub(crate) value: V, - info: TrioArc>, - nodes: TrioArc>>, + info: MiniArc>, + nodes: MiniArc>>, } impl ValueEntry { - pub(crate) fn new(value: V, entry_info: TrioArc>) -> Self { + pub(crate) fn new(value: V, entry_info: MiniArc>) -> Self { #[cfg(feature = "unstable-debug-counters")] self::debug_counters::InternalGlobalDebugCounters::value_entry_created(); Self { value, info: entry_info, - nodes: TrioArc::new(Mutex::new(DeqNodes::default())), + nodes: MiniArc::new(Mutex::new(DeqNodes::default())), } } - pub(crate) fn new_from(value: V, entry_info: TrioArc>, other: &Self) -> Self { + pub(crate) fn new_from(value: V, entry_info: MiniArc>, other: &Self) -> Self { #[cfg(feature = "unstable-debug-counters")] self::debug_counters::InternalGlobalDebugCounters::value_entry_created(); Self { value, info: entry_info, - nodes: TrioArc::clone(&other.nodes), + nodes: MiniArc::clone(&other.nodes), } } - pub(crate) fn entry_info(&self) -> &TrioArc> { + pub(crate) fn entry_info(&self) -> &MiniArc> { &self.info } @@ -224,7 +224,7 @@ impl ValueEntry { self.info.policy_weight() } - pub(crate) fn deq_nodes(&self) -> &TrioArc>> { + pub(crate) fn deq_nodes(&self) -> &MiniArc>> { &self.nodes } @@ -278,7 +278,7 @@ impl Drop for ValueEntry { } } -impl AccessTime for TrioArc> { +impl AccessTime for MiniArc> { #[inline] fn last_accessed(&self) -> Option { self.info.last_accessed() @@ -302,7 +302,7 @@ impl AccessTime for TrioArc> { pub(crate) enum ReadOp { Hit { - value_entry: TrioArc>, + value_entry: MiniArc>, is_expiry_modified: bool, }, // u64 is the hash of the key. @@ -312,7 +312,7 @@ pub(crate) enum ReadOp { pub(crate) enum WriteOp { Upsert { key_hash: KeyHash, - value_entry: TrioArc>, + value_entry: MiniArc>, /// Entry generation after the operation. entry_gen: u16, old_weight: u32, @@ -324,7 +324,7 @@ pub(crate) enum WriteOp { }, } -/// Cloning a `WriteOp` is safe and cheap because it uses `Arc` and `TrioArc` pointers to +/// Cloning a `WriteOp` is safe and cheap because it uses `Arc` and `MiniArc` pointers to /// the actual data. impl Clone for WriteOp { fn clone(&self) -> Self { @@ -337,7 +337,7 @@ impl Clone for WriteOp { new_weight, } => Self::Upsert { key_hash: key_hash.clone(), - value_entry: TrioArc::clone(value_entry), + value_entry: MiniArc::clone(value_entry), entry_gen: *entry_gen, old_weight: *old_weight, new_weight: *new_weight, @@ -366,13 +366,13 @@ impl WriteOp { pub(crate) fn new_upsert( key: &Arc, hash: u64, - value_entry: &TrioArc>, + value_entry: &MiniArc>, entry_generation: u16, old_weight: u32, new_weight: u32, ) -> Self { let key_hash = KeyHash::new(Arc::clone(key), hash); - let value_entry = TrioArc::clone(value_entry); + let value_entry = MiniArc::clone(value_entry); Self::Upsert { key_hash, value_entry, @@ -384,15 +384,15 @@ impl WriteOp { } pub(crate) struct OldEntryInfo { - pub(crate) entry: TrioArc>, + pub(crate) entry: MiniArc>, pub(crate) last_accessed: Option, pub(crate) last_modified: Option, } impl OldEntryInfo { - pub(crate) fn new(entry: &TrioArc>) -> Self { + pub(crate) fn new(entry: &MiniArc>) -> Self { Self { - entry: TrioArc::clone(entry), + entry: MiniArc::clone(entry), last_accessed: entry.last_accessed(), last_modified: entry.last_modified(), } diff --git a/src/common/concurrent/deques.rs b/src/common/concurrent/deques.rs index 424fc104..37245b1e 100644 --- a/src/common/concurrent/deques.rs +++ b/src/common/concurrent/deques.rs @@ -6,7 +6,8 @@ use crate::common::{ use std::ptr::NonNull; use tagptr::TagNonNull; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; + pub(crate) struct Deques { pub(crate) window: Deque>, // Not used yet. pub(crate) probation: Deque>, @@ -50,7 +51,7 @@ impl Deques { &mut self, region: CacheRegion, khd: KeyHashDate, - entry: &TrioArc>, + entry: &MiniArc>, ) { let node = Box::new(DeqNode::new(khd)); let node = match region { @@ -66,14 +67,14 @@ impl Deques { pub(crate) fn push_back_wo( &mut self, kd: KeyHashDate, - entry: &TrioArc>, + entry: &MiniArc>, ) { let node = Box::new(DeqNode::new(kd)); let node = self.write_order.push_back(node); entry.set_write_order_q_node(Some(node)); } - pub(crate) fn move_to_back_ao(&mut self, entry: &TrioArc>) { + pub(crate) fn move_to_back_ao(&mut self, entry: &MiniArc>) { if let Some(tagged_node) = entry.access_order_q_node() { let (node, tag) = tagged_node.decompose(); let p = unsafe { node.as_ref() }; @@ -95,7 +96,7 @@ impl Deques { pub(crate) fn move_to_back_ao_in_deque( deq_name: &str, deq: &mut Deque>, - entry: &TrioArc>, + entry: &MiniArc>, ) { if let Some(tagged_node) = entry.access_order_q_node() { let (node, tag) = tagged_node.decompose(); @@ -111,7 +112,7 @@ impl Deques { } } - pub(crate) fn move_to_back_wo(&mut self, entry: &TrioArc>) { + pub(crate) fn move_to_back_wo(&mut self, entry: &MiniArc>) { if let Some(node) = entry.write_order_q_node() { let p = unsafe { node.as_ref() }; if self.write_order.contains(p) { @@ -122,7 +123,7 @@ impl Deques { pub(crate) fn move_to_back_wo_in_deque( deq: &mut Deque>, - entry: &TrioArc>, + entry: &MiniArc>, ) { if let Some(node) = entry.write_order_q_node() { let p = unsafe { node.as_ref() }; @@ -132,7 +133,7 @@ impl Deques { } } - pub(crate) fn unlink_ao(&mut self, entry: &TrioArc>) { + pub(crate) fn unlink_ao(&mut self, entry: &MiniArc>) { if let Some(node) = entry.take_access_order_q_node() { self.unlink_node_ao(node); } @@ -141,14 +142,14 @@ impl Deques { pub(crate) fn unlink_ao_from_deque( deq_name: &str, deq: &mut Deque>, - entry: &TrioArc>, + entry: &MiniArc>, ) { if let Some(node) = entry.take_access_order_q_node() { unsafe { Self::unlink_node_ao_from_deque(deq_name, deq, node) }; } } - pub(crate) fn unlink_wo(deq: &mut Deque>, entry: &TrioArc>) { + pub(crate) fn unlink_wo(deq: &mut Deque>, entry: &MiniArc>) { if let Some(node) = entry.take_write_order_q_node() { Self::unlink_node_wo(deq, node); } diff --git a/src/common/timer_wheel.rs b/src/common/timer_wheel.rs index aa78affa..73add563 100644 --- a/src/common/timer_wheel.rs +++ b/src/common/timer_wheel.rs @@ -20,7 +20,7 @@ use super::{ }; use parking_lot::Mutex; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; const BUCKET_COUNTS: &[u64] = &[ 64, // roughly seconds @@ -69,16 +69,16 @@ pub(crate) enum TimerNode { /// The position (level and index) of the timer wheel bucket. pos: Option<(u8, u8)>, /// An Arc pointer to the `EntryInfo` of the cache entry (`ValueEntry`). - entry_info: TrioArc>, + entry_info: MiniArc>, /// An Arc pointer to the `DeqNodes` of the cache entry (`ValueEntry`). - deq_nodes: TrioArc>>, + deq_nodes: MiniArc>>, }, } impl TimerNode { fn new( - entry_info: TrioArc>, - deq_nodes: TrioArc>>, + entry_info: MiniArc>, + deq_nodes: MiniArc>>, level: usize, index: usize, ) -> Self { @@ -118,7 +118,7 @@ impl TimerNode { matches!(self, Self::Sentinel) } - pub(crate) fn entry_info(&self) -> &TrioArc> { + pub(crate) fn entry_info(&self) -> &MiniArc> { if let Self::Entry { entry_info, .. } = &self { entry_info } else { @@ -209,8 +209,8 @@ impl TimerWheel { /// Schedules a timer event for the node. pub(crate) fn schedule( &mut self, - entry_info: TrioArc>, - deq_nodes: TrioArc>>, + entry_info: MiniArc>, + deq_nodes: MiniArc>>, ) -> Option>>> { debug_assert!(self.is_enabled()); @@ -397,7 +397,7 @@ pub(crate) enum TimerEvent { // from one wheel to another in a lower level of the hierarchy. (This variant // is mainly used for testing) #[cfg(test)] - Rescheduled(TrioArc>), + Rescheduled(MiniArc>), #[cfg(not(test))] Rescheduled(()), /// This timer node (containing a cache entry) has been removed from the timer. @@ -517,7 +517,7 @@ impl<'iter, K> Iterator for TimerEventsIter<'iter, K> { // Get the entry info before rescheduling (mutating) the node to // avoid Stacked Borrows/Tree Borrows violations on `node_p`. let entry_info = - TrioArc::clone(unsafe { node_p.as_ref() }.element.entry_info()); + MiniArc::clone(unsafe { node_p.as_ref() }.element.entry_info()); match self.timer_wheel.schedule_existing_node(node_p) { ReschedulingResult::Rescheduled => { @@ -564,7 +564,7 @@ mod tests { time::{CheckedTimeOps, Clock, Instant, Mock}, }; - use triomphe::Arc as TrioArc; + use moka_arc::MiniArc; #[test] fn test_bucket_indices() { @@ -654,10 +654,10 @@ mod tests { let hash = key as u64; let key_hash = KeyHash::new(Arc::new(key), hash); let policy_weight = 0; - let entry_info = TrioArc::new(EntryInfo::new(key_hash, now, policy_weight)); + let entry_info = MiniArc::new(EntryInfo::new(key_hash, now, policy_weight)); entry_info.set_expiration_time(Some(now.checked_add(ttl).unwrap())); let deq_nodes = Default::default(); - let timer_node = timer.schedule(entry_info, TrioArc::clone(&deq_nodes)); + let timer_node = timer.schedule(entry_info, MiniArc::clone(&deq_nodes)); deq_nodes.lock().set_timer_node(timer_node); } diff --git a/src/future/base_cache.rs b/src/future/base_cache.rs index 0c9d0a5f..f302b66a 100644 --- a/src/future/base_cache.rs +++ b/src/future/base_cache.rs @@ -51,7 +51,7 @@ use std::{ }, time::{Duration, Instant as StdInstant}, }; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; pub(crate) type HouseKeeperArc = Arc; @@ -127,7 +127,7 @@ impl BaseCache { pub(crate) fn notify_invalidate( &self, key: &Arc, - entry: &TrioArc>, + entry: &MiniArc>, ) -> BoxFuture<'static, ()> where K: Send + Sync + 'static, @@ -335,7 +335,7 @@ where let ent = Entry::new(maybe_key, entry.value.clone(), false, false); let maybe_op = if record_read { Some(ReadOp::Hit { - value_entry: TrioArc::clone(entry), + value_entry: MiniArc::clone(entry), is_expiry_modified, }) } else { @@ -758,11 +758,11 @@ impl BaseCache { value: V, timestamp: Instant, policy_weight: u32, - ) -> (TrioArc>, u16) { + ) -> (MiniArc>, u16) { let key_hash = KeyHash::new(Arc::clone(key), hash); - let info = TrioArc::new(EntryInfo::new(key_hash, timestamp, policy_weight)); + let info = MiniArc::new(EntryInfo::new(key_hash, timestamp, policy_weight)); let gen: u16 = info.entry_gen(); - (TrioArc::new(ValueEntry::new(value, info)), gen) + (MiniArc::new(ValueEntry::new(value, info)), gen) } #[inline] @@ -772,15 +772,15 @@ impl BaseCache { timestamp: Instant, policy_weight: u32, other: &ValueEntry, - ) -> (TrioArc>, u16) { - let info = TrioArc::clone(other.entry_info()); + ) -> (MiniArc>, u16) { + let info = MiniArc::clone(other.entry_info()); // To prevent this updated ValueEntry from being evicted by an expiration // policy, increment the entry generation. let gen = info.incr_entry_gen(); info.set_last_accessed(timestamp); info.set_last_modified(timestamp); info.set_policy_weight(policy_weight); - (TrioArc::new(ValueEntry::new_from(value, info, other)), gen) + (MiniArc::new(ValueEntry::new_from(value, info, other)), gen) } fn expire_after_create( @@ -901,7 +901,7 @@ impl<'a, K, V> EvictionState<'a, K, V> { async fn notify_entry_removal( &mut self, key: Arc, - entry: &TrioArc>, + entry: &MiniArc>, cause: RemovalCause, ) where K: Send + Sync + 'static, @@ -991,7 +991,7 @@ enum AdmissionResult { Rejected, } -type CacheStore = crate::cht::SegmentedHashMap, TrioArc>, S>; +type CacheStore = crate::cht::SegmentedHashMap, MiniArc>, S>; struct Clocks { // Lock for this Clocks instance. Used when the `expiration_clock` is set. @@ -1285,7 +1285,7 @@ where where K: Borrow, Q: Hash + Eq + ?Sized, - F: FnOnce(&Arc, &TrioArc>) -> T, + F: FnOnce(&Arc, &MiniArc>) -> T, { self.cache .get_key_value_and(hash, |k| (k as &K).borrow() == key, with_entry) @@ -1296,7 +1296,7 @@ where where K: Borrow, Q: Hash + Eq + ?Sized, - F: FnOnce(&Arc, &TrioArc>) -> Option, + F: FnOnce(&Arc, &MiniArc>) -> Option, { self.cache .get_key_value_and_then(hash, |k| (k as &K).borrow() == key, with_entry) @@ -1337,7 +1337,7 @@ where /// Returns `true` if the entry is invalidated by `invalidate_entries_if` method. #[inline] - fn is_invalidated_entry(&self, key: &Arc, entry: &TrioArc>) -> bool + fn is_invalidated_entry(&self, key: &Arc, entry: &MiniArc>) -> bool where V: Clone, { @@ -1667,7 +1667,7 @@ where async fn handle_upsert( &self, kh: KeyHash, - entry: TrioArc>, + entry: MiniArc>, gen: u16, old_weight: u32, new_weight: u32, @@ -1717,7 +1717,7 @@ where kh.hash, |k| k == &kh.key, |_, current_entry| { - TrioArc::ptr_eq(entry.entry_info(), current_entry.entry_info()) + MiniArc::ptr_eq(entry.entry_info(), current_entry.entry_info()) && current_entry.entry_info().entry_gen() == gen }, ); @@ -1822,7 +1822,7 @@ where kh.hash, |k| k == &key, |_, current_entry| { - TrioArc::ptr_eq(entry.entry_info(), current_entry.entry_info()) + MiniArc::ptr_eq(entry.entry_info(), current_entry.entry_info()) && current_entry.entry_info().entry_gen() == gen }, ); @@ -1926,7 +1926,7 @@ where fn handle_admit( &self, - entry: &TrioArc>, + entry: &MiniArc>, policy_weight: u32, deqs: &mut Deques, timer_wheel: &mut TimerWheel, @@ -1951,7 +1951,7 @@ where /// NOTE: This method may enable the timer wheel. fn update_timer_wheel( &self, - entry: &TrioArc>, + entry: &MiniArc>, timer_wheel: &mut TimerWheel, ) { // Enable the timer wheel if needed. @@ -1971,8 +1971,8 @@ where // expiration time and not registered to the timer wheel. (true, None) => { let timer = timer_wheel.schedule( - TrioArc::clone(entry.entry_info()), - TrioArc::clone(entry.deq_nodes()), + MiniArc::clone(entry.entry_info()), + MiniArc::clone(entry.deq_nodes()), ); entry.set_timer_node(timer); } @@ -2000,7 +2000,7 @@ where fn handle_remove( deqs: &mut Deques, timer_wheel: &mut TimerWheel, - entry: TrioArc>, + entry: MiniArc>, gen: Option, counters: &mut EvictionCounters, ) { @@ -2012,7 +2012,7 @@ where fn handle_remove_without_timer_wheel( deqs: &mut Deques, - entry: TrioArc>, + entry: MiniArc>, gen: Option, counters: &mut EvictionCounters, ) { @@ -2035,7 +2035,7 @@ where ao_deq: &mut Deque>, wo_deq: &mut Deque>, timer_wheel: &mut TimerWheel, - entry: TrioArc>, + entry: MiniArc>, counters: &mut EvictionCounters, ) { if let Some(timer) = entry.take_timer_node() { @@ -2576,7 +2576,7 @@ where pub(crate) async fn notify_single_removal( &self, key: Arc, - entry: &TrioArc>, + entry: &MiniArc>, cause: RemovalCause, ) { if let Some(notifier) = &self.removal_notifier { @@ -2588,7 +2588,7 @@ where fn notify_upsert( &self, key: Arc, - entry: &TrioArc>, + entry: &MiniArc>, last_accessed: Option, last_modified: Option, ) -> BoxFuture<'static, ()> { @@ -2629,7 +2629,7 @@ where fn notify_invalidate( &self, key: &Arc, - entry: &TrioArc>, + entry: &MiniArc>, ) -> BoxFuture<'static, ()> { use futures_util::future::FutureExt; @@ -2715,7 +2715,7 @@ where /// Returns `true` if this entry is expired by its per-entry TTL. #[inline] -fn is_expired_by_per_entry_ttl(entry_info: &TrioArc>, now: Instant) -> bool { +fn is_expired_by_per_entry_ttl(entry_info: &MiniArc>, now: Instant) -> bool { if let Some(ts) = entry_info.expiration_time() { ts <= now } else { diff --git a/src/future/invalidator.rs b/src/future/invalidator.rs index 4c159f6c..27e8f1e2 100644 --- a/src/future/invalidator.rs +++ b/src/future/invalidator.rs @@ -16,7 +16,7 @@ use std::{ Arc, }, }; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; use uuid::Uuid; pub(crate) type PredicateFun = Arc bool + Send + Sync + 'static>; @@ -141,7 +141,7 @@ impl Invalidator { // This method will be called by the get method of Cache. #[inline] - pub(crate) fn apply_predicates(&self, key: &Arc, entry: &TrioArc>) -> bool + pub(crate) fn apply_predicates(&self, key: &Arc, entry: &MiniArc>) -> bool where K: Hash + Eq + Send + Sync + 'static, V: Clone + Send + Sync + 'static, @@ -296,7 +296,7 @@ where key: &Arc, hash: u64, ts: Instant, - ) -> Option>> + ) -> Option>> where K: Send + Sync + 'static, V: Clone + Send + Sync + 'static, diff --git a/src/future/key_lock.rs b/src/future/key_lock.rs index fc06ff2f..6449aa10 100644 --- a/src/future/key_lock.rs +++ b/src/future/key_lock.rs @@ -6,11 +6,11 @@ use std::{ use crate::cht::SegmentedHashMap; use async_lock::{Mutex, MutexGuard}; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; const LOCK_MAP_NUM_SEGMENTS: usize = 64; -type LockMap = SegmentedHashMap, TrioArc>, S>; +type LockMap = SegmentedHashMap, MiniArc>, S>; // We need the `where` clause here because of the Drop impl. pub(crate) struct KeyLock<'a, K, S> @@ -21,7 +21,7 @@ where map: &'a LockMap, key: Arc, hash: u64, - lock: TrioArc>, + lock: MiniArc>, } impl<'a, K, S> Drop for KeyLock<'a, K, S> @@ -30,11 +30,11 @@ where S: BuildHasher, { fn drop(&mut self) { - if TrioArc::count(&self.lock) <= 2 { + if MiniArc::count(&self.lock) <= 2 { self.map.remove_if( self.hash, |k| k == &self.key, - |_k, v| TrioArc::count(v) <= 2, + |_k, v| MiniArc::count(v) <= 2, ); } } @@ -45,7 +45,7 @@ where K: Eq + Hash, S: BuildHasher, { - fn new(map: &'a LockMap, key: &Arc, hash: u64, lock: TrioArc>) -> Self { + fn new(map: &'a LockMap, key: &Arc, hash: u64, lock: MiniArc>) -> Self { Self { map, key: Arc::clone(key), @@ -76,7 +76,7 @@ where pub(crate) fn key_lock(&self, key: &Arc) -> KeyLock<'_, K, S> { let hash = self.locks.hash(key); - let kl = TrioArc::new(Mutex::new(())); + let kl = MiniArc::new(Mutex::new(())); match self .locks .insert_if_not_present(Arc::clone(key), hash, kl.clone()) diff --git a/src/future/value_initializer.rs b/src/future/value_initializer.rs index 63b7a341..53d40929 100644 --- a/src/future/value_initializer.rs +++ b/src/future/value_initializer.rs @@ -8,7 +8,7 @@ use std::{ pin::Pin, sync::Arc, }; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; use crate::{ ops::compute::{CompResult, Op}, @@ -49,7 +49,7 @@ impl fmt::Debug for WaiterValue { } } -type Waiter = TrioArc>>; +type Waiter = MiniArc>>; type WaiterMap = crate::cht::SegmentedHashMap<(Arc, TypeId), Waiter, S>; struct WaiterGuard<'a, K, V, S> @@ -116,7 +116,7 @@ pub(crate) struct ValueInitializer { // try_get_with method. We use the type ID as a part of the key to ensure that we // can always downcast the trait object ErrorObject (in Waiter) into its // concrete type. - waiters: TrioArc>, + waiters: MiniArc>, } impl ValueInitializer @@ -127,7 +127,7 @@ where { pub(crate) fn with_hasher(hasher: S) -> Self { Self { - waiters: TrioArc::new(crate::cht::SegmentedHashMap::with_num_segments_and_hasher( + waiters: MiniArc::new(crate::cht::SegmentedHashMap::with_num_segments_and_hasher( WAITER_MAP_NUM_SEGMENTS, hasher, )), @@ -172,7 +172,7 @@ where let (w_key, w_hash) = waiter_key_hash(&self.waiters, c_key, type_id); - let waiter = TrioArc::new(RwLock::new(WaiterValue::Computing)); + let waiter = MiniArc::new(RwLock::new(WaiterValue::Computing)); // NOTE: We have to acquire a write lock before `try_insert_waiter`, // so that any concurrent attempt will get our lock and wait on it. let lock = waiter.write().await; @@ -281,7 +281,7 @@ where let type_id = TypeId::of::(); let (w_key, w_hash) = waiter_key_hash(&self.waiters, &c_key, type_id); - let waiter = TrioArc::new(RwLock::new(WaiterValue::Computing)); + let waiter = MiniArc::new(RwLock::new(WaiterValue::Computing)); // NOTE: We have to acquire a write lock before `try_insert_waiter`, // so that any concurrent attempt will get our lock and wait on it. let lock = waiter.write().await; @@ -482,7 +482,7 @@ where (Arc, TypeId): Eq + Hash, S: BuildHasher, { - let waiter = TrioArc::clone(waiter); + let waiter = MiniArc::clone(waiter); waiter_map.insert_if_not_present(w_key, w_hash, waiter) } diff --git a/src/sync/value_initializer.rs b/src/sync/value_initializer.rs index f66aa9ef..90528f46 100644 --- a/src/sync/value_initializer.rs +++ b/src/sync/value_initializer.rs @@ -5,7 +5,7 @@ use std::{ hash::{BuildHasher, Hash}, sync::Arc, }; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; use crate::{ ops::compute::{CompResult, Op}, @@ -38,7 +38,7 @@ impl fmt::Debug for WaiterValue { } } -type Waiter = TrioArc>>; +type Waiter = MiniArc>>; pub(crate) enum InitResult { Initialized(V), @@ -96,7 +96,7 @@ where let (w_key, w_hash) = self.waiter_key_hash(key, type_id); - let waiter = TrioArc::new(RwLock::new(WaiterValue::Computing)); + let waiter = MiniArc::new(RwLock::new(WaiterValue::Computing)); let mut lock = waiter.write(); loop { @@ -194,7 +194,7 @@ where let type_id = TypeId::of::(); let (w_key, w_hash) = self.waiter_key_hash(&c_key, type_id); - let waiter = TrioArc::new(RwLock::new(WaiterValue::Computing)); + let waiter = MiniArc::new(RwLock::new(WaiterValue::Computing)); // NOTE: We have to acquire a write lock before `try_insert_waiter`, // so that any concurrent attempt will get our lock and wait on it. let mut lock = waiter.write(); @@ -370,7 +370,7 @@ where w_hash: u64, waiter: &Waiter, ) -> Option> { - let waiter = TrioArc::clone(waiter); + let waiter = MiniArc::clone(waiter); self.waiters.insert_if_not_present(w_key, w_hash, waiter) } diff --git a/src/sync_base/base_cache.rs b/src/sync_base/base_cache.rs index fe951d29..d262cd59 100644 --- a/src/sync_base/base_cache.rs +++ b/src/sync_base/base_cache.rs @@ -45,7 +45,7 @@ use std::{ }, time::{Duration, Instant as StdInstant}, }; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; pub(crate) type HouseKeeperArc = Arc; @@ -109,7 +109,7 @@ impl BaseCache { self.inner.current_time_from_expiration_clock() } - pub(crate) fn notify_invalidate(&self, key: &Arc, entry: &TrioArc>) + pub(crate) fn notify_invalidate(&self, key: &Arc, entry: &MiniArc>) where K: Send + Sync + 'static, V: Clone + Send + Sync + 'static, @@ -306,7 +306,7 @@ where } else { // Valid entry. let maybe_key = if need_key { Some(Arc::clone(k)) } else { None }; - Some((maybe_key, TrioArc::clone(entry))) + Some((maybe_key, MiniArc::clone(entry))) } }); @@ -630,11 +630,11 @@ impl BaseCache { value: V, timestamp: Instant, policy_weight: u32, - ) -> (TrioArc>, u16) { + ) -> (MiniArc>, u16) { let key_hash = KeyHash::new(Arc::clone(key), hash); - let info = TrioArc::new(EntryInfo::new(key_hash, timestamp, policy_weight)); + let info = MiniArc::new(EntryInfo::new(key_hash, timestamp, policy_weight)); let gen: u16 = info.entry_gen(); - (TrioArc::new(ValueEntry::new(value, info)), gen) + (MiniArc::new(ValueEntry::new(value, info)), gen) } #[inline] @@ -644,15 +644,15 @@ impl BaseCache { timestamp: Instant, policy_weight: u32, other: &ValueEntry, - ) -> (TrioArc>, u16) { - let info = TrioArc::clone(other.entry_info()); + ) -> (MiniArc>, u16) { + let info = MiniArc::clone(other.entry_info()); // To prevent this updated ValueEntry from being evicted by an expiration // policy, increment the entry generation. let gen = info.incr_entry_gen(); info.set_last_accessed(timestamp); info.set_last_modified(timestamp); info.set_policy_weight(policy_weight); - (TrioArc::new(ValueEntry::new_from(value, info, other)), gen) + (MiniArc::new(ValueEntry::new_from(value, info, other)), gen) } fn expire_after_create( @@ -773,7 +773,7 @@ impl<'a, K, V> EvictionState<'a, K, V> { fn notify_entry_removal( &mut self, key: Arc, - entry: &TrioArc>, + entry: &MiniArc>, cause: RemovalCause, ) where K: Send + Sync + 'static, @@ -863,7 +863,7 @@ enum AdmissionResult { Rejected, } -type CacheStore = crate::cht::SegmentedHashMap, TrioArc>, S>; +type CacheStore = crate::cht::SegmentedHashMap, MiniArc>, S>; struct Clocks { has_expiration_clock: AtomicBool, @@ -1140,7 +1140,7 @@ where where K: Borrow, Q: Hash + Eq + ?Sized, - F: FnOnce(&Arc, &TrioArc>) -> T, + F: FnOnce(&Arc, &MiniArc>) -> T, { self.cache .get_key_value_and(hash, |k| (k as &K).borrow() == key, with_entry) @@ -1151,7 +1151,7 @@ where where K: Borrow, Q: Hash + Eq + ?Sized, - F: FnOnce(&Arc, &TrioArc>) -> Option, + F: FnOnce(&Arc, &MiniArc>) -> Option, { self.cache .get_key_value_and_then(hash, |k| (k as &K).borrow() == key, with_entry) @@ -1192,7 +1192,7 @@ where /// Returns `true` if the entry is invalidated by `invalidate_entries_if` method. #[inline] - fn is_invalidated_entry(&self, key: &Arc, entry: &TrioArc>) -> bool + fn is_invalidated_entry(&self, key: &Arc, entry: &MiniArc>) -> bool where V: Clone, { @@ -1515,7 +1515,7 @@ where fn handle_upsert( &self, kh: KeyHash, - entry: TrioArc>, + entry: MiniArc>, gen: u16, old_weight: u32, new_weight: u32, @@ -1561,7 +1561,7 @@ where kh.hash, |k| k == &kh.key, |_, current_entry| { - TrioArc::ptr_eq(entry.entry_info(), current_entry.entry_info()) + MiniArc::ptr_eq(entry.entry_info(), current_entry.entry_info()) && current_entry.entry_info().entry_gen() == gen }, ); @@ -1658,7 +1658,7 @@ where kh.hash, |k| k == &key, |_, current_entry| { - TrioArc::ptr_eq(entry.entry_info(), current_entry.entry_info()) + MiniArc::ptr_eq(entry.entry_info(), current_entry.entry_info()) && current_entry.entry_info().entry_gen() == gen }, ); @@ -1760,7 +1760,7 @@ where fn handle_admit( &self, - entry: &TrioArc>, + entry: &MiniArc>, policy_weight: u32, deqs: &mut Deques, timer_wheel: &mut TimerWheel, @@ -1785,7 +1785,7 @@ where /// NOTE: This method may enable the timer wheel. fn update_timer_wheel( &self, - entry: &TrioArc>, + entry: &MiniArc>, timer_wheel: &mut TimerWheel, ) { // Enable the timer wheel if needed. @@ -1805,8 +1805,8 @@ where // expiration time and not registered to the timer wheel. (true, None) => { let timer = timer_wheel.schedule( - TrioArc::clone(entry.entry_info()), - TrioArc::clone(entry.deq_nodes()), + MiniArc::clone(entry.entry_info()), + MiniArc::clone(entry.deq_nodes()), ); entry.set_timer_node(timer); } @@ -1834,7 +1834,7 @@ where fn handle_remove( deqs: &mut Deques, timer_wheel: &mut TimerWheel, - entry: TrioArc>, + entry: MiniArc>, gen: Option, counters: &mut EvictionCounters, ) { @@ -1846,7 +1846,7 @@ where fn handle_remove_without_timer_wheel( deqs: &mut Deques, - entry: TrioArc>, + entry: MiniArc>, gen: Option, counters: &mut EvictionCounters, ) { @@ -1869,7 +1869,7 @@ where ao_deq: &mut Deque>, wo_deq: &mut Deque>, timer_wheel: &mut TimerWheel, - entry: TrioArc>, + entry: MiniArc>, counters: &mut EvictionCounters, ) { if let Some(timer) = entry.take_timer_node() { @@ -2368,7 +2368,7 @@ where pub(crate) fn notify_single_removal( &self, key: Arc, - entry: &TrioArc>, + entry: &MiniArc>, cause: RemovalCause, ) { if let Some(notifier) = &self.removal_notifier { @@ -2380,7 +2380,7 @@ where fn notify_upsert( &self, key: Arc, - entry: &TrioArc>, + entry: &MiniArc>, last_accessed: Option, last_modified: Option, ) { @@ -2407,7 +2407,7 @@ where } #[inline] - fn notify_invalidate(&self, key: &Arc, entry: &TrioArc>) { + fn notify_invalidate(&self, key: &Arc, entry: &MiniArc>) { let now = self.current_time_from_expiration_clock(); let exp = &self.expiration_policy; @@ -2480,7 +2480,7 @@ where /// Returns `true` if this entry is expired by its per-entry TTL. #[inline] -fn is_expired_by_per_entry_ttl(entry_info: &TrioArc>, now: Instant) -> bool { +fn is_expired_by_per_entry_ttl(entry_info: &MiniArc>, now: Instant) -> bool { if let Some(ts) = entry_info.expiration_time() { ts <= now } else { diff --git a/src/sync_base/invalidator.rs b/src/sync_base/invalidator.rs index dfb62c42..f281f709 100644 --- a/src/sync_base/invalidator.rs +++ b/src/sync_base/invalidator.rs @@ -16,7 +16,7 @@ use std::{ Arc, }, }; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; use uuid::Uuid; pub(crate) type PredicateFun = Arc bool + Send + Sync + 'static>; @@ -141,7 +141,7 @@ impl Invalidator { // This method will be called by the get method of Cache. #[inline] - pub(crate) fn apply_predicates(&self, key: &Arc, entry: &TrioArc>) -> bool + pub(crate) fn apply_predicates(&self, key: &Arc, entry: &MiniArc>) -> bool where K: Hash + Eq + Send + Sync + 'static, V: Clone + Send + Sync + 'static, @@ -296,7 +296,7 @@ where key: &Arc, hash: u64, ts: Instant, - ) -> Option>> + ) -> Option>> where K: Send + Sync + 'static, V: Clone + Send + Sync + 'static, diff --git a/src/sync_base/key_lock.rs b/src/sync_base/key_lock.rs index abc96873..fc5ad402 100644 --- a/src/sync_base/key_lock.rs +++ b/src/sync_base/key_lock.rs @@ -6,11 +6,11 @@ use std::{ use crate::cht::SegmentedHashMap; use parking_lot::{Mutex, MutexGuard}; -use triomphe::Arc as TrioArc; +use moka_arc::MiniArc; const LOCK_MAP_NUM_SEGMENTS: usize = 64; -type LockMap = SegmentedHashMap, TrioArc>, S>; +type LockMap = SegmentedHashMap, MiniArc>, S>; // We need the `where` clause here because of the Drop impl. pub(crate) struct KeyLock<'a, K, S> @@ -21,7 +21,7 @@ where map: &'a LockMap, key: Arc, hash: u64, - lock: TrioArc>, + lock: MiniArc>, } impl<'a, K, S> Drop for KeyLock<'a, K, S> @@ -30,11 +30,11 @@ where S: BuildHasher, { fn drop(&mut self) { - if TrioArc::count(&self.lock) <= 2 { + if MiniArc::count(&self.lock) <= 2 { self.map.remove_if( self.hash, |k| k == &self.key, - |_k, v| TrioArc::count(v) <= 2, + |_k, v| MiniArc::count(v) <= 2, ); } } @@ -45,7 +45,7 @@ where K: Eq + Hash, S: BuildHasher, { - fn new(map: &'a LockMap, key: &Arc, hash: u64, lock: TrioArc>) -> Self { + fn new(map: &'a LockMap, key: &Arc, hash: u64, lock: MiniArc>) -> Self { Self { map, key: Arc::clone(key), @@ -76,7 +76,7 @@ where pub(crate) fn key_lock(&self, key: &Arc) -> KeyLock<'_, K, S> { let hash = self.locks.hash(key); - let kl = TrioArc::new(Mutex::new(())); + let kl = MiniArc::new(Mutex::new(())); match self .locks .insert_if_not_present(Arc::clone(key), hash, kl.clone())