dependency-cache 0.4.0__tar.gz → 0.4.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.4.0 → dependency_cache-0.4.2}/PKG-INFO +1 -1
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/pyproject.toml +1 -1
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/dependency_cache/dependency_cache.pyi +4 -2
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/test_python.py +28 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/src/decorator.rs +7 -6
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/src/dependency_cache_base.rs +60 -43
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/src/metadata_hash_manager.rs +17 -10
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/src/py_introspection_utils.rs +1 -1
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/uv.lock +1 -1
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/.github/workflows/CI.yml +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/.github/workflows/Test.yml +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/.gitignore +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/Cargo.lock +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/Cargo.toml +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/LICENSE +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/README.md +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/dependency_cache/__init__.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/dependency_cache/plot_dependency_graph.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/automagic_dependency_example.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/example.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/inheritance_example.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/manual_dependency_example.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/runtime_dependencies_example.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/runtime_function_calls_example.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/static_function_calls_example.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/use_cache_example.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/test_python_automagic.py +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/src/decorator_factory.rs +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/src/dependency_graph.rs +0 -0
- {dependency_cache-0.4.0 → dependency_cache-0.4.2}/src/lib.rs +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dependency_cache
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.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.4.
|
|
14
|
+
version = "0.4.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" }
|
{dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/dependency_cache/dependency_cache.pyi
RENAMED
|
@@ -10,8 +10,10 @@ class DependencyCacheBase:
|
|
|
10
10
|
@property
|
|
11
11
|
def dependency_graph(self, /) -> dict: ...
|
|
12
12
|
def dump_cache(self, /, order: Sequence[str] | None = None) -> dict: ...
|
|
13
|
-
def get_cached_value(
|
|
14
|
-
|
|
13
|
+
def get_cached_value(
|
|
14
|
+
self, /, method_name: str, strict: bool = True, **kwargs
|
|
15
|
+
) -> Any | None: ...
|
|
16
|
+
def is_cached(self, /, method_name: str, **kwargs) -> bool: ...
|
|
15
17
|
def load_cache(self, /, data: dict, order: Sequence[str] | None = None) -> None: ...
|
|
16
18
|
def update_cached_value(
|
|
17
19
|
self, /, method_name: str, value: Any, **kwargs
|
|
@@ -139,6 +139,12 @@ class SerialisableObj(DependencyCacheBase):
|
|
|
139
139
|
return None
|
|
140
140
|
|
|
141
141
|
|
|
142
|
+
class NonesObj(DependencyCacheBase):
|
|
143
|
+
@dependency_cached()
|
|
144
|
+
def A(self):
|
|
145
|
+
return None
|
|
146
|
+
|
|
147
|
+
|
|
142
148
|
def test_raises_typeerror():
|
|
143
149
|
with pytest.raises(TypeError):
|
|
144
150
|
c = IncorrectInheritance()
|
|
@@ -345,3 +351,25 @@ def test_serialising_data(data, result):
|
|
|
345
351
|
assert obj.C() is not None
|
|
346
352
|
|
|
347
353
|
assert obj.C() == result
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def test_strict_get_raises():
|
|
357
|
+
obj = NonesObj()
|
|
358
|
+
|
|
359
|
+
with pytest.raises(KeyError):
|
|
360
|
+
obj.get_cached_value("A")
|
|
361
|
+
|
|
362
|
+
assert obj.A() is None
|
|
363
|
+
|
|
364
|
+
assert obj.is_cached("A") is True
|
|
365
|
+
|
|
366
|
+
assert obj.get_cached_value("A") is None
|
|
367
|
+
|
|
368
|
+
obj.clear_cached_value("A")
|
|
369
|
+
|
|
370
|
+
assert obj.is_cached("A") is False
|
|
371
|
+
|
|
372
|
+
with pytest.raises(KeyError):
|
|
373
|
+
obj.get_cached_value("A")
|
|
374
|
+
|
|
375
|
+
assert obj.get_cached_value("A", strict=False) is None
|
|
@@ -18,7 +18,7 @@ pub struct DependencyCacheDecorator {
|
|
|
18
18
|
#[pymethods]
|
|
19
19
|
impl DependencyCacheDecorator {
|
|
20
20
|
fn __set__(&self, _obj: Py<PyAny>, _value: Py<PyAny>) -> PyResult<()> {
|
|
21
|
-
return Err(PyTypeError::new_err("
|
|
21
|
+
return Err(PyTypeError::new_err("Cannot assign to decorated method"));
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
fn __get__(
|
|
@@ -57,18 +57,19 @@ impl DependencyCacheDecorator {
|
|
|
57
57
|
let instance = args.get_item(0)?;
|
|
58
58
|
let base = instance.cast::<DependencyCacheBase>().map_err(|_| {
|
|
59
59
|
PyTypeError::new_err(
|
|
60
|
-
"
|
|
60
|
+
"The decorated method's class must inherit from DependencyCacheBase",
|
|
61
61
|
)
|
|
62
62
|
})?;
|
|
63
63
|
|
|
64
64
|
let func = self.func.bind(py);
|
|
65
65
|
let (hash, metadata) = normalise_function_signature_and_hash(py, &self.method_name, Some(func), args, kwargs)?;
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
// objects cannot pass themselves as an argument as the cache will trigger already borrowed error
|
|
68
|
+
let key = base.try_borrow_mut()?.metadata_hash_manager.create_cache_key(py, hash, metadata.unbind())?;
|
|
68
69
|
|
|
69
70
|
// 1. add/check child dependencies
|
|
70
71
|
let mut dependency_keys = Vec::with_capacity(self.dependencies.len());
|
|
71
|
-
if self.dependencies.
|
|
72
|
+
if !self.dependencies.is_empty() {
|
|
72
73
|
for (hash, metadata) in &self.dependencies {
|
|
73
74
|
let dependency_key = base.borrow_mut()
|
|
74
75
|
.metadata_hash_manager
|
|
@@ -90,7 +91,7 @@ impl DependencyCacheDecorator {
|
|
|
90
91
|
|
|
91
92
|
let outcome = (|| -> PyResult<Py<PyAny>> {
|
|
92
93
|
// 3. check cache for value
|
|
93
|
-
let cached = base.borrow().
|
|
94
|
+
let cached = base.borrow().get_cached_value_by_key(py, key);
|
|
94
95
|
if let Some(cached) = cached {
|
|
95
96
|
return Ok(cached);
|
|
96
97
|
}
|
|
@@ -102,7 +103,7 @@ impl DependencyCacheDecorator {
|
|
|
102
103
|
base.borrow_mut()
|
|
103
104
|
.validate_current_method(key, self.use_cache);
|
|
104
105
|
base.borrow_mut()
|
|
105
|
-
.
|
|
106
|
+
.set_cached_value_by_key(key, result.clone_ref(py));
|
|
106
107
|
Ok(result)
|
|
107
108
|
})();
|
|
108
109
|
|
|
@@ -23,19 +23,19 @@ pub struct DependencyCacheBase {
|
|
|
23
23
|
|
|
24
24
|
impl DependencyCacheBase {
|
|
25
25
|
|
|
26
|
-
pub fn
|
|
27
|
-
if !self.method_dependency_graph.is_valid(
|
|
26
|
+
pub fn set_cached_value_by_key(&mut self, key: CacheKey, value: Py<PyAny>) {
|
|
27
|
+
if !self.method_dependency_graph.is_valid(key) {
|
|
28
28
|
return ();
|
|
29
29
|
}
|
|
30
|
-
self.cache.insert(
|
|
30
|
+
self.cache.insert(key, value);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
pub fn validate_current_method(&mut self,
|
|
33
|
+
pub fn validate_current_method(&mut self, key: CacheKey, use_cache: bool) {
|
|
34
34
|
if !use_cache {
|
|
35
|
-
self.method_dependency_graph.permanently_invalidate(
|
|
35
|
+
self.method_dependency_graph.permanently_invalidate(key);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
let child_validation_states: Vec<CacheKey> = self.method_dependency_graph.list_child_methods(&
|
|
38
|
+
let child_validation_states: Vec<CacheKey> = self.method_dependency_graph.list_child_methods(&key);
|
|
39
39
|
|
|
40
40
|
let mut new_state = ValidationState::Valid;
|
|
41
41
|
|
|
@@ -52,13 +52,13 @@ impl DependencyCacheBase {
|
|
|
52
52
|
|
|
53
53
|
match new_state {
|
|
54
54
|
ValidationState::PermanentlyInvalid => {
|
|
55
|
-
self.method_dependency_graph.permanently_invalidate(
|
|
55
|
+
self.method_dependency_graph.permanently_invalidate(key);
|
|
56
56
|
}
|
|
57
57
|
ValidationState::Invalid => {
|
|
58
|
-
self.method_dependency_graph.temporarily_invalidate(
|
|
58
|
+
self.method_dependency_graph.temporarily_invalidate(key);
|
|
59
59
|
}
|
|
60
60
|
ValidationState::Valid => {
|
|
61
|
-
self.method_dependency_graph.validate(
|
|
61
|
+
self.method_dependency_graph.validate(key);
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
}
|
|
@@ -67,38 +67,46 @@ impl DependencyCacheBase {
|
|
|
67
67
|
self.call_stack.last().cloned()
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
pub fn push_call_stack(&mut self,
|
|
71
|
-
self.call_stack.push((
|
|
70
|
+
pub fn push_call_stack(&mut self, key: CacheKey, add_parent_dependencies: bool) {
|
|
71
|
+
self.call_stack.push((key, add_parent_dependencies));
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
pub fn pop_call_stack(&mut self) {
|
|
75
75
|
self.call_stack.pop();
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
pub fn add_parent_dependency(&mut self,
|
|
78
|
+
pub fn add_parent_dependency(&mut self, key: CacheKey, dependency: CacheKey) {
|
|
79
79
|
self.method_dependency_graph
|
|
80
|
-
.add_parent_dependency(
|
|
80
|
+
.add_parent_dependency(key, vec![dependency]);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
pub fn add_children_dependencies(&mut self,
|
|
83
|
+
pub fn add_children_dependencies(&mut self, key: CacheKey, dependency: Vec<CacheKey>) {
|
|
84
84
|
self.method_dependency_graph
|
|
85
|
-
.add_children_dependency(
|
|
85
|
+
.add_children_dependency(key, dependency);
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
pub fn
|
|
89
|
-
if self.method_dependency_graph.is_valid(
|
|
90
|
-
return self.cache.get(&
|
|
88
|
+
pub fn get_cached_value_by_key(&self, py: Python<'_>, key: CacheKey) -> Option<Py<PyAny>> {
|
|
89
|
+
if self.method_dependency_graph.is_valid(key) {
|
|
90
|
+
return self.cache.get(&key).map(|obj| obj.clone_ref(py));
|
|
91
91
|
}
|
|
92
92
|
return None;
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
pub fn
|
|
95
|
+
pub fn create_key(&mut self, py: Python<'_>, method_name: &str, kwargs: &Option<Py<PyDict>>) -> PyResult<CacheKey> {
|
|
96
96
|
let empty_args = PyTuple::empty(py);
|
|
97
97
|
let kwargs_bound = kwargs.as_ref().map(|k| k.bind(py));
|
|
98
|
-
let (hash, sig) = normalise_function_signature_and_hash(py,
|
|
98
|
+
let (hash, sig) = normalise_function_signature_and_hash(py, method_name, None, &empty_args, kwargs_bound)?;
|
|
99
99
|
let key = self.metadata_hash_manager.create_cache_key(py, hash, sig.unbind());
|
|
100
100
|
return key;
|
|
101
101
|
}
|
|
102
|
+
|
|
103
|
+
pub fn get_key(&mut self, py: Python<'_>, method_name: &str, kwargs: &Option<Py<PyDict>>) -> PyResult<CacheKey> {
|
|
104
|
+
let empty_args = PyTuple::empty(py);
|
|
105
|
+
let kwargs_bound = kwargs.as_ref().map(|k| k.bind(py));
|
|
106
|
+
let (hash, sig) = normalise_function_signature_and_hash(py, method_name, None, &empty_args, kwargs_bound)?;
|
|
107
|
+
let key = self.metadata_hash_manager.get_cache_key(py, &hash, &sig);
|
|
108
|
+
return key;
|
|
109
|
+
}
|
|
102
110
|
}
|
|
103
111
|
|
|
104
112
|
#[pymethods]
|
|
@@ -114,39 +122,48 @@ impl DependencyCacheBase {
|
|
|
114
122
|
};
|
|
115
123
|
}
|
|
116
124
|
|
|
117
|
-
#[pyo3(signature = (method_name, **kwargs))]
|
|
118
|
-
pub fn get_cached_value(&mut self, py: Python<'_>, method_name: String, kwargs: Option<Py<PyDict>>) -> PyResult<Option<Py<PyAny>>> {
|
|
119
|
-
let
|
|
125
|
+
#[pyo3(signature = (method_name, strict=true, **kwargs))]
|
|
126
|
+
pub fn get_cached_value(&mut self, py: Python<'_>, method_name: String, strict: bool, kwargs: Option<Py<PyDict>>) -> PyResult<Option<Py<PyAny>>> {
|
|
127
|
+
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
120
128
|
|
|
121
|
-
if self.method_dependency_graph.is_valid(
|
|
122
|
-
|
|
129
|
+
if self.method_dependency_graph.is_valid(key) {
|
|
130
|
+
if let Some(obj) = self.cache.get(&key) {
|
|
131
|
+
return Ok(Some(obj.clone_ref(py)));
|
|
132
|
+
}
|
|
123
133
|
}
|
|
134
|
+
|
|
135
|
+
if strict {
|
|
136
|
+
return Err(PyKeyError::new_err(format!(
|
|
137
|
+
"Cannot get cached value for '{method_name}'"
|
|
138
|
+
)));
|
|
139
|
+
};
|
|
124
140
|
return Ok(None);
|
|
125
141
|
}
|
|
126
142
|
|
|
143
|
+
#[pyo3(signature = (method_name, **kwargs))]
|
|
127
144
|
pub fn is_cached(
|
|
128
145
|
&mut self,
|
|
129
146
|
py: Python<'_>,
|
|
130
147
|
method_name: String,
|
|
131
148
|
kwargs: Option<Py<PyDict>>,
|
|
132
149
|
) -> PyResult<bool> {
|
|
133
|
-
let
|
|
134
|
-
Ok(self.method_dependency_graph.is_valid(
|
|
150
|
+
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
151
|
+
Ok(self.method_dependency_graph.is_valid(key) && self.cache.contains_key(&key))
|
|
135
152
|
}
|
|
136
153
|
|
|
137
154
|
|
|
138
155
|
#[pyo3(signature = (method_name, value, **kwargs))]
|
|
139
156
|
pub fn update_cached_value(&mut self, py: Python<'_>, method_name: String, value: Py<PyAny>, kwargs: Option<Py<PyDict>>) -> PyResult<()> {
|
|
140
|
-
let
|
|
157
|
+
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
141
158
|
|
|
142
|
-
let Some(v) = self.cache.get_mut(&
|
|
159
|
+
let Some(v) = self.cache.get_mut(&key) else {
|
|
143
160
|
return Err(PyKeyError::new_err(format!(
|
|
144
161
|
"Cannot update cached value for '{method_name}'"
|
|
145
162
|
)));
|
|
146
163
|
};
|
|
147
164
|
*v = value;
|
|
148
|
-
self.method_dependency_graph.temporarily_invalidate(
|
|
149
|
-
self.method_dependency_graph.validate(
|
|
165
|
+
self.method_dependency_graph.temporarily_invalidate(key);
|
|
166
|
+
self.method_dependency_graph.validate(key);
|
|
150
167
|
return Ok(());
|
|
151
168
|
}
|
|
152
169
|
|
|
@@ -157,22 +174,22 @@ impl DependencyCacheBase {
|
|
|
157
174
|
|
|
158
175
|
#[pyo3(signature = (method_name, **kwargs))]
|
|
159
176
|
pub fn clear_cached_value(&mut self, py: Python<'_>, method_name: String, kwargs: Option<Py<PyDict>>) -> PyResult<()> {
|
|
160
|
-
let
|
|
177
|
+
let key = self.get_key(py, &method_name, &kwargs)?;
|
|
161
178
|
|
|
162
|
-
if self.cache.remove(&
|
|
179
|
+
if self.cache.remove(&key).is_none() {
|
|
163
180
|
return Err(PyKeyError::new_err(format!(
|
|
164
181
|
"Cannot clear cached value for '{method_name}'"
|
|
165
182
|
)));
|
|
166
183
|
}
|
|
167
|
-
self.method_dependency_graph.temporarily_invalidate(
|
|
184
|
+
self.method_dependency_graph.temporarily_invalidate(key);
|
|
168
185
|
Ok(())
|
|
169
186
|
}
|
|
170
187
|
|
|
171
188
|
#[getter]
|
|
172
189
|
pub fn get_cached_values<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
|
|
173
190
|
let dict = PyDict::new(py);
|
|
174
|
-
for (
|
|
175
|
-
let Some(bound) = self.metadata_hash_manager.get_signature(py,
|
|
191
|
+
for (key, value) in &self.cache {
|
|
192
|
+
let Some(bound) = self.metadata_hash_manager.get_signature(py, key) else {
|
|
176
193
|
continue;
|
|
177
194
|
};
|
|
178
195
|
|
|
@@ -196,8 +213,8 @@ impl DependencyCacheBase {
|
|
|
196
213
|
pub fn get_dependency_graph<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
|
|
197
214
|
let dict = PyDict::new(py);
|
|
198
215
|
|
|
199
|
-
for (
|
|
200
|
-
let Some(child_bound) = self.metadata_hash_manager.get_signature(py,
|
|
216
|
+
for (child_key, parent_keys) in &self.method_dependency_graph.clone_graph() {
|
|
217
|
+
let Some(child_bound) = self.metadata_hash_manager.get_signature(py, child_key) else {
|
|
201
218
|
continue;
|
|
202
219
|
};
|
|
203
220
|
|
|
@@ -213,8 +230,8 @@ impl DependencyCacheBase {
|
|
|
213
230
|
};
|
|
214
231
|
|
|
215
232
|
let mut parent_bounds = Vec::new();
|
|
216
|
-
for
|
|
217
|
-
if let Some(bound) = self.metadata_hash_manager.get_signature(py,
|
|
233
|
+
for parent_key in parent_keys {
|
|
234
|
+
if let Some(bound) = self.metadata_hash_manager.get_signature(py, parent_key) {
|
|
218
235
|
|
|
219
236
|
let parent_name: String = bound.get_item(0)?.extract()?;
|
|
220
237
|
let parent_args_item = bound.get_item(1)?;
|
|
@@ -239,8 +256,8 @@ impl DependencyCacheBase {
|
|
|
239
256
|
#[getter]
|
|
240
257
|
pub fn get_validation_state<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
|
|
241
258
|
let dict = PyDict::new(py);
|
|
242
|
-
for (
|
|
243
|
-
let Some(meta_bound) = self.metadata_hash_manager.get_signature(py,
|
|
259
|
+
for (key, state) in &self.method_dependency_graph.clone_state() {
|
|
260
|
+
let Some(meta_bound) = self.metadata_hash_manager.get_signature(py, key) else {
|
|
244
261
|
continue;
|
|
245
262
|
};
|
|
246
263
|
|
|
@@ -357,7 +374,7 @@ impl DependencyCacheBase {
|
|
|
357
374
|
let _ = slf.call_method(name, (), None)?;
|
|
358
375
|
|
|
359
376
|
let mut base = slf.borrow_mut();
|
|
360
|
-
let key = base.
|
|
377
|
+
let key = base.create_key(py, name, &None)?;
|
|
361
378
|
base.cache.insert(key, loaded_value.unbind());
|
|
362
379
|
Ok(())
|
|
363
380
|
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
use pyo3::prelude::*;
|
|
2
2
|
use pyo3::types::PyTuple;
|
|
3
|
+
use pyo3::exceptions::PyKeyError;
|
|
4
|
+
|
|
3
5
|
use std::collections::HashMap;
|
|
4
6
|
|
|
5
7
|
pub type CacheKey = u64;
|
|
@@ -15,7 +17,7 @@ impl MetadataHashManager {
|
|
|
15
17
|
Self { buckets: HashMap::new(), signatures: HashMap::new(), next_id: 0 }
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
fn find_cache_key(
|
|
19
21
|
&self,
|
|
20
22
|
py: Python<'_>,
|
|
21
23
|
hash: &isize,
|
|
@@ -24,32 +26,37 @@ impl MetadataHashManager {
|
|
|
24
26
|
let Some(bucket) = self.buckets.get(hash) else {
|
|
25
27
|
return Ok(None);
|
|
26
28
|
};
|
|
27
|
-
if bucket.len() == 1 {
|
|
28
|
-
let key = bucket[0];
|
|
29
|
-
let stored = self.signatures[&key].bind(py);
|
|
30
|
-
return Ok(stored.eq(sig)?.then_some(key));
|
|
31
|
-
}
|
|
32
29
|
for &key in bucket {
|
|
33
|
-
let
|
|
34
|
-
if
|
|
30
|
+
let stored_signature = self.signatures[&key].bind(py);
|
|
31
|
+
if stored_signature.eq(sig)? {
|
|
35
32
|
return Ok(Some(key));
|
|
36
33
|
}
|
|
37
34
|
}
|
|
38
35
|
Ok(None)
|
|
39
36
|
}
|
|
40
37
|
|
|
38
|
+
pub fn get_cache_key(
|
|
39
|
+
&self,
|
|
40
|
+
py: Python<'_>,
|
|
41
|
+
hash: &isize,
|
|
42
|
+
sig: &Bound<'_, PyTuple>,
|
|
43
|
+
) -> PyResult<CacheKey> {
|
|
44
|
+
self.find_cache_key(py, hash, sig)?
|
|
45
|
+
.ok_or_else(|| PyKeyError::new_err("No cache key found for provided signature"))
|
|
46
|
+
}
|
|
47
|
+
|
|
41
48
|
pub fn create_cache_key(
|
|
42
49
|
&mut self,
|
|
43
50
|
py: Python<'_>,
|
|
44
51
|
hash: isize,
|
|
45
52
|
sig: Py<PyTuple>,
|
|
46
53
|
) -> PyResult<CacheKey> {
|
|
47
|
-
if let Some(key) = self.
|
|
54
|
+
if let Some(key) = self.find_cache_key(py, &hash, sig.bind(py))? {
|
|
48
55
|
return Ok(key);
|
|
49
56
|
}
|
|
50
57
|
let key = self.next_id;
|
|
51
58
|
self.next_id += 1;
|
|
52
|
-
self.buckets.entry(hash
|
|
59
|
+
self.buckets.entry(hash).or_default().push(key);
|
|
53
60
|
self.signatures.insert(key, sig);
|
|
54
61
|
Ok(key)
|
|
55
62
|
}
|
|
@@ -182,7 +182,7 @@ pub fn ast_inspect_method_dependencies(py: Python<'_>, python_func: &Py<PyAny>)
|
|
|
182
182
|
let ast_module = PyModule::import(py, "ast")?;
|
|
183
183
|
let ast_tree = ast_module
|
|
184
184
|
.call_method1("parse", (function_string_clean,))
|
|
185
|
-
.map_err(|e| PyValueError::new_err(format!("
|
|
185
|
+
.map_err(|e| PyValueError::new_err(format!("Failed to parse source: {e}")))?;
|
|
186
186
|
|
|
187
187
|
let ast_function = ast_module.getattr("FunctionDef")?;
|
|
188
188
|
let ast_async_function = ast_module.getattr("AsyncFunctionDef")?;
|
|
@@ -329,7 +329,7 @@ wheels = [
|
|
|
329
329
|
|
|
330
330
|
[[package]]
|
|
331
331
|
name = "dependency-cache"
|
|
332
|
-
version = "0.4.
|
|
332
|
+
version = "0.4.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.4.0 → dependency_cache-0.4.2}/python/dependency_cache/plot_dependency_graph.py
RENAMED
|
File without changes
|
{dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/automagic_dependency_example.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/manual_dependency_example.py
RENAMED
|
File without changes
|
{dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/runtime_dependencies_example.py
RENAMED
|
File without changes
|
{dependency_cache-0.4.0 → dependency_cache-0.4.2}/python/examples/runtime_function_calls_example.py
RENAMED
|
File without changes
|
{dependency_cache-0.4.0 → dependency_cache-0.4.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
|