Crates.io | cache-macro |
lib.rs | cache-macro |
version | 0.4.1 |
source | src |
created_at | 2018-11-25 14:47:00.192438 |
updated_at | 2018-11-25 14:54:03.060752 |
description | A procedural macro for automatically caching the output of functions. |
homepage | https://github.com/tylerreisinger/cache-macro |
repository | https://github.com/tylerreisinger/cache-macro |
max_upload_size | |
id | 98601 |
size | 33,571 |
A procedural macro to automatically cache the result of a function given a set of inputs.
Previously named 'lru-cache-macros', but renamed to reflect the broadening of scope.
use cache_macro::cache;
use lru_cache::LruCache;
#[cache(LruCache : LruCache::new(20))]
fn fib(x: u32) -> u64 {
println!("{:?}", x);
if x <= 1 {
1
} else {
fib(x - 1) + fib(x - 2)
}
}
assert_eq!(fib(19), 6765);
The above example only calls fib
twenty times, with the values from 0 to 19. All intermediate
results because of the recursion hit the cache.
Simply place #[cache(CacheType : constructor)]
above your function. The function must obey a few properties
to use lru_cache:
Clone
.self
in any form.The LruCache
type used must accept two generic parameters <Args, Return>
and must support methods
get_mut(&K) -> Option<&mut V>
and insert(K, V)
. The lru-cache
(for LRU caching)
and expiring_map
(for time-to-live caching) crates currently meet these requirements.
Currently, this crate only works on nightly rust. However, once the 2018 edition stabilizes as well as the procedural macro diagnostic interface, it should be able to run on stable.
The lru_cache macro can be configured by adding additional attributes under #[cache(...)]
.
All configuration attributes take the form #[cache_cfg(...)]
. The available attributes are:
#[cache_cfg(ignore_args = ...)]
This allows certain arguments to be ignored for the purposes of caching. That means they are not part of the hash table key and thus should never influence the output of the function. It can be useful for diagnostic settings, returning the number of times executed, or other introspection purposes.
ignore_args
takes a comma-separated list of variable identifiers to ignore.
use cache_macro::cache;
use lru_cache::LruCache;
#[cache(LruCache : LruCache::new(20))]
#[cache_cfg(ignore_args = call_count)]
fn fib(x: u64, call_count: &mut u32) -> u64 {
*call_count += 1;
if x <= 1 {
1
} else {
fib(x - 1, call_count) + fib(x - 2, call_count)
}
}
let mut call_count = 0;
assert_eq!(fib(39, &mut call_count), 102_334_155);
assert_eq!(call_count, 40);
The call_count
argument can vary, caching is only done based on x
.
#[cache_cfg(thread_local)]
Store the cache in thread-local storage instead of global static storage. This avoids the overhead of Mutex locking, but each thread will be given its own cache, and all caching will not affect any other thread.
Expanding on the first example:
use cache_macro::cache;
use lru_cache::LruCache;
#[cache(LruCache : LruCache::new(20))]
#[cache_cfg(thread_local)]
fn fib(x: u32) -> u64 {
println!("{:?}", x);
if x <= 1 {
1
} else {
fib(x - 1) + fib(x - 2)
}
}
assert_eq!(fib(19), 6765);
The created cache is stored as a static variable protected by a mutex unless the #[cache_cfg(thread_local)]
configuration is added.
With the default settings, the fibonacci example will generate the following code:
fn __lru_base_fib(x: u32) -> u64 {
if x <= 1 { 1 } else { fib(x - 1) + fib(x - 2) }
}
fn fib(x: u32) -> u64 {
use lazy_static::lazy_static;
use std::sync::Mutex;
lazy_static! {
static ref cache: Mutex<::lru_cache::LruCache<(u32,), u64>> =
Mutex::new(::lru_cache::LruCache::new(20usize));
}
let cloned_args = (x.clone(),);
let mut cache_unlocked = cache.lock().unwrap();
let stored_result = cache_unlocked.get_mut(&cloned_args);
if let Some(stored_result) = stored_result {
return stored_result.clone();
};
drop(cache_unlocked);
let ret = __lru_base_fib(x);
let mut cache_unlocked = cache.lock().unwrap();
cache_unlocked.insert(cloned_args, ret.clone());
ret
}
Whereas, if you use the #[lru_config(thread_local)]
the generated code will look like:
fn __lru_base_fib(x: u32) -> u64 {
if x <= 1 { 1 } else { fib(x - 1) + fib(x - 2) }
}
fn fib(x: u32) -> u64 {
use std::cell::UnsafeCell;
use std::thread_local;
thread_local!(
static cache: UnsafeCell<::lru_cache::LruCache<(u32,), u64>> =
UnsafeCell::new(::lru_cache::LruCache::new(20usize));
);
cache.with(|c|
{
let mut cache_ref = unsafe { &mut *c.get() };
let cloned_args = (x.clone(),);
let stored_result = cache_ref.get_mut(&cloned_args);
if let Some(stored_result) = stored_result {
stored_result.clone()
} else {
let ret = __lru_base_fib(x);
cache_ref.insert(cloned_args, ret.clone());
ret
}
})
}