Skip to content

Commit

Permalink
Change the behaviour of the iteratores
Browse files Browse the repository at this point in the history
Previously, iterators used capacity and length of the cache to know "is cache have changes?"
But now, each cache has a number called "state" that increments with each change; iterators now uses "state" number.
  • Loading branch information
awolverp committed Nov 25, 2024
1 parent 193fd31 commit 09b6eb5
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 92 deletions.
1 change: 1 addition & 0 deletions cachebox/_cachebox.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class BaseCacheImpl(typing.Generic[KT, VT]):
def __class_getitem__(*args) -> None: ...
@property
def maxsize(self) -> int: ...
def _state(self) -> int: ...
def __len__(self) -> int: ...
def __sizeof__(self) -> int: ...
def __bool__(self) -> bool: ...
Expand Down
24 changes: 15 additions & 9 deletions src/bridge/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ impl Cache {
lock.maxsize.get()
}

pub fn _state(&self) -> usize {
let lock = self.raw.lock();
lock.state.get()
}

/// Returns the number of elements in the table - len(self)
pub fn __len__(&self) -> usize {
let lock = self.raw.lock();
Expand Down Expand Up @@ -145,11 +150,11 @@ impl Cache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<cache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());
let iter = unsafe { lock.table.iter() };

let result = cache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(iter),
typ: 0,
};
Expand Down Expand Up @@ -338,7 +343,8 @@ impl Cache {
/// Shrinks the cache to fit len(self) elements.
pub fn shrink_to_fit(&self) {
let mut lock = self.raw.lock();
lock.table.shrink_to(0, |x| x.0.hash)
lock.table.shrink_to(0, |x| x.0.hash);
lock.state.change();
}

/// Updates the cache with elements from a dictionary or an iterable object of key/value pairs.
Expand Down Expand Up @@ -367,11 +373,11 @@ impl Cache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<cache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());
let iter = unsafe { lock.table.iter() };

let result = cache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(iter),
typ: 2,
};
Expand All @@ -389,11 +395,11 @@ impl Cache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<cache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());
let iter = unsafe { lock.table.iter() };

let result = cache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(iter),
typ: 0,
};
Expand All @@ -411,11 +417,11 @@ impl Cache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<cache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());
let iter = unsafe { lock.table.iter() };

let result = cache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(iter),
typ: 1,
};
Expand Down
25 changes: 17 additions & 8 deletions src/bridge/fifocache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ impl FIFOCache {
lock.maxsize.get()
}

pub fn _state(&self) -> usize {
let lock = self.raw.lock();
lock.state.get()
}

/// Returns the number of elements in the table - len(self)
pub fn __len__(&self) -> usize {
let lock = self.raw.lock();
Expand Down Expand Up @@ -137,10 +142,11 @@ impl FIFOCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<fifocache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let state = lock.state.get();
let len = lock.table.len();

let result = fifocache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.iter()),
typ: 0,
};
Expand Down Expand Up @@ -365,10 +371,11 @@ impl FIFOCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<fifocache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let state = lock.state.get();
let len = lock.table.len();

let result = fifocache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.iter()),
typ: 2,
};
Expand All @@ -385,10 +392,11 @@ impl FIFOCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<fifocache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let state = lock.state.get();
let len = lock.table.len();

let result = fifocache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.iter()),
typ: 0,
};
Expand All @@ -405,10 +413,11 @@ impl FIFOCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<fifocache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let state = lock.state.get();
let len = lock.table.len();

let result = fifocache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.iter()),
typ: 1,
};
Expand Down
21 changes: 13 additions & 8 deletions src/bridge/lfucache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ impl LFUCache {
lock.maxsize.get()
}

pub fn _state(&self) -> usize {
let lock = self.raw.lock();
lock.state.get()
}

