diff --git a/src/hashbrown/control/bitmask.rs b/src/hashbrown/control/bitmask.rs index d811fab..0462765 100644 --- a/src/hashbrown/control/bitmask.rs +++ b/src/hashbrown/control/bitmask.rs @@ -1,7 +1,7 @@ -use super::group::BitMaskWord; -use super::group::NonZeroBitMaskWord; use super::group::BITMASK_ITER_MASK; use super::group::BITMASK_STRIDE; +use super::group::BitMaskWord; +use super::group::NonZeroBitMaskWord; /// A bit mask which contains the result of a `Match` operation on a `Group` and /// allows iterating through them. diff --git a/src/hashbrown/control/group/mod.rs b/src/hashbrown/control/group/mod.rs index ac0ac4f..8296dc8 100644 --- a/src/hashbrown/control/group/mod.rs +++ b/src/hashbrown/control/group/mod.rs @@ -43,8 +43,8 @@ cfg_if::cfg_if! { use generic as imp; } } +pub(super) use self::imp::BITMASK_ITER_MASK; +pub(super) use self::imp::BITMASK_STRIDE; pub(super) use self::imp::BitMaskWord; pub(crate) use self::imp::Group; pub(super) use self::imp::NonZeroBitMaskWord; -pub(super) use self::imp::BITMASK_ITER_MASK; -pub(super) use self::imp::BITMASK_STRIDE; diff --git a/src/internal/linked_list.rs b/src/internal/linked_list.rs index b3473ac..06487e6 100644 --- a/src/internal/linked_list.rs +++ b/src/internal/linked_list.rs @@ -1,10 +1,9 @@ +use std::alloc::Layout; use std::alloc::alloc; use std::alloc::dealloc; use std::alloc::handle_alloc_error; -use std::alloc::Layout; use std::marker::PhantomData; -use std::ptr::NonNull; -use std::ptr::{self}; +use std::ptr; /// Maximum number of nodes parked on the free list const FREE_LIST_CAPACITY: usize = 256; @@ -41,7 +40,7 @@ pub struct Node { /// A doubly-linked list with an internal, bounded free-list for node reuse. pub struct LinkedList { - sentinel: NonNull, + sentinel: ptr::NonNull, free_head: *mut Links, free_len: usize, len: usize, @@ -60,7 +59,7 @@ impl LinkedList { /// - The caller must not use `node` again as an "unlinked" node until it is /// unlinked again. #[inline] - unsafe fn push_front_node(&mut self, node: NonNull) { + unsafe fn push_front_node(&mut self, node: ptr::NonNull) { let s = self.sentinel.as_ptr(); let n = node.as_ptr(); unsafe { @@ -79,7 +78,7 @@ impl LinkedList { /// /// Same contract as [`push_front_node`](Self::push_front_node). #[inline] - unsafe fn push_back_node(&mut self, node: NonNull) { + unsafe fn push_back_node(&mut self, node: ptr::NonNull) { let s = self.sentinel.as_ptr(); let n = node.as_ptr(); unsafe { @@ -94,7 +93,7 @@ impl LinkedList { /// Removes and returns the node at the front of the list, if any. #[inline] - fn pop_front_node(&mut self) -> Option> { + fn pop_front_node(&mut self) -> Option> { if self.len == 0 { return None; } @@ -109,12 +108,12 @@ impl LinkedList { }; self.len -= 1; // SAFETY: read from a valid node above, so non-null. - Some(unsafe { NonNull::new_unchecked(node) }) + Some(unsafe { ptr::NonNull::new_unchecked(node) }) } /// Removes and returns the node at the back of the list, if any. #[inline] - fn pop_back_node(&mut self) -> Option> { + fn pop_back_node(&mut self) -> Option> { if self.len == 0 { return None; } @@ -129,7 +128,7 @@ impl LinkedList { }; self.len -= 1; // SAFETY: read from a valid node above, so non-null. - Some(unsafe { NonNull::new_unchecked(node) }) + Some(unsafe { ptr::NonNull::new_unchecked(node) }) } /// Unlinks `node` from the list, without deallocating or reading its element. @@ -138,7 +137,7 @@ impl LinkedList { /// /// `node` must point to a node currently linked into this list (not the sentinel). #[inline] - unsafe fn unlink_node(&mut self, node: NonNull) { + unsafe fn unlink_node(&mut self, node: ptr::NonNull) { let n = node.as_ptr(); unsafe { // SAFETY: `n` linked per fn contract; `prev`/`next` valid. @@ -181,7 +180,7 @@ impl LinkedList { /// `node` must point to a node whose `element` is initialized and which is /// no longer linked into the list (already unlinked by the caller). #[inline] - unsafe fn take_element_and_recycle(&mut self, node: NonNull) -> T { + unsafe fn take_element_and_recycle(&mut self, node: ptr::NonNull) -> T { let node_ptr = node.as_ptr() as *mut Node; // SAFETY: `element` initialized per fn contract; `ptr::read` doesn't drop. let element = unsafe { ptr::read(&(*node_ptr).element) }; @@ -196,7 +195,7 @@ impl LinkedList { /// /// `node` must point to a node currently linked into this list #[inline] - unsafe fn remove_node(&mut self, node: NonNull) -> T { + unsafe fn remove_node(&mut self, node: ptr::NonNull) -> T { // SAFETY: caller guarantees `node` is linked. unsafe { self.unlink_node(node) }; self.len -= 1; @@ -304,7 +303,7 @@ impl LinkedList { p }; // SAFETY: non-null checked above (or diverged). - let sentinel = unsafe { NonNull::new_unchecked(raw) }; + let sentinel = unsafe { ptr::NonNull::new_unchecked(raw) }; LinkedList { sentinel, free_head: ptr::null_mut(), @@ -350,7 +349,7 @@ impl LinkedList { let node = unsafe { (*s).next }; // SAFETY: non-null above; layout-compatible (see `Node` docs). Some(Cursor(unsafe { - NonNull::new_unchecked(node as *mut Node) + ptr::NonNull::new_unchecked(node as *mut Node) })) } @@ -366,7 +365,7 @@ impl LinkedList { let node = unsafe { (*s).prev }; // SAFETY: same as `cursor_front`. Some(Cursor(unsafe { - NonNull::new_unchecked(node as *mut Node) + ptr::NonNull::new_unchecked(node as *mut Node) })) } @@ -375,13 +374,13 @@ impl LinkedList { pub fn push_front(&mut self, elt: T) -> Cursor { let node = self.get_node(elt); // SAFETY: `get_node` never returns null. - let links = unsafe { NonNull::new_unchecked(node as *mut Links) }; + let links = unsafe { ptr::NonNull::new_unchecked(node as *mut Links) }; // SAFETY: freshly obtained node, unlinked (see `push_front_node` contract). unsafe { self.push_front_node(links) }; self.len += 1; // SAFETY: same pointer validated above. - Cursor(unsafe { NonNull::new_unchecked(node) }) + Cursor(unsafe { ptr::NonNull::new_unchecked(node) }) } /// Removes and returns the front element, or `None` if the list is empty. @@ -399,14 +398,14 @@ impl LinkedList { pub fn push_back(&mut self, elt: T) -> Cursor { let node = self.get_node(elt); // SAFETY: `get_node` always returns a non-null, freshly-usable `Node` pointer. - let links = unsafe { NonNull::new_unchecked(node as *mut Links) }; + let links = unsafe { ptr::NonNull::new_unchecked(node as *mut Links) }; // SAFETY: `links` refers to a node that was just obtained and is // not linked into any list yet. unsafe { self.push_back_node(links) }; self.len += 1; // SAFETY: `node` is the same non-null pointer validated above. - Cursor(unsafe { NonNull::new_unchecked(node) }) + Cursor(unsafe { ptr::NonNull::new_unchecked(node) }) } /// Removes and returns the back element, or `None` if the list is empty. @@ -476,7 +475,7 @@ impl Drop for LinkedList { /// later access or reposition that node via [`Cursor::element`], /// [`Cursor::move_to_front`], [`Cursor::move_to_back`], or [`Cursor::unlink`]. /// -/// `#[repr(transparent)]` over `NonNull>` means a `Cursor` has the +/// `#[repr(transparent)]` over `ptr::NonNull>` means a `Cursor` has the /// exact same layout as the raw pointer it wraps — no extra state is tracked, /// so the cursor does *not* know which `LinkedList` it came from, whether the /// node is still linked, or whether other `Cursor`s alias the same node. All @@ -487,9 +486,9 @@ impl Drop for LinkedList { /// equality (`PartialEq`/`Eq`) compares the underlying pointer, i.e. identity /// of the node, not the value of the element. #[repr(transparent)] -pub struct Cursor(NonNull>); +pub struct Cursor(ptr::NonNull>); -// `NonNull>` is just a pointer; copying it is always safe. +// `ptr::NonNull>` is just a pointer; copying it is always safe. impl Clone for Cursor { #[inline] fn clone(&self) -> Self { @@ -514,10 +513,10 @@ impl Cursor { /// Relies on `#[repr(C)]` on `Node` placing `links` at offset 0, so the /// cast is always valid regardless of `T`. #[inline] - fn links(&self) -> NonNull { + fn links(&self) -> ptr::NonNull { let ptr = self.0.as_ptr() as *mut Links; - // SAFETY: `self.0` is `NonNull`, so the reinterpreted pointer is non-null too. - unsafe { NonNull::new_unchecked(ptr) } + // SAFETY: `self.0` is `ptr::NonNull`, so the reinterpreted pointer is non-null too. + unsafe { ptr::NonNull::new_unchecked(ptr) } } /// Returns a reference to the node's element, with a caller-chosen lifetime. @@ -619,7 +618,7 @@ impl Cursor { pub struct RawIter { next: *mut Links, len: usize, - _marker: PhantomData>>, + _marker: PhantomData>>, } impl Iterator for RawIter { @@ -639,7 +638,7 @@ impl Iterator for RawIter { self.next = unsafe { (*node).next }; // SAFETY: `node` is non-null (came from a valid linked `Links`), and // by `#[repr(C)]` on `Node` a `*mut Links` is a valid `*mut Node`. - let cursor = Cursor(unsafe { NonNull::new_unchecked(node as *mut Node) }); + let cursor = Cursor(unsafe { ptr::NonNull::new_unchecked(node as *mut Node) }); Some(cursor) } diff --git a/src/macro_rules.rs b/src/macro_rules.rs index 0328ab4..aaf70dd 100644 --- a/src/macro_rules.rs +++ b/src/macro_rules.rs @@ -54,7 +54,7 @@ macro_rules! implement_view_guard { let cache = self.cache.get(); let inner = cache.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); if self.initial_gv == inner.shared().generation_version().get() { return Ok(policy); diff --git a/src/policies/fifopolicy.rs b/src/policies/fifopolicy.rs index 0199c86..61f8547 100644 --- a/src/policies/fifopolicy.rs +++ b/src/policies/fifopolicy.rs @@ -166,10 +166,11 @@ impl FIFOPolicy { #[inline] fn decrement_indexes(&mut self, start: usize, end: usize) { - const MAX_FRONT_OFFSET: usize = cfg_select! { - feature = "use-small-offset" => u8::MAX as usize, - not(feature = "use-small-offset") => usize::MAX - isize::MAX as usize, - }; + const MAX_FRONT_OFFSET: usize = + cfg_select! { + feature = "use-small-offset" => u8::MAX as usize, + not(feature = "use-small-offset") => usize::MAX - isize::MAX as usize, + }; // Fast path: shifting the entire front is a single counter increment. // Guard against overflow; the full-normalization path below handles that case. diff --git a/src/policies/ttlpolicy.rs b/src/policies/ttlpolicy.rs index 70b24ce..89bf82e 100644 --- a/src/policies/ttlpolicy.rs +++ b/src/policies/ttlpolicy.rs @@ -256,10 +256,11 @@ impl TTLPolicy { #[inline] fn decrement_indexes(&mut self, start: usize, end: usize) { - const MAX_FRONT_OFFSET: usize = cfg_select! { - feature = "use-small-offset" => u8::MAX as usize, - not(feature = "use-small-offset") => usize::MAX - isize::MAX as usize, - }; + const MAX_FRONT_OFFSET: usize = + cfg_select! { + feature = "use-small-offset" => u8::MAX as usize, + not(feature = "use-small-offset") => usize::MAX - isize::MAX as usize, + }; // Fast path: shifting the entire front is a single counter increment. // Guard against overflow; the full-normalization path below handles that case. diff --git a/src/policies/wrapped.rs b/src/policies/wrapped.rs index 7eabc6a..f1fcc84 100644 --- a/src/policies/wrapped.rs +++ b/src/policies/wrapped.rs @@ -53,7 +53,7 @@ impl Wrapped

