Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/internal/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,17 @@ impl<T> Cursor<T> {
unsafe { list.push_front_node(links) };
}

/// Returns `true` if the node this cursor points to is the last node of `list`.
///
/// # Safety
///
/// Same contract as [`move_to_front`](Self::move_to_front).
#[inline]
pub unsafe fn is_back(&self, list: &LinkedList<T>) -> bool {
// SAFETY: the sentinel is always valid; caller guarantees the node is linked.
unsafe { (*list.sentinel.as_ptr()).prev == self.links().as_ptr() }
}

/// Moves the node this cursor points to the back of `list`.
///
/// # Safety
Expand Down
1 change: 1 addition & 0 deletions src/policies/fifopolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ impl PolicyExt for FIFOPolicy {
&mut self,
py: pyo3::Python,
key: &<Self::Handle as HandleExt>::Key,
_shared: &Self::Shared,
) -> pyo3::PyResult<Option<&Self::Handle>> {
let eq = |index: &usize| get_handle!(&self, *index).key().py_eq(py, key);
match self.table.get(key.hash(), eq)? {
Expand Down
1 change: 1 addition & 0 deletions src/policies/lfupolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ impl PolicyExt for LFUPolicy {
&mut self,
py: pyo3::Python,
key: &<Self::Handle as HandleExt>::Key,
_shared: &Self::Shared,
) -> pyo3::PyResult<Option<&Self::Handle>> {
let cursor = self
.table
Expand Down
9 changes: 8 additions & 1 deletion src/policies/lrupolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ impl PolicyExt for LRUPolicy {
&mut self,
py: pyo3::Python,
key: &<Self::Handle as HandleExt>::Key,
shared: &Self::Shared,
) -> pyo3::PyResult<Option<&Self::Handle>> {
unsafe {
let bucket = self
Expand All @@ -177,7 +178,13 @@ impl PolicyExt for LRUPolicy {

match bucket {
Some(cursor) => {
cursor.move_to_back(&mut self.list);
// moving the entry is a mutation, so it bumps the generation like
// any other; a hit on the most recent entry moves nothing
if !cursor.is_back(&self.list) {
shared.generation_version().increment();
cursor.move_to_back(&mut self.list);
}

Ok(Some(cursor.element()))
}
None => Ok(None),
Expand Down
1 change: 1 addition & 0 deletions src/policies/nopolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ impl PolicyExt for NoPolicy {
&mut self,
py: pyo3::Python,
key: &<Self::Handle as HandleExt>::Key,
_shared: &Self::Shared,
) -> pyo3::PyResult<Option<&Self::Handle>> {
let bucket = self.table.find(key.hash(), |x| key.py_eq(py, x.key()))?;
Ok(bucket.map(|x| unsafe { x.as_ref() }))
Expand Down
1 change: 1 addition & 0 deletions src/policies/rrpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ impl PolicyExt for RRPolicy {
&mut self,
py: pyo3::Python,
key: &<Self::Handle as HandleExt>::Key,
_shared: &Self::Shared,
) -> pyo3::PyResult<Option<&Self::Handle>> {
let bucket = self.table.find(key.hash(), |x| key.py_eq(py, x.key()))?;
Ok(bucket.map(|x| unsafe { x.as_ref() }))
Expand Down
1 change: 1 addition & 0 deletions src/policies/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub trait PolicyExt: Sized {
&mut self,
py: pyo3::Python,
key: &<Self::Handle as HandleExt>::Key,
shared: &Self::Shared,
) -> pyo3::PyResult<Option<&Self::Handle>>;

/// Returns a [`PolicyEntry`] for the slot at `hash` / `eq`.
Expand Down
1 change: 1 addition & 0 deletions src/policies/ttlpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ impl PolicyExt for TTLPolicy {
&mut self,
py: pyo3::Python,
key: &<Self::Handle as HandleExt>::Key,
_shared: &Self::Shared,
) -> pyo3::PyResult<Option<&Self::Handle>> {
let eq = |index: &usize| get_handle!(&self, *index).key().py_eq(py, key);

Expand Down
1 change: 1 addition & 0 deletions src/policies/vttlpolicy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ impl PolicyExt for VTTLPolicy {
&mut self,
py: pyo3::Python,
key: &<Self::Handle as HandleExt>::Key,
_shared: &Self::Shared,
) -> pyo3::PyResult<Option<&Self::Handle>> {
let cursor = self
.table
Expand Down
2 changes: 1 addition & 1 deletion src/policies/wrapped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<P: PolicyExt> Wrapped<P> {
) -> pyo3::PyResult<bool> {
let mut lock = self.inner.lock();

let handle = lock.get(py, key)?;
let handle = lock.get(py, key, &self.shared)?;
Ok(handle.is_some())
}

Expand Down
10 changes: 5 additions & 5 deletions src/pyclasses/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl PyCache {
let inner = self.0.get();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand All @@ -302,7 +302,7 @@ impl PyCache {
let inner = self.0.get();
let mut policy = inner.policy();

match policy.get(py, &key)? {
match policy.get(py, &key, inner.shared())? {
Some(x) => Ok(x.value().clone_ref(py)),
None => Err(new_py_error!(
PyKeyError,
Expand Down Expand Up @@ -334,7 +334,7 @@ impl PyCache {
let shared = inner.shared();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down Expand Up @@ -383,7 +383,7 @@ impl PyCache {
{
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}
}
Expand All @@ -393,7 +393,7 @@ impl PyCache {

let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down
10 changes: 5 additions & 5 deletions src/pyclasses/fifocache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl PyFIFOCache {
let inner = self.0.get();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand All @@ -308,7 +308,7 @@ impl PyFIFOCache {
let inner = self.0.get();
let mut policy = inner.policy();

match policy.get(py, &key)? {
match policy.get(py, &key, inner.shared())? {
Some(x) => Ok(x.value().clone_ref(py)),
None => Err(new_py_error!(
PyKeyError,
Expand Down Expand Up @@ -340,7 +340,7 @@ impl PyFIFOCache {
let shared = inner.shared();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down Expand Up @@ -389,7 +389,7 @@ impl PyFIFOCache {
{
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}
}
Expand All @@ -399,7 +399,7 @@ impl PyFIFOCache {

let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down
10 changes: 5 additions & 5 deletions src/pyclasses/lfucache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl PyLFUCache {
let inner = self.0.get();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand All @@ -326,7 +326,7 @@ impl PyLFUCache {
let inner = self.0.get();
let mut policy = inner.policy();

match policy.get(py, &key)? {
match policy.get(py, &key, inner.shared())? {
Some(x) => Ok(x.value().clone_ref(py)),
None => Err(new_py_error!(
PyKeyError,
Expand Down Expand Up @@ -358,7 +358,7 @@ impl PyLFUCache {
let shared = inner.shared();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down Expand Up @@ -408,7 +408,7 @@ impl PyLFUCache {
{
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}
}
Expand All @@ -418,7 +418,7 @@ impl PyLFUCache {

let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down
10 changes: 5 additions & 5 deletions src/pyclasses/lrucache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl PyLRUCache {
let inner = self.0.get();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand All @@ -335,7 +335,7 @@ impl PyLRUCache {
let inner = self.0.get();
let mut policy = inner.policy();

match policy.get(py, &key)? {
match policy.get(py, &key, inner.shared())? {
Some(x) => Ok(x.value().clone_ref(py)),
None => Err(new_py_error!(
PyKeyError,
Expand Down Expand Up @@ -367,7 +367,7 @@ impl PyLRUCache {
let shared = inner.shared();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down Expand Up @@ -416,7 +416,7 @@ impl PyLRUCache {
{
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}
}
Expand All @@ -426,7 +426,7 @@ impl PyLRUCache {

let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down
10 changes: 5 additions & 5 deletions src/pyclasses/rrcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl PyRRCache {
let inner = self.0.get();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand All @@ -306,7 +306,7 @@ impl PyRRCache {
let inner = self.0.get();
let mut policy = inner.policy();

match policy.get(py, &key)? {
match policy.get(py, &key, inner.shared())? {
Some(x) => Ok(x.value().clone_ref(py)),
None => Err(new_py_error!(
PyKeyError,
Expand Down Expand Up @@ -338,7 +338,7 @@ impl PyRRCache {
let shared = inner.shared();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down Expand Up @@ -387,7 +387,7 @@ impl PyRRCache {
{
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}
}
Expand All @@ -397,7 +397,7 @@ impl PyRRCache {

let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down
12 changes: 6 additions & 6 deletions src/pyclasses/ttlcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl PyTTLCache {
let inner = self.0.get();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand All @@ -311,7 +311,7 @@ impl PyTTLCache {
let inner = self.0.get();
let mut policy = inner.policy();

match policy.get(py, &key)? {
match policy.get(py, &key, inner.shared())? {
Some(x) => Ok(x.value().clone_ref(py)),
None => Err(new_py_error!(
PyKeyError,
Expand Down Expand Up @@ -343,7 +343,7 @@ impl PyTTLCache {
let shared = inner.shared();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down Expand Up @@ -393,7 +393,7 @@ impl PyTTLCache {
{
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}
}
Expand All @@ -403,7 +403,7 @@ impl PyTTLCache {

let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
return Ok(x.value().clone_ref(py));
}

Expand Down Expand Up @@ -724,7 +724,7 @@ impl PyTTLCache {
let inner = self.0.get();
let mut policy = inner.policy();

if let Some(x) = policy.get(py, &key)? {
if let Some(x) = policy.get(py, &key, inner.shared())? {
let dur = x
.expires_at()
.duration_since(std::time::SystemTime::now())
Expand Down
Loading