/// Returns the number of elements in the table - len(self)
pub fn __len__(&self) -> usize {
let lock = self.raw.lock();
Expand Down Expand Up @@ -144,10 +149,10 @@ impl LFUCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<lfucache_iterator>> {
let mut lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());

let result = lfucache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.iter()),
typ: 0,
};
Expand Down Expand Up @@ -393,10 +398,10 @@ impl LFUCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<lfucache_iterator>> {
let mut lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());

let result = lfucache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.iter()),
typ: 2,
};
Expand All @@ -413,10 +418,10 @@ impl LFUCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<lfucache_iterator>> {
let mut lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());

let result = lfucache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.iter()),
typ: 0,
};
Expand All @@ -433,10 +438,10 @@ impl LFUCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<lfucache_iterator>> {
let mut lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());

let result = lfucache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.iter()),
typ: 1,
};
Expand Down
21 changes: 13 additions & 8 deletions src/bridge/lrucache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ impl LRUCache {
lock.maxsize.get()
}

pub fn _state(&self) -> usize {
let lock = self.raw.lock();
lock.state.get()
}

/// Returns the number of elements in the table - len(self)
pub fn __len__(&self) -> usize {
let lock = self.raw.lock();
Expand Down Expand Up @@ -138,10 +143,10 @@ impl LRUCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<lrucache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());

let result = lrucache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.list.iter()),
typ: 0,
};
Expand Down Expand Up @@ -387,10 +392,10 @@ impl LRUCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<lrucache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());

let result = lrucache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.list.iter()),
typ: 2,
};
Expand All @@ -407,10 +412,10 @@ impl LRUCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<lrucache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());

let result = lrucache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.list.iter()),
typ: 0,
};
Expand All @@ -427,10 +432,10 @@ impl LRUCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<lrucache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());

let result = lrucache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(lock.list.iter()),
typ: 1,
};
Expand Down
24 changes: 15 additions & 9 deletions src/bridge/rrcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ impl RRCache {
lock.maxsize.get()
}

pub fn _state(&self) -> usize {
let lock = self.raw.lock();
lock.state.get()
}

/// Returns the number of elements in the table - len(self)
pub fn __len__(&self) -> usize {
let lock = self.raw.lock();
Expand Down Expand Up @@ -174,11 +179,11 @@ impl RRCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<cache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());
let iter = unsafe { lock.table.iter() };

let result = cache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(iter),
typ: 0,
};
Expand Down Expand Up @@ -376,7 +381,8 @@ impl RRCache {
/// Shrinks the cache to fit len(self) elements.
pub fn shrink_to_fit(&self) {
let mut lock = self.raw.lock();
lock.table.shrink_to(0, |x| x.0.hash)
lock.table.shrink_to(0, |x| x.0.hash);
lock.state.change();
}

/// Updates the cache with elements from a dictionary or an iterable object of key/value pairs.
Expand Down Expand Up @@ -430,11 +436,11 @@ impl RRCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<cache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());
let iter = unsafe { lock.table.iter() };

let result = cache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(iter),
typ: 2,
};
Expand All @@ -452,11 +458,11 @@ impl RRCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<cache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());
let iter = unsafe { lock.table.iter() };

let result = cache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(iter),
typ: 0,
};
Expand All @@ -474,11 +480,11 @@ impl RRCache {
py: pyo3::Python<'_>,
) -> pyo3::PyResult<pyo3::Py<cache_iterator>> {
let lock = slf.raw.lock();
let (len, capacity) = (lock.table.len(), lock.table.capacity());
let (len, state) = (lock.table.len(), lock.state.get());
let iter = unsafe { lock.table.iter() };

let result = cache_iterator {
ptr: _KeepForIter::new(slf.as_ptr(), capacity, len),
ptr: _KeepForIter::new(slf.as_ptr(), state, len),
iter: crate::mutex::Mutex::new(iter),
typ: 1,
};
Expand Down
Loading

0 comments on commit 09b6eb5

Please sign in to comment.