{ /// Acquires the mutex and returns a guard over the mutable policy state. // https://github.com/awolverp/cachebox/issues/95 #[inline(always)] - pub fn policy_py_attached(&self, py: pyo3::Python<'_>) -> parking_lot::MutexGuard<'_, P> { + pub fn policy(&self, py: pyo3::Python<'_>) -> parking_lot::MutexGuard<'_, P> { pyo3::sync::MutexExt::lock_py_attached(&self.inner, py) } @@ -63,7 +63,7 @@ impl Wrapped

{ /// The mutexes are always taken in address order, so `a == b` and `b == a` /// on two threads cannot each hold one lock and wait for the other. // https://github.com/awolverp/cachebox/issues/118 - pub fn policy_pair_py_attached<'a>( + pub fn policy_ordered_pair<'a>( &'a self, other: &'a Self, py: pyo3::Python<'_>, @@ -74,11 +74,11 @@ impl Wrapped

{ debug_assert!(!std::ptr::eq(self, other)); if (self as *const Self) < (other as *const Self) { - let first = self.policy_py_attached(py); - (first, other.policy_py_attached(py)) + let first = self.policy(py); + (first, other.policy(py)) } else { - let first = other.policy_py_attached(py); - (self.policy_py_attached(py), first) + let first = other.policy(py); + (self.policy(py), first) } } @@ -100,16 +100,14 @@ const EXTEND_CHUNK: usize = 1024; /// Built-in containers whose iteration runs no Python code. Exact types only: /// a subclass can override `__iter__`. fn iterates_without_python(obj: &alias::BoundObject) -> bool { - use pyo3::types::PyAnyMethods; - use pyo3::types::PyFrozenSet; - use pyo3::types::PyList; - use pyo3::types::PySet; - use pyo3::types::PyTuple; - - obj.is_exact_instance_of::() - || obj.is_exact_instance_of::() - || obj.is_exact_instance_of::() - || obj.is_exact_instance_of::() + let ptr = obj.as_ptr(); + + unsafe { + pyo3::ffi::Py_TYPE(ptr) == crate::typeref::STD_FROZENSET_TYPE + || pyo3::ffi::Py_TYPE(ptr) == crate::typeref::STD_LIST_TYPE + || pyo3::ffi::Py_TYPE(ptr) == crate::typeref::STD_SET_TYPE + || pyo3::ffi::Py_TYPE(ptr) == crate::typeref::STD_TUPLE_TYPE + } } #[inline(always)] @@ -156,7 +154,7 @@ impl Wrapped

{ /// Returns the remaining size. Equals to `maxsize - current_size`. #[inline] pub fn remaining_size(&self, py: pyo3::Python<'_>) -> usize { - let policy = self.policy_py_attached(py); + let policy = self.policy(py); self.shared.maxsize().saturating_sub(policy.current_size()) } @@ -167,7 +165,7 @@ impl Wrapped

{ py: pyo3::Python<'_>, key: &::Key, ) -> pyo3::PyResult { - let mut lock = self.policy_py_attached(py); + let mut lock = self.policy(py); let handle = lock.get(py, key, &self.shared)?; Ok(handle.is_some()) @@ -195,7 +193,7 @@ impl Wrapped

{ py: pyo3::Python<'_>, handle: P::Handle, ) -> pyo3::PyResult> { - let mut lock = self.policy_py_attached(py); + let mut lock = self.policy(py); self.insert_no_lock(&mut lock, py, handle) } @@ -207,7 +205,7 @@ impl Wrapped

{ py: pyo3::Python<'_>, key: &::Key, ) -> pyo3::PyResult> { - let mut lock = self.policy_py_attached(py); + let mut lock = self.policy(py); let entry = lock.entry(py, key, &self.shared)?; match entry { @@ -251,13 +249,11 @@ impl Wrapped

{ let py = iterable.py(); - // Using [pyo3::ffi::PyObject_TypeCheck] and [Bound::cast_unchecked] is so faster than [Bound::cast] - let is_dictionary = unsafe { + if unsafe { pyo3::ffi::PyObject_TypeCheck(iterable.as_ptr(), crate::typeref::STD_DICT_TYPE) == 1 - }; - if is_dictionary { + } { let dict = unsafe { iterable.cast_unchecked::() }; - let mut lock = self.policy_py_attached(py); + let mut lock = self.policy(py); for pair in dict.items() { let (key, value) = unsafe { @@ -272,7 +268,7 @@ impl Wrapped

{ } if iterates_without_python(&iterable) { - let mut lock = self.policy_py_attached(py); + let mut lock = self.policy(py); for pair in iterable.try_iter()? { let pair = pair?; @@ -324,7 +320,7 @@ impl Wrapped

{ // Declared before the lock, so after an error the handles not inserted // are dropped (and their finalizers run) once the lock is released. let mut pending = handles.drain(..); - let mut lock = self.policy_py_attached(py); + let mut lock = self.policy(py); pending.try_for_each(|handle| insert_inner(&mut lock, &self.shared, py, handle).map(drop)) } @@ -340,7 +336,7 @@ impl Wrapped

{ return Ok(0); } - let mut lock = self.policy_py_attached(py); + let mut lock = self.policy(py); let mut count: pyo3::ffi::Py_ssize_t = 0; while count < n { @@ -364,7 +360,7 @@ impl Wrapped

{ #[inline] pub fn clone_ref(&self, py: pyo3::Python) -> Self { let shared = self.shared.clone_ref(py); - let policy = self.policy_py_attached(py).clone_ref(py); + let policy = self.policy(py).clone_ref(py); Self { shared, diff --git a/src/pyclasses/cache.rs b/src/pyclasses/cache.rs index df4f7be..0b9a305 100644 --- a/src/pyclasses/cache.rs +++ b/src/pyclasses/cache.rs @@ -119,7 +119,7 @@ impl PyCache { #[inline] fn current_size(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - Ok(inner.policy_py_attached(py).current_size()) + Ok(inner.policy(py).current_size()) } #[inline] @@ -139,7 +139,7 @@ impl PyCache { #[inline] fn capacity(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().capacity()) } @@ -148,7 +148,7 @@ impl PyCache { #[inline] fn __len__(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().len()) } @@ -160,7 +160,7 @@ impl PyCache { let Ok(inner) = self.0.get() else { return FIXED_SIZE; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); FIXED_SIZE + (policy.table().capacity() * size_of::()) } @@ -171,7 +171,7 @@ impl PyCache { return false; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); !policy.table().is_empty() } @@ -193,7 +193,7 @@ impl PyCache { #[inline] fn is_empty(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().is_empty()) } @@ -203,7 +203,7 @@ impl PyCache { fn is_full(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.current_size() >= shared.maxsize()) } @@ -282,7 +282,7 @@ impl PyCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -305,7 +305,7 @@ impl PyCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); match policy.get(py, &key, inner.shared())? { Some(x) => Ok(x.value().clone_ref(py)), @@ -337,7 +337,7 @@ impl PyCache { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -386,7 +386,7 @@ impl PyCache { let shared = inner.shared(); { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -396,7 +396,7 @@ impl PyCache { // `factory` is Python code: a GC pass inside it would deadlock on `__traverse__` let default_object = factory.call0(py)?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -457,7 +457,7 @@ impl PyCache { /// NOTE: `Cache` always raises `NotImplementedError` because has neither policy nor algorithm to evict items. fn popitem(&self, py: pyo3::Python) -> pyo3::PyResult<(alias::PyObject, alias::PyObject)> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); let handle = policy.evict(inner.shared())?; drop(policy); @@ -481,7 +481,7 @@ impl PyCache { #[inline] fn shrink_to_fit(&self, py: pyo3::Python) -> pyo3::PyResult<()> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.shrink_to_fit(inner.shared()); Ok(()) } @@ -491,7 +491,7 @@ impl PyCache { fn clear(&self, py: pyo3::Python, reuse: bool) -> pyo3::PyResult<()> { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(shared); @@ -514,7 +514,7 @@ impl PyCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy.py_eq( py, @@ -536,7 +536,7 @@ impl PyCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy .py_eq( @@ -551,7 +551,7 @@ impl PyCache { fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); let result = PyCacheItems { cache: slf.clone().unbind(), @@ -566,7 +566,7 @@ impl PyCache { fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); let result = PyCacheValues { cache: slf.clone().unbind(), @@ -580,7 +580,7 @@ impl PyCache { fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); let result = PyCacheKeys { cache: slf.clone().unbind(), @@ -623,7 +623,7 @@ impl PyCache { fn __repr__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult { let inner = slf.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); let iter = unsafe { policy @@ -668,7 +668,7 @@ impl PyCache { fn __clear__(&self, py: pyo3::Python) { if let Ok(inner) = self.0.get() { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(inner.shared()); } else { // object is not initialized, so there's nothing to clear diff --git a/src/pyclasses/fifocache.rs b/src/pyclasses/fifocache.rs index 769ae0c..c5cfda2 100644 --- a/src/pyclasses/fifocache.rs +++ b/src/pyclasses/fifocache.rs @@ -125,7 +125,7 @@ impl PyFIFOCache { #[inline] fn current_size(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - Ok(inner.policy_py_attached(py).current_size()) + Ok(inner.policy(py).current_size()) } #[inline] @@ -145,7 +145,7 @@ impl PyFIFOCache { #[inline] fn capacity(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().capacity().min(policy.entries().capacity())) } @@ -154,7 +154,7 @@ impl PyFIFOCache { #[inline] fn __len__(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); debug_assert!(policy.table().len() == policy.entries().len()); Ok(policy.table().len()) @@ -167,7 +167,7 @@ impl PyFIFOCache { let Ok(inner) = self.0.get() else { return FIXED_SIZE; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); let table_cap = policy.table().capacity() * size_of::(); let vecdeque_cap = policy.entries().capacity() * size_of::(); @@ -179,7 +179,7 @@ impl PyFIFOCache { let Ok(inner) = self.0.get() else { return false; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); !policy.table().is_empty() } @@ -201,7 +201,7 @@ impl PyFIFOCache { #[inline] fn is_empty(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().is_empty()) } @@ -211,7 +211,7 @@ impl PyFIFOCache { fn is_full(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.current_size() >= shared.maxsize()) } @@ -287,7 +287,7 @@ impl PyFIFOCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -310,7 +310,7 @@ impl PyFIFOCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); match policy.get(py, &key, inner.shared())? { Some(x) => Ok(x.value().clone_ref(py)), @@ -342,7 +342,7 @@ impl PyFIFOCache { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -391,7 +391,7 @@ impl PyFIFOCache { let shared = inner.shared(); { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -401,7 +401,7 @@ impl PyFIFOCache { // `factory` is Python code: a GC pass inside it would deadlock on `__traverse__` let default_object = factory.call0(py)?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -460,7 +460,7 @@ impl PyFIFOCache { /// Remove and return a (key, value) pair as a 2-tuple. fn popitem(&self, py: pyo3::Python) -> pyo3::PyResult<(alias::PyObject, alias::PyObject)> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); let handle = policy.evict(inner.shared())?; drop(policy); @@ -484,7 +484,7 @@ impl PyFIFOCache { #[inline] fn shrink_to_fit(&self, py: pyo3::Python) -> pyo3::PyResult<()> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.shrink_to_fit(inner.shared()); Ok(()) } @@ -494,7 +494,7 @@ impl PyFIFOCache { fn clear(&self, py: pyo3::Python, reuse: bool) -> pyo3::PyResult<()> { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(shared); @@ -516,7 +516,7 @@ impl PyFIFOCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy.py_eq( py, @@ -538,7 +538,7 @@ impl PyFIFOCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy .py_eq( @@ -553,7 +553,7 @@ impl PyFIFOCache { fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] let result = PyFIFOCacheItems { @@ -567,7 +567,7 @@ impl PyFIFOCache { fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] let result = PyFIFOCacheValues { @@ -581,7 +581,7 @@ impl PyFIFOCache { fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] let result = PyFIFOCacheKeys { @@ -624,7 +624,7 @@ impl PyFIFOCache { fn __repr__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult { let inner = slf.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); let iter = policy.entries().iter().map(|handle| { ( @@ -650,7 +650,7 @@ impl PyFIFOCache { mut n: pyo3::ffi::Py_ssize_t, ) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); if n < 0 { n += policy.entries().len() as isize; @@ -667,7 +667,7 @@ impl PyFIFOCache { fn last(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); match policy.entries().back() { Some(handle) => Ok(handle.key().as_ref().clone_ref(py)), None => Err(new_py_error!(PyIndexError, "`n` out of range")), @@ -695,7 +695,7 @@ impl PyFIFOCache { // object is not initialized, so there's nothing to clear return; }; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(inner.shared()); } } diff --git a/src/pyclasses/lfucache.rs b/src/pyclasses/lfucache.rs index 8fb36c9..34cc13e 100644 --- a/src/pyclasses/lfucache.rs +++ b/src/pyclasses/lfucache.rs @@ -141,7 +141,7 @@ impl PyLFUCache { #[inline] fn current_size(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - Ok(inner.policy_py_attached(py).current_size()) + Ok(inner.policy(py).current_size()) } #[inline] @@ -161,7 +161,7 @@ impl PyLFUCache { #[inline] fn capacity(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().capacity()) } @@ -170,7 +170,7 @@ impl PyLFUCache { #[inline] fn __len__(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); debug_assert!(policy.table().len() == policy.heap().len()); Ok(policy.table().len()) @@ -183,7 +183,7 @@ impl PyLFUCache { let Ok(inner) = self.0.get() else { return FIXED_SIZE; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); let table_cap = policy.table().capacity() * 8; let list_cap = policy.heap().len() * size_of::(); @@ -196,7 +196,7 @@ impl PyLFUCache { let Ok(inner) = self.0.get() else { return false; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); !policy.table().is_empty() } @@ -218,7 +218,7 @@ impl PyLFUCache { #[inline] fn is_empty(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().is_empty()) } @@ -228,7 +228,7 @@ impl PyLFUCache { fn is_full(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.current_size() >= shared.maxsize()) } @@ -305,7 +305,7 @@ impl PyLFUCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -328,7 +328,7 @@ impl PyLFUCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); match policy.get(py, &key, inner.shared())? { Some(x) => Ok(x.value().clone_ref(py)), @@ -360,7 +360,7 @@ impl PyLFUCache { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -410,7 +410,7 @@ impl PyLFUCache { let shared = inner.shared(); { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -420,7 +420,7 @@ impl PyLFUCache { // `factory` is Python code: a GC pass inside it would deadlock on `__traverse__` let default_object = factory.call0(py)?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -480,7 +480,7 @@ impl PyLFUCache { /// Remove and return a (key, value) pair as a 2-tuple. fn popitem(&self, py: pyo3::Python) -> pyo3::PyResult<(alias::PyObject, alias::PyObject)> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); let handle = policy.evict(inner.shared())?; drop(policy); @@ -503,7 +503,7 @@ impl PyLFUCache { #[inline] fn shrink_to_fit(&self, py: pyo3::Python) -> pyo3::PyResult<()> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.shrink_to_fit(inner.shared()); Ok(()) } @@ -513,7 +513,7 @@ impl PyLFUCache { fn clear(&self, py: pyo3::Python, reuse: bool) -> pyo3::PyResult<()> { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(shared); @@ -535,7 +535,7 @@ impl PyLFUCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy.py_eq( py, @@ -557,7 +557,7 @@ impl PyLFUCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy .py_eq( @@ -572,7 +572,7 @@ impl PyLFUCache { fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let gv = inner.shared().generation_version(); let iter = policy.iter(gv); @@ -587,7 +587,7 @@ impl PyLFUCache { fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let gv = inner.shared().generation_version(); let iter = policy.iter(gv); @@ -602,7 +602,7 @@ impl PyLFUCache { fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let gv = inner.shared().generation_version(); let iter = policy.iter(gv); @@ -624,7 +624,7 @@ impl PyLFUCache { slf: pyo3::Bound<'_, Self>, ) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let gv = inner.shared().generation_version(); let iter = policy.iter(gv); @@ -664,7 +664,7 @@ impl PyLFUCache { fn __repr__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult { let inner = slf.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // We cannot use heap.iter here, because it requires re-sorting // and this can lead to intrupt iterators. @@ -702,7 +702,7 @@ impl PyLFUCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); if let Some(x) = policy.peek(py, &key)? { return Ok(x.value().clone_ref(py)); @@ -724,7 +724,7 @@ impl PyLFUCache { mut n: pyo3::ffi::Py_ssize_t, ) -> pyo3::PyResult { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if n < 0 { n += policy.table().len() as isize; @@ -762,7 +762,7 @@ impl PyLFUCache { // object is not initialized, so there's nothing to clear return; }; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(inner.shared()); } } diff --git a/src/pyclasses/lrucache.rs b/src/pyclasses/lrucache.rs index 3b0b4d9..9e54532 100644 --- a/src/pyclasses/lrucache.rs +++ b/src/pyclasses/lrucache.rs @@ -151,7 +151,7 @@ impl PyLRUCache { #[inline] fn current_size(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - Ok(inner.policy_py_attached(py).current_size()) + Ok(inner.policy(py).current_size()) } #[inline] @@ -171,7 +171,7 @@ impl PyLRUCache { #[inline] fn capacity(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().capacity().max(policy.list().capacity())) } @@ -180,7 +180,7 @@ impl PyLRUCache { #[inline] fn __len__(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); debug_assert!(policy.table().len() == policy.list().len()); Ok(policy.table().len()) @@ -193,7 +193,7 @@ impl PyLRUCache { let Ok(inner) = self.0.get() else { return FIXED_SIZE; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); let table_cap = policy.table().capacity() * 8; let list_cap = policy.list().len() * size_of::(); @@ -206,7 +206,7 @@ impl PyLRUCache { let Ok(inner) = self.0.get() else { return false; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); !policy.table().is_empty() } @@ -228,7 +228,7 @@ impl PyLRUCache { #[inline] fn is_empty(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().is_empty()) } @@ -238,7 +238,7 @@ impl PyLRUCache { fn is_full(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.current_size() >= shared.maxsize()) } @@ -314,7 +314,7 @@ impl PyLRUCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -337,7 +337,7 @@ impl PyLRUCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); match policy.get(py, &key, inner.shared())? { Some(x) => Ok(x.value().clone_ref(py)), @@ -369,7 +369,7 @@ impl PyLRUCache { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -418,7 +418,7 @@ impl PyLRUCache { let shared = inner.shared(); { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -428,7 +428,7 @@ impl PyLRUCache { // `factory` is Python code: a GC pass inside it would deadlock on `__traverse__` let default_object = factory.call0(py)?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -487,7 +487,7 @@ impl PyLRUCache { /// Remove and return a (key, value) pair as a 2-tuple. fn popitem(&self, py: pyo3::Python) -> pyo3::PyResult<(alias::PyObject, alias::PyObject)> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); let handle = policy.evict(inner.shared())?; drop(policy); @@ -511,7 +511,7 @@ impl PyLRUCache { #[inline] fn shrink_to_fit(&self, py: pyo3::Python) -> pyo3::PyResult<()> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.shrink_to_fit(inner.shared()); Ok(()) } @@ -521,7 +521,7 @@ impl PyLRUCache { fn clear(&self, py: pyo3::Python, reuse: bool) -> pyo3::PyResult<()> { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(shared); @@ -543,7 +543,7 @@ impl PyLRUCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy.py_eq( py, @@ -565,7 +565,7 @@ impl PyLRUCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy .py_eq( @@ -579,7 +579,7 @@ impl PyLRUCache { fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] let result = PyLRUCacheItems { @@ -592,7 +592,7 @@ impl PyLRUCache { fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] let result = PyLRUCacheValues { @@ -605,7 +605,7 @@ impl PyLRUCache { fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] let result = PyLRUCacheKeys { @@ -648,7 +648,7 @@ impl PyLRUCache { fn __repr__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult { let inner = slf.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); let iter = unsafe { policy.list().iter().map(|cursor| { @@ -680,7 +680,7 @@ impl PyLRUCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); if let Some(x) = policy.peek(py, &key)? { return Ok(x.value().clone_ref(py)); @@ -698,7 +698,7 @@ impl PyLRUCache { #[inline] fn least_recently_used(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); match policy.list().cursor_front() { Some(cursor) => Ok(unsafe { cursor.element().key().clone_ref(py).into() }), @@ -709,7 +709,7 @@ impl PyLRUCache { #[inline] fn most_recently_used(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); match policy.list().cursor_back() { Some(cursor) => Ok(unsafe { cursor.element().key().clone_ref(py).into() }), @@ -740,7 +740,7 @@ impl PyLRUCache { // object is not initialized, so there's nothing to clear return; }; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(inner.shared()); } } diff --git a/src/pyclasses/rrcache.rs b/src/pyclasses/rrcache.rs index 14ac518..fb488fb 100644 --- a/src/pyclasses/rrcache.rs +++ b/src/pyclasses/rrcache.rs @@ -123,7 +123,7 @@ impl PyRRCache { #[inline] fn current_size(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - Ok(inner.policy_py_attached(py).current_size()) + Ok(inner.policy(py).current_size()) } #[inline] @@ -143,7 +143,7 @@ impl PyRRCache { #[inline] fn capacity(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().capacity()) } @@ -152,7 +152,7 @@ impl PyRRCache { #[inline] fn __len__(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().len()) } @@ -164,7 +164,7 @@ impl PyRRCache { let Ok(inner) = self.0.get() else { return FIXED_SIZE; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); FIXED_SIZE + policy.table().capacity() * size_of::() } @@ -174,7 +174,7 @@ impl PyRRCache { let Ok(inner) = self.0.get() else { return false; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); !policy.table().is_empty() } @@ -196,7 +196,7 @@ impl PyRRCache { #[inline] fn is_empty(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().is_empty()) } @@ -206,7 +206,7 @@ impl PyRRCache { fn is_full(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.current_size() >= shared.maxsize()) } @@ -285,7 +285,7 @@ impl PyRRCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -308,7 +308,7 @@ impl PyRRCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); match policy.get(py, &key, inner.shared())? { Some(x) => Ok(x.value().clone_ref(py)), @@ -340,7 +340,7 @@ impl PyRRCache { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -389,7 +389,7 @@ impl PyRRCache { let shared = inner.shared(); { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -399,7 +399,7 @@ impl PyRRCache { // `factory` is Python code: a GC pass inside it would deadlock on `__traverse__` let default_object = factory.call0(py)?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -460,7 +460,7 @@ impl PyRRCache { /// NOTE: `Cache` always raises `NotImplementedError` because has neither policy nor algorithm to evict items. fn popitem(&self, py: pyo3::Python) -> pyo3::PyResult<(alias::PyObject, alias::PyObject)> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); let handle = policy.evict(inner.shared())?; drop(policy); @@ -484,7 +484,7 @@ impl PyRRCache { #[inline] fn shrink_to_fit(&self, py: pyo3::Python) -> pyo3::PyResult<()> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.shrink_to_fit(inner.shared()); Ok(()) } @@ -494,7 +494,7 @@ impl PyRRCache { fn clear(&self, py: pyo3::Python, reuse: bool) -> pyo3::PyResult<()> { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(shared); @@ -516,7 +516,7 @@ impl PyRRCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy.py_eq( py, @@ -538,7 +538,7 @@ impl PyRRCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy .py_eq( @@ -553,7 +553,7 @@ impl PyRRCache { fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] let result = PyRRCacheItems { @@ -567,7 +567,7 @@ impl PyRRCache { fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] let result = PyRRCacheValues { @@ -581,7 +581,7 @@ impl PyRRCache { fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] let result = PyRRCacheKeys { @@ -624,7 +624,7 @@ impl PyRRCache { fn __repr__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult { let inner = slf.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); let iter = unsafe { policy @@ -652,7 +652,7 @@ impl PyRRCache { #[inline] fn random_key(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); let bucket = policy .random_bucket() @@ -685,7 +685,7 @@ impl PyRRCache { // object is not initialized, so there's nothing to clear return; }; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(inner.shared()); } } diff --git a/src/pyclasses/ttlcache.rs b/src/pyclasses/ttlcache.rs index 1ec696e..006990e 100644 --- a/src/pyclasses/ttlcache.rs +++ b/src/pyclasses/ttlcache.rs @@ -104,7 +104,7 @@ impl PyTTLCache { #[inline] fn current_size(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.expire(inner.shared().generation_version()); Ok(policy.current_size()) } @@ -113,7 +113,7 @@ impl PyTTLCache { fn remaining_size(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.expire(inner.shared().generation_version()); } Ok(inner.remaining_size(py)) @@ -137,7 +137,7 @@ impl PyTTLCache { #[inline] fn capacity(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().capacity().min(policy.entries().capacity())) } @@ -146,7 +146,7 @@ impl PyTTLCache { #[inline] fn __len__(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); debug_assert!(policy.table().len() == policy.entries().len()); Ok(policy.table().len()) @@ -159,7 +159,7 @@ impl PyTTLCache { let Ok(inner) = self.0.get() else { return FIXED_SIZE; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); let table_cap = policy.table().capacity() * size_of::(); let vecdeque_cap = policy.entries().capacity() * size_of::(); @@ -172,7 +172,7 @@ impl PyTTLCache { let Ok(inner) = self.0.get() else { return false; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); !policy.table().is_empty() } @@ -194,7 +194,7 @@ impl PyTTLCache { #[inline] fn is_empty(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().is_empty()) } @@ -204,7 +204,7 @@ impl PyTTLCache { fn is_full(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.current_size() >= shared.maxsize()) } @@ -290,7 +290,7 @@ impl PyTTLCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -313,7 +313,7 @@ impl PyTTLCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); match policy.get(py, &key, inner.shared())? { Some(x) => Ok(x.value().clone_ref(py)), @@ -345,7 +345,7 @@ impl PyTTLCache { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -395,7 +395,7 @@ impl PyTTLCache { let shared = inner.shared(); { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -405,7 +405,7 @@ impl PyTTLCache { // `factory` is Python code: a GC pass inside it would deadlock on `__traverse__` let default_object = factory.call0(py)?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -465,7 +465,7 @@ impl PyTTLCache { /// Remove and return a (key, value) pair as a 2-tuple. fn popitem(&self, py: pyo3::Python) -> pyo3::PyResult<(alias::PyObject, alias::PyObject)> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); let handle = policy.evict(inner.shared())?; drop(policy); @@ -489,7 +489,7 @@ impl PyTTLCache { #[inline] fn shrink_to_fit(&self, py: pyo3::Python) -> pyo3::PyResult<()> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.shrink_to_fit(inner.shared()); Ok(()) } @@ -499,7 +499,7 @@ impl PyTTLCache { fn clear(&self, py: pyo3::Python, reuse: bool) -> pyo3::PyResult<()> { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(shared); @@ -521,7 +521,7 @@ impl PyTTLCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy.py_eq( py, @@ -543,7 +543,7 @@ impl PyTTLCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy .py_eq( @@ -558,7 +558,7 @@ impl PyTTLCache { fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let iter = policy.iter(inner.shared()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] @@ -573,7 +573,7 @@ impl PyTTLCache { fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let iter = policy.iter(inner.shared()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] @@ -588,7 +588,7 @@ impl PyTTLCache { fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let iter = policy.iter(inner.shared()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] @@ -633,7 +633,7 @@ impl PyTTLCache { fn __repr__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult { let inner = slf.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); let now = std::time::SystemTime::now(); let iter = policy @@ -662,7 +662,7 @@ impl PyTTLCache { fn expire(&self, py: pyo3::Python, reuse: bool) -> pyo3::PyResult<()> { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.expire(shared.generation_version()); @@ -679,7 +679,7 @@ impl PyTTLCache { mut n: pyo3::ffi::Py_ssize_t, ) -> pyo3::PyResult { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.expire(inner.shared().generation_version()); @@ -698,7 +698,7 @@ impl PyTTLCache { fn last(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.expire(inner.shared().generation_version()); @@ -718,7 +718,7 @@ impl PyTTLCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { let dur = x @@ -775,7 +775,7 @@ impl PyTTLCache { py: pyo3::Python, ) -> pyo3::PyResult<(alias::PyObject, alias::PyObject, f64)> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); let handle = policy.evict(inner.shared())?; drop(policy); @@ -796,7 +796,7 @@ impl PyTTLCache { // Create the iterator and read the generation under one lock, or a mutation in // between would make stale positions look current. - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let iter = policy.iter(inner.shared()); // SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`] @@ -829,7 +829,7 @@ impl PyTTLCache { // object is not initialized, so there's nothing to clear return; }; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(inner.shared()); } } diff --git a/src/pyclasses/vttlcache.rs b/src/pyclasses/vttlcache.rs index 6ac2a9a..800c68b 100644 --- a/src/pyclasses/vttlcache.rs +++ b/src/pyclasses/vttlcache.rs @@ -98,7 +98,7 @@ impl PyVTTLCache { #[inline] fn current_size(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.expire(inner.shared().generation_version()); Ok(policy.current_size()) } @@ -107,7 +107,7 @@ impl PyVTTLCache { fn remaining_size(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.expire(inner.shared().generation_version()); } @@ -125,7 +125,7 @@ impl PyVTTLCache { #[inline] fn capacity(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().capacity()) } @@ -134,7 +134,7 @@ impl PyVTTLCache { #[inline] fn __len__(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); debug_assert!(policy.table().len() == policy.heap().len()); Ok(policy.table().len()) @@ -147,7 +147,7 @@ impl PyVTTLCache { let Ok(inner) = self.0.get() else { return FIXED_SIZE; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); let table_cap = policy.table().capacity() * 8; let list_cap = policy.heap().len() * size_of::(); @@ -160,7 +160,7 @@ impl PyVTTLCache { let Ok(inner) = self.0.get() else { return false; }; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); !policy.table().is_empty() } @@ -182,7 +182,7 @@ impl PyVTTLCache { #[inline] fn is_empty(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.table().is_empty()) } @@ -192,7 +192,7 @@ impl PyVTTLCache { fn is_full(&self, py: pyo3::Python) -> pyo3::PyResult { let inner = self.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(py); + let policy = inner.policy(py); Ok(policy.current_size() >= shared.maxsize()) } @@ -273,7 +273,7 @@ impl PyVTTLCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -296,7 +296,7 @@ impl PyVTTLCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); match policy.get(py, &key, inner.shared())? { Some(x) => Ok(x.value().clone_ref(py)), @@ -326,7 +326,7 @@ impl PyVTTLCache { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -372,7 +372,7 @@ impl PyVTTLCache { let shared = inner.shared(); { - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -382,7 +382,7 @@ impl PyVTTLCache { // `factory` is Python code: a GC pass inside it would deadlock on `__traverse__` let default_object = factory.call0(py)?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(x) = policy.get(py, &key, inner.shared())? { return Ok(x.value().clone_ref(py)); @@ -439,7 +439,7 @@ impl PyVTTLCache { /// Remove and return a (key, value) pair as a 2-tuple. fn popitem(&self, py: pyo3::Python) -> pyo3::PyResult<(alias::PyObject, alias::PyObject)> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); let handle = policy.evict(inner.shared())?; drop(policy); @@ -463,7 +463,7 @@ impl PyVTTLCache { #[inline] fn shrink_to_fit(&self, py: pyo3::Python) -> pyo3::PyResult<()> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.shrink_to_fit(inner.shared()); Ok(()) } @@ -473,7 +473,7 @@ impl PyVTTLCache { fn clear(&self, py: pyo3::Python, reuse: bool) -> pyo3::PyResult<()> { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(shared); @@ -495,7 +495,7 @@ impl PyVTTLCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy.py_eq( py, @@ -517,7 +517,7 @@ impl PyVTTLCache { let self_inner = slf.0.get()?; let other_inner = other.0.get()?; - let (self_policy, other_policy) = self_inner.policy_pair_py_attached(other_inner, py); + let (self_policy, other_policy) = self_inner.policy_ordered_pair(other_inner, py); self_policy .py_eq( @@ -531,7 +531,7 @@ impl PyVTTLCache { fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let gv = inner.shared().generation_version(); let iter = policy.iter(gv); @@ -546,7 +546,7 @@ impl PyVTTLCache { fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let gv = inner.shared().generation_version(); let iter = policy.iter(gv); @@ -561,7 +561,7 @@ impl PyVTTLCache { fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let gv = inner.shared().generation_version(); let iter = policy.iter(gv); @@ -607,7 +607,7 @@ impl PyVTTLCache { fn __repr__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult { let inner = slf.0.get()?; let shared = inner.shared(); - let policy = inner.policy_py_attached(slf.py()); + let policy = inner.policy(slf.py()); let now = std::time::SystemTime::now(); @@ -642,7 +642,7 @@ impl PyVTTLCache { fn expire(&self, py: pyo3::Python, reuse: bool) -> pyo3::PyResult<()> { let inner = self.0.get()?; let shared = inner.shared(); - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.expire(shared.generation_version()); @@ -662,7 +662,7 @@ impl PyVTTLCache { let key = utils::PrecomputedHashObject::new(py, key)?; let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); if let Some(handle) = policy.get(py, &key, inner.shared())? { let dur = match handle.expires_at() { @@ -733,7 +733,7 @@ impl PyVTTLCache { py: pyo3::Python, ) -> pyo3::PyResult<(alias::PyObject, alias::PyObject, alias::PyObject)> { let inner = self.0.get()?; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); let handle = policy.evict(inner.shared())?; drop(policy); @@ -758,7 +758,7 @@ impl PyVTTLCache { slf: pyo3::Bound<'_, Self>, ) -> pyo3::PyResult> { let inner = slf.get().0.get()?; - let mut policy = inner.policy_py_attached(slf.py()); + let mut policy = inner.policy(slf.py()); let gv = inner.shared().generation_version(); let iter = policy.iter(gv); @@ -794,7 +794,7 @@ impl PyVTTLCache { // object is not initialized, so there's nothing to clear return; }; - let mut policy = inner.policy_py_attached(py); + let mut policy = inner.policy(py); policy.clear(inner.shared()); } } diff --git a/src/typeref.rs b/src/typeref.rs index e671a43..04a648c 100644 --- a/src/typeref.rs +++ b/src/typeref.rs @@ -1,8 +1,8 @@ -/// Raw pointer to the CPython `dict`, cached at initialization. pub static mut STD_DICT_TYPE: *mut pyo3::ffi::PyTypeObject = std::ptr::null_mut(); - -/// Raw pointer to the CPython `tuple`, cached at initialization. pub static mut STD_TUPLE_TYPE: *mut pyo3::ffi::PyTypeObject = std::ptr::null_mut(); +pub static mut STD_LIST_TYPE: *mut pyo3::ffi::PyTypeObject = std::ptr::null_mut(); +pub static mut STD_SET_TYPE: *mut pyo3::ffi::PyTypeObject = std::ptr::null_mut(); +pub static mut STD_FROZENSET_TYPE: *mut pyo3::ffi::PyTypeObject = std::ptr::null_mut(); #[inline(never)] unsafe fn get_type_object_for( @@ -17,6 +17,9 @@ fn _initialize_typeref(py: pyo3::Python) { unsafe { STD_DICT_TYPE = get_type_object_for::(py); STD_TUPLE_TYPE = get_type_object_for::(py); + STD_LIST_TYPE = get_type_object_for::(py); + STD_SET_TYPE = get_type_object_for::(py); + STD_FROZENSET_TYPE = get_type_object_for::(py); } } diff --git a/tests/mixins.py b/tests/mixins.py index 94bc495..a9cc5fe 100644 --- a/tests/mixins.py +++ b/tests/mixins.py @@ -443,6 +443,7 @@ def pairs(): with pytest.raises(ValueError, match="stop"): cache.update(pairs()) + assert cache.get("a") == 1 assert cache.get("b") == 2