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
30 changes: 19 additions & 11 deletions src/pyclasses/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,52 +542,55 @@ impl PyCache {
.map(|x| !x)
}

fn items(&self) -> pyo3::PyResult<pyo3::Py<PyCacheItems>> {
let inner = self.0.get();
fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyCacheItems>> {
let inner = slf.get().0.get();
let gv = inner.shared().generation_version().clone();
let initial_gv = gv.get();

let result = PyCacheItems {
cache: slf.as_any().clone().unbind(),
// SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`]
iter: parking_lot::Mutex::new(unsafe { inner.policy().table().iter() }),
gv,
initial_gv,
};

pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

fn values(&self) -> pyo3::PyResult<pyo3::Py<PyCacheValues>> {
let inner = self.0.get();
fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyCacheValues>> {
let inner = slf.get().0.get();
let gv = inner.shared().generation_version().clone();
let initial_gv = gv.get();

let result = PyCacheValues {
cache: slf.as_any().clone().unbind(),
// SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`]
iter: parking_lot::Mutex::new(unsafe { inner.policy().table().iter() }),
gv,
initial_gv,
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyCacheKeys>> {
let inner = self.0.get();
fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyCacheKeys>> {
let inner = slf.get().0.get();
let gv = inner.shared().generation_version().clone();
let initial_gv = gv.get();

let result = PyCacheKeys {
cache: slf.as_any().clone().unbind(),
// SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`]
iter: parking_lot::Mutex::new(unsafe { inner.policy().table().iter() }),
gv,
initial_gv,
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

#[inline]
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyCacheKeys>> {
self.keys()
fn __iter__(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyCacheKeys>> {
Self::keys(slf)
}

fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {
Expand Down Expand Up @@ -681,6 +684,7 @@ macro_rules! implement_iterator {
$(
implement_pyclass! {
[generic, frozen] $name as $pyname {
cache: pyo3::Py<pyo3::PyAny>,
initial_gv: u32,
gv: utils::GenerationVersion,
iter: parking_lot::Mutex<crate::hashbrown::raw::RawIter<nopolicy::Handle>>,
Expand All @@ -694,6 +698,10 @@ macro_rules! implement_iterator {
slf
}

fn __traverse__(&self, visit: pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {
visit.call(&self.cache)
}

fn __next__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult<$rt_type> {
if slf.initial_gv != slf.gv.get() {
return Err(new_py_error!(
Expand Down
30 changes: 19 additions & 11 deletions src/pyclasses/fifocache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,51 +546,54 @@ impl PyFIFOCache {
.map(|x| !x)
}

fn items(&self) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheItems>> {
let inner = self.0.get();
fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheItems>> {
let inner = slf.get().0.get();
let gv = inner.shared().generation_version().clone();
let initial_gv = gv.get();

// SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`]
let result = PyFIFOCacheItems {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(inner.policy().iter()),
gv,
initial_gv,
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

fn values(&self) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheValues>> {
let inner = self.0.get();
fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheValues>> {
let inner = slf.get().0.get();
let gv = inner.shared().generation_version().clone();
let initial_gv = gv.get();

// SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`]
let result = PyFIFOCacheValues {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(inner.policy().iter()),
gv,
initial_gv,
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheKeys>> {
let inner = self.0.get();
fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheKeys>> {
let inner = slf.get().0.get();
let gv = inner.shared().generation_version().clone();
let initial_gv = gv.get();

// SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`]
let result = PyFIFOCacheKeys {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(inner.policy().iter()),
gv,
initial_gv,
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

#[inline]
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheKeys>> {
self.keys()
fn __iter__(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyFIFOCacheKeys>> {
Self::keys(slf)
}

fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {
Expand Down Expand Up @@ -707,6 +710,7 @@ macro_rules! implement_iterator {
$(
implement_pyclass! {
[generic, frozen] $name as $pyname {
cache: pyo3::Py<pyo3::PyAny>,
initial_gv: u32,
gv: utils::GenerationVersion,
iter: parking_lot::Mutex<utils::RawVecDequeIter<fifopolicy::Handle>>,
Expand All @@ -720,6 +724,10 @@ macro_rules! implement_iterator {
slf
}

fn __traverse__(&self, visit: pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {
visit.call(&self.cache)
}

fn __next__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult<$rt_type> {
if slf.initial_gv != slf.gv.get() {
return Err(new_py_error!(
Expand Down
39 changes: 25 additions & 14 deletions src/pyclasses/lfucache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,69 +565,75 @@ impl PyLFUCache {
.map(|x| !x)
}

fn items(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheItems>> {
let inner = self.0.get();
fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyLFUCacheItems>> {
let inner = slf.get().0.get();
let mut policy = inner.policy();

let gv = inner.shared().generation_version();
let iter = policy.iter(gv);

let result = PyLFUCacheItems {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(iter),
gv: gv.clone(),
initial_gv: gv.get(),
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

fn values(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheValues>> {
let inner = self.0.get();
fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyLFUCacheValues>> {
let inner = slf.get().0.get();
let mut policy = inner.policy();

let gv = inner.shared().generation_version();
let iter = policy.iter(gv);

let result = PyLFUCacheValues {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(iter),
gv: gv.clone(),
initial_gv: gv.get(),
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheKeys>> {
let inner = self.0.get();
fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyLFUCacheKeys>> {
let inner = slf.get().0.get();
let mut policy = inner.policy();

let gv = inner.shared().generation_version();
let iter = policy.iter(gv);

let result = PyLFUCacheKeys {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(iter),
gv: gv.clone(),
initial_gv: gv.get(),
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

#[inline]
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheKeys>> {
self.keys()
fn __iter__(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyLFUCacheKeys>> {
Self::keys(slf)
}

fn items_with_frequency(&self) -> pyo3::PyResult<pyo3::Py<PyLFUCacheItemsWithFrequency>> {
let inner = self.0.get();
fn items_with_frequency(
slf: pyo3::Bound<'_, Self>,
) -> pyo3::PyResult<pyo3::Py<PyLFUCacheItemsWithFrequency>> {
let inner = slf.get().0.get();
let mut policy = inner.policy();

let gv = inner.shared().generation_version();
let iter = policy.iter(gv);

let result = PyLFUCacheItemsWithFrequency {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(iter),
gv: gv.clone(),
initial_gv: gv.get(),
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {
Expand Down Expand Up @@ -771,6 +777,7 @@ macro_rules! implement_iterator {
$(
implement_pyclass! {
[generic, frozen] $name as $pyname {
cache: pyo3::Py<pyo3::PyAny>,
initial_gv: u32,
gv: utils::GenerationVersion,
iter: parking_lot::Mutex<lazyheap::RawIter<lfupolicy::FrequencyHandle>>,
Expand All @@ -784,6 +791,10 @@ macro_rules! implement_iterator {
slf
}

fn __traverse__(&self, visit: pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {
visit.call(&self.cache)
}

fn __next__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult<$rt_type> {
if slf.initial_gv != slf.gv.get() {
return Err(new_py_error!(
Expand Down
30 changes: 19 additions & 11 deletions src/pyclasses/lrucache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,51 +573,54 @@ impl PyLRUCache {
.map(|x| !x)
}

fn items(&self) -> pyo3::PyResult<pyo3::Py<PyLRUCacheItems>> {
let inner = self.0.get();
fn items(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyLRUCacheItems>> {
let inner = slf.get().0.get();
let gv = inner.shared().generation_version().clone();
let initial_gv = gv.get();

// SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`]
let result = PyLRUCacheItems {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(unsafe { inner.policy().list().iter() }),
gv,
initial_gv,
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

fn values(&self) -> pyo3::PyResult<pyo3::Py<PyLRUCacheValues>> {
let inner = self.0.get();
fn values(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyLRUCacheValues>> {
let inner = slf.get().0.get();
let gv = inner.shared().generation_version().clone();
let initial_gv = gv.get();

// SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`]
let result = PyLRUCacheValues {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(unsafe { inner.policy().list().iter() }),
gv,
initial_gv,
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

fn keys(&self) -> pyo3::PyResult<pyo3::Py<PyLRUCacheKeys>> {
let inner = self.0.get();
fn keys(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyLRUCacheKeys>> {
let inner = slf.get().0.get();
let gv = inner.shared().generation_version().clone();
let initial_gv = gv.get();

// SAFETY: We cannot use lifetimes here, but we're tracking changes using [`GenerationVersion`]
let result = PyLRUCacheKeys {
cache: slf.as_any().clone().unbind(),
iter: parking_lot::Mutex::new(unsafe { inner.policy().list().iter() }),
gv,
initial_gv,
};
pyo3::Python::attach(|py| pyo3::Py::new(py, result))
pyo3::Py::new(slf.py(), result)
}

#[inline]
fn __iter__(&self) -> pyo3::PyResult<pyo3::Py<PyLRUCacheKeys>> {
self.keys()
fn __iter__(slf: pyo3::Bound<'_, Self>) -> pyo3::PyResult<pyo3::Py<PyLRUCacheKeys>> {
Self::keys(slf)
}

fn copy(&self, py: pyo3::Python) -> pyo3::PyResult<pyo3::Py<Self>> {
Expand Down Expand Up @@ -755,6 +758,7 @@ macro_rules! implement_iterator {
$(
implement_pyclass! {
[generic, frozen] $name as $pyname {
cache: pyo3::Py<pyo3::PyAny>,
initial_gv: u32,
gv: utils::GenerationVersion,
iter: parking_lot::Mutex<linked_list::RawIter<lrupolicy::Handle>>,
Expand All @@ -768,6 +772,10 @@ macro_rules! implement_iterator {
slf
}

fn __traverse__(&self, visit: pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {
visit.call(&self.cache)
}

fn __next__(slf: pyo3::PyRef<'_, Self>) -> pyo3::PyResult<$rt_type> {
if slf.initial_gv != slf.gv.get() {
return Err(new_py_error!(
Expand Down
Loading
Loading