dependency-cache 0.5.1__tar.gz → 0.5.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/PKG-INFO +1 -1
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/pyproject.toml +1 -1
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/src/dependency_cache_base.rs +80 -39
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/src/metadata_hash_manager.rs +1 -1
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/uv.lock +1 -1
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/.github/workflows/CI.yml +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/.github/workflows/Test.yml +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/.gitignore +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/Cargo.lock +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/Cargo.toml +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/LICENSE +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/README.md +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/dependency_cache/__init__.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/dependency_cache/dependency_cache.pyi +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/dependency_cache/plot_dependency_graph.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/automagic_dependency_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/custom_decorator_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/custom_scenario_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/inheritance_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/manual_dependency_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/runtime_dependencies_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/runtime_function_calls_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/serialisable_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/static_function_calls_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/use_cache_example.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/test_python.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/test_python_automagic.py +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/src/decorator.rs +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/src/decorator_factory.rs +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/src/dependency_graph.rs +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/src/lib.rs +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/src/py_introspection_utils.rs +0 -0
- {dependency_cache-0.5.1 → dependency_cache-0.5.2}/src/scenario.rs +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dependency_cache
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.2
|
|
4
4
|
Classifier: Programming Language :: Rust
|
|
5
5
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
6
6
|
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
@@ -11,7 +11,7 @@ classifiers = [
|
|
|
11
11
|
"Programming Language :: Python :: Implementation :: PyPy",
|
|
12
12
|
]
|
|
13
13
|
keywords = ["caching", "dependency-graph", "invalidation"]
|
|
14
|
-
version = "0.5.
|
|
14
|
+
version = "0.5.2"
|
|
15
15
|
description = "A caching library for Python object methods that utilises a dependency graph to track relationships and control invalidation, enabling efficient recomputation"
|
|
16
16
|
readme = "README.md"
|
|
17
17
|
license = { text = "MIT" }
|
|
@@ -5,7 +5,7 @@ use pyo3::exceptions::{PyKeyError, PyRuntimeError, PyValueError};
|
|
|
5
5
|
use rand::rng;
|
|
6
6
|
use rand::seq::SliceRandom;
|
|
7
7
|
|
|
8
|
-
use std::collections::
|
|
8
|
+
use std::collections::HashSet;
|
|
9
9
|
|
|
10
10
|
use crate::dependency_graph::{MethodDependencyGraph, ValidationState};
|
|
11
11
|
use crate::py_introspection_utils::{normalise_function_signature_and_hash, validate_self_only_method};
|
|
@@ -14,14 +14,23 @@ use crate::metadata_hash_manager::{CacheKey, MetadataHashManager};
|
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
pub struct CacheLayer {
|
|
17
|
-
pub cache:
|
|
17
|
+
pub cache: Vec<Option<Py<PyAny>>>,
|
|
18
18
|
pub graph_snapshot: MethodDependencyGraph<CacheKey>, //TODO: maybe less expensive way to handling graph snapshot...
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
|
|
22
|
+
#[inline]
|
|
23
|
+
fn set_cache_value(cache: &mut Vec<Option<Py<PyAny>>>, key: usize, value: Py<PyAny>) {
|
|
24
|
+
if key >= cache.len() {
|
|
25
|
+
cache.resize_with(key + 1, || None);
|
|
26
|
+
}
|
|
27
|
+
cache[key] = Some(value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
22
31
|
#[pyclass(subclass)]
|
|
23
32
|
pub struct DependencyCacheBase {
|
|
24
|
-
pub cache:
|
|
33
|
+
pub cache: Vec<Option<Py<PyAny>>>,
|
|
25
34
|
pub method_dependency_graph: MethodDependencyGraph<CacheKey>,
|
|
26
35
|
pub metadata_hash_manager: MetadataHashManager,
|
|
27
36
|
pub call_stack: Vec<(CacheKey, bool)>,
|
|
@@ -35,9 +44,9 @@ impl DependencyCacheBase {
|
|
|
35
44
|
return ();
|
|
36
45
|
}
|
|
37
46
|
if let Some(layer) = self.cache_stack.last_mut() {
|
|
38
|
-
layer.cache
|
|
47
|
+
set_cache_value(&mut layer.cache, key, value);
|
|
39
48
|
} else {
|
|
40
|
-
self.cache
|
|
49
|
+
set_cache_value(&mut self.cache, key, value);
|
|
41
50
|
};
|
|
42
51
|
}
|
|
43
52
|
|
|
@@ -98,10 +107,10 @@ impl DependencyCacheBase {
|
|
|
98
107
|
|
|
99
108
|
pub fn get_cached_value_by_key(&self, py: Python<'_>, key: CacheKey) -> Option<Py<PyAny>> {
|
|
100
109
|
if self.method_dependency_graph.is_valid(key) {
|
|
101
|
-
if let Some(temp_cache_value) = self.get_from_temporary_cache(py,
|
|
110
|
+
if let Some(temp_cache_value) = self.get_from_temporary_cache(py, key) {
|
|
102
111
|
return Some(temp_cache_value);
|
|
103
112
|
}
|
|
104
|
-
return self.cache.get(
|
|
113
|
+
return self.cache.get(key).and_then(|obj| obj.as_ref().map(|v| v.clone_ref(py)));
|
|
105
114
|
}
|
|
106
115
|
return None;
|
|
107
116
|
}
|
|
@@ -124,7 +133,7 @@ impl DependencyCacheBase {
|
|
|
124
133
|
|
|
125
134
|
pub fn push_cache_stack(&mut self) {
|
|
126
135
|
return self.cache_stack.push(CacheLayer {
|
|
127
|
-
cache:
|
|
136
|
+
cache: Vec::new(),
|
|
128
137
|
graph_snapshot: self.method_dependency_graph.clone(),
|
|
129
138
|
});
|
|
130
139
|
}
|
|
@@ -139,59 +148,91 @@ impl DependencyCacheBase {
|
|
|
139
148
|
return Ok(());
|
|
140
149
|
}
|
|
141
150
|
|
|
142
|
-
pub fn get_from_temporary_cache(&self, py: Python<'_>, key:
|
|
151
|
+
pub fn get_from_temporary_cache(&self, py: Python<'_>, key: CacheKey) -> Option<Py<PyAny>> {
|
|
143
152
|
for layer in self.cache_stack.iter().rev() {
|
|
144
|
-
if let Some(v) = layer.cache.get(key) {
|
|
153
|
+
if let Some(Some(v)) = layer.cache.get(key) {
|
|
145
154
|
return Some(v.clone_ref(py));
|
|
146
155
|
}
|
|
147
156
|
}
|
|
148
157
|
return None;
|
|
149
158
|
}
|
|
150
159
|
|
|
151
|
-
pub fn contains_cached_value(&self, key:
|
|
152
|
-
if !self.method_dependency_graph.is_valid(
|
|
160
|
+
pub fn contains_cached_value(&self, key: CacheKey) -> bool {
|
|
161
|
+
if !self.method_dependency_graph.is_valid(key) {
|
|
153
162
|
return false;
|
|
154
163
|
}
|
|
155
164
|
return self.cache_stack
|
|
156
165
|
.iter()
|
|
157
166
|
.rev()
|
|
158
|
-
.any(|layer| layer.cache.
|
|
159
|
-
|| self.cache.
|
|
167
|
+
.any(|layer| layer.cache.get(key).is_some_and(|obj| obj.is_some()))
|
|
168
|
+
|| self.cache.get(key).is_some_and(|obj| obj.is_some());
|
|
160
169
|
}
|
|
161
170
|
|
|
162
|
-
pub fn remove_cached_value(&mut self, key:
|
|
171
|
+
pub fn remove_cached_value(&mut self, key: CacheKey) -> Option<Py<PyAny>> {
|
|
163
172
|
for layer in self.cache_stack.iter_mut().rev() {
|
|
164
|
-
if let Some(
|
|
165
|
-
|
|
173
|
+
if let Some(opt) = layer.cache.get_mut(key) {
|
|
174
|
+
if let Some(v) = opt.take() {
|
|
175
|
+
return Some(v);
|
|
176
|
+
}
|
|
166
177
|
}
|
|
167
178
|
}
|
|
168
|
-
|
|
179
|
+
if let Some(opt) = self.cache.get_mut(key) {
|
|
180
|
+
return opt.take();
|
|
181
|
+
}
|
|
182
|
+
return None;
|
|
169
183
|
}
|
|
170
184
|
|
|
171
|
-
pub fn effective_cache(&self) ->
|
|
172
|
-
let
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
185
|
+
pub fn effective_cache(&self) -> Vec<(CacheKey, &Py<PyAny>)> {
|
|
186
|
+
let max_len = self
|
|
187
|
+
.cache_stack
|
|
188
|
+
.iter()
|
|
189
|
+
.map(|l| l.cache.len())
|
|
190
|
+
.chain(std::iter::once(self.cache.len()))
|
|
191
|
+
.max()
|
|
192
|
+
.unwrap_or(0);
|
|
193
|
+
|
|
194
|
+
let mut result = Vec::new();
|
|
195
|
+
for i in 0..max_len {
|
|
196
|
+
let mut value: Option<&Py<PyAny>> =
|
|
197
|
+
self.cache.get(i).and_then(|opt| opt.as_ref());
|
|
198
|
+
|
|
199
|
+
for layer in &self.cache_stack {
|
|
200
|
+
if let Some(Some(v)) = layer.cache.get(i) {
|
|
201
|
+
value = Some(v);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if let Some(v) = value {
|
|
206
|
+
result.push((i, v));
|
|
176
207
|
}
|
|
177
208
|
}
|
|
178
|
-
return
|
|
209
|
+
return result;
|
|
179
210
|
}
|
|
180
211
|
|
|
181
212
|
pub fn clear_effective_cache(&mut self) -> () {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
213
|
+
let mut removed_keys: HashSet<CacheKey> = HashSet::new();
|
|
214
|
+
|
|
215
|
+
for layer in self.cache_stack.iter_mut() {
|
|
216
|
+
for (i, obj) in layer.cache.iter_mut().enumerate() {
|
|
217
|
+
if obj.is_some() {
|
|
218
|
+
*obj = None;
|
|
219
|
+
removed_keys.insert(i);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
for (i, obj) in self.cache.iter_mut().enumerate() {
|
|
224
|
+
if obj.is_some() {
|
|
225
|
+
*obj = None;
|
|
226
|
+
removed_keys.insert(i);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if removed_keys.is_empty() {
|
|
231
|
+
self.method_dependency_graph.invalidate_all();
|
|
232
|
+
} else {
|
|
188
233
|
for key in removed_keys {
|
|
189
234
|
self.method_dependency_graph.temporarily_invalidate(key);
|
|
190
235
|
}
|
|
191
|
-
layer.cache.clear();
|
|
192
|
-
} else {
|
|
193
|
-
self.method_dependency_graph.invalidate_all();
|
|
194
|
-
self.cache.clear();
|
|
195
236
|
}
|
|
196
237
|
}
|
|
197
238
|
}
|
|
@@ -202,7 +243,7 @@ impl DependencyCacheBase {
|
|
|
202
243
|
#[pyo3(signature = (*_args, **_kwargs))]
|
|
203
244
|
fn new(_args: &Bound<'_, PyTuple>, _kwargs: Option<&Bound<'_, PyDict>>) -> Self {
|
|
204
245
|
return Self {
|
|
205
|
-
cache:
|
|
246
|
+
cache: Vec::new(),
|
|
206
247
|
method_dependency_graph: MethodDependencyGraph::new(),
|
|
207
248
|
metadata_hash_manager: MetadataHashManager::new(),
|
|
208
249
|
call_stack: Vec::new(),
|
|
@@ -240,7 +281,7 @@ impl DependencyCacheBase {
|
|
|
240
281
|
kwargs: Option<Py<PyDict>>,
|
|
241
282
|
) -> PyResult<bool> {
|
|
242
283
|
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
243
|
-
return Ok(self.contains_cached_value(
|
|
284
|
+
return Ok(self.contains_cached_value(key));
|
|
244
285
|
}
|
|
245
286
|
|
|
246
287
|
|
|
@@ -254,7 +295,7 @@ impl DependencyCacheBase {
|
|
|
254
295
|
) -> PyResult<()> {
|
|
255
296
|
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
256
297
|
|
|
257
|
-
if !self.contains_cached_value(
|
|
298
|
+
if !self.contains_cached_value(key) {
|
|
258
299
|
return Err(PyKeyError::new_err(format!(
|
|
259
300
|
"Cannot update cached value for '{method_name}'"
|
|
260
301
|
)));
|
|
@@ -278,7 +319,7 @@ impl DependencyCacheBase {
|
|
|
278
319
|
) -> PyResult<()> {
|
|
279
320
|
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
280
321
|
|
|
281
|
-
if self.remove_cached_value(
|
|
322
|
+
if self.remove_cached_value(key).is_none() {
|
|
282
323
|
return Err(PyKeyError::new_err(format!(
|
|
283
324
|
"Cannot clear cached value for '{method_name}'"
|
|
284
325
|
)));
|
|
@@ -478,7 +519,7 @@ impl DependencyCacheBase {
|
|
|
478
519
|
|
|
479
520
|
let mut base = slf.borrow_mut();
|
|
480
521
|
let key = base.create_key(py, name, &None)?;
|
|
481
|
-
base.cache.
|
|
522
|
+
set_cache_value(base.cache.as_mut(), key, loaded_value.unbind());
|
|
482
523
|
Ok(())
|
|
483
524
|
};
|
|
484
525
|
|
|
@@ -329,7 +329,7 @@ wheels = [
|
|
|
329
329
|
|
|
330
330
|
[[package]]
|
|
331
331
|
name = "dependency-cache"
|
|
332
|
-
version = "0.5.
|
|
332
|
+
version = "0.5.2"
|
|
333
333
|
source = { editable = "." }
|
|
334
334
|
dependencies = [
|
|
335
335
|
{ name = "matplotlib", version = "3.7.5", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.9'" },
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/dependency_cache/dependency_cache.pyi
RENAMED
|
File without changes
|
{dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/dependency_cache/plot_dependency_graph.py
RENAMED
|
File without changes
|
{dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/automagic_dependency_example.py
RENAMED
|
File without changes
|
{dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/custom_decorator_example.py
RENAMED
|
File without changes
|
{dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/custom_scenario_example.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/manual_dependency_example.py
RENAMED
|
File without changes
|
{dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/runtime_dependencies_example.py
RENAMED
|
File without changes
|
{dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/runtime_function_calls_example.py
RENAMED
|
File without changes
|
|
File without changes
|
{dependency_cache-0.5.1 → dependency_cache-0.5.2}/python/examples/static_function_calls_example.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|