Attribute Macro memoize::memoize [−][src]
#[memoize]
Expand description
memoize is an attribute to create a memoized version of a (simple enough) function.
So far, it works on functions with one or more arguments which are Clone
- and Hash
-able,
returning a Clone
-able value. 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()
.
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()]
This mechanism can, in principle, be extended (in the source code) to any other cache mechanism.