#[memoize]
Expand description
memoize is an attribute to create a memoized version of a (simple enough) function.
So far, it works on non-method functions with one or more arguments returning a Clone
-able
value. Arguments that are cached must be Clone
-able and Hash
-able as well. Several clones
happen within the storage and recall layer, with the assumption being that memoize
is used to
cache such expensive functions that very few clone()
s do not matter. memoize
doesn’t work on
methods (functions with [&/&mut/]self
receiver).
Calls are memoized for the lifetime of a program, using a statically allocated, Mutex-protected HashMap.
Memoizing functions is very simple: As long as the above-stated requirements are fulfilled,
simply use the #[memoize::memoize]
attribute:
use memoize::memoize;
#[memoize]
fn hello(arg: String, arg2: usize) -> bool {
arg.len()%2 == arg2
}
// `hello` is only called once.
assert!(! hello("World".to_string(), 0));
assert!(! hello("World".to_string(), 0));
If you need to use the un-memoized function, it is always available as memoized_original_{fn}
,
in this case: memoized_original_hello()
.
Parameters can be ignored by the cache using the Ignore
parameter. Ignore
can be specified
multiple times, once per each parameter. Ignore
d parameters do not need to implement Clone
or Hash
.
See the examples
for concrete applications.
The following descriptions need the full
feature enabled.
The memoize
attribute can take further parameters in order to use an LRU cache:
#[memoize(Capacity: 1234)]
. In that case, instead of a HashMap
we use an lru::LruCache
with the given capacity.
#[memoize(TimeToLive: Duration::from_secs(2))]
. In that case, cached value will be actual
no longer than duration provided and refreshed with next request. If you prefer chrono::Duration,
it can be also used: #[memoize(TimeToLive: chrono::Duration::hours(9).to_std().unwrap()]
You can also specify a custom hasher: #[memoize(CustomHasher: ahash::HashMap)]
, as some hashers don’t use a new()
method to initialize them, you can also specifiy a HasherInit
parameter, like this: #[memoize(CustomHasher: FxHashMap, HasherInit: FxHashMap::default())]
, so it will initialize your FxHashMap
with FxHashMap::default()
insteado of FxHashMap::new()
This mechanism can, in principle, be extended (in the source code) to any other cache mechanism.
memoized_flush_<function name>()
allows you to clear the underlying memoization cache of a
function. This function is generated with the same visibility as the memoized function.