Closed kalekundert closed 2 years ago
On the topic of caching, I'm also wondering if would be possible to implement @cached_property
-style caching, e.g. where the cached value is written to the instance dictionary such that it shadows the descriptor and can be read/written with no performance penalty.
Some thoughts on how to do this:
load()
. To do this, I'd need to maintain a list of the attributes that have been cached.I can't completely get rid or the get
and set
arguments, though. SWMB uses get
a lot, in particular for binding arguments. set
is less used, but goes hand-in-hand with get
.
Pseudo-code implementation:
Getter
gains a dynamic
attribute.
Key.dynamic
: taken from configMethod.dynamic
: True by defaultFunc.dynamic
: False by defaultValue.dynamic
: FalseValuesIter
gains dynamic
attribute that is set by the iterator itself during iteration. Basically, it will be False unless any getter with dynamic == True
is encountered.param
stores ValuesIter.dynamic
in its state, and uses it to decide when to refresh the cache.
For example, I might want to recalculate an attribute each time if its value involves a
Method
key, since that could depend on other attributes of the object, but cache the attribute if it involves any other key. Note that this depends on which values are actually used; e.g. consumed by thepick()
generator.Key
should take its default caching behavior from the configs it matches, so that static configs can be cached by default while potentially dynamic configs can be looked up every time.The API would be straight-forward: