#memoization #cache #proc-macro

macro memoize

Attribute macro for auto-memoizing functions with somewhat-simple signatures

8 releases

0.1.7 Feb 22, 2021
0.1.6 Dec 16, 2020
0.1.4 Oct 16, 2020

#125 in Procedural macros

Download history 11/week @ 2020-12-31 5/week @ 2021-01-07 11/week @ 2021-01-14 9/week @ 2021-01-21 4/week @ 2021-01-28 5/week @ 2021-02-04 106/week @ 2021-02-11 33/week @ 2021-02-18 12/week @ 2021-02-25 2/week @ 2021-03-04 6/week @ 2021-03-11 37/week @ 2021-03-25 13/week @ 2021-04-01 12/week @ 2021-04-08 20/week @ 2021-04-15

84 downloads per month

MIT license

17KB
236 lines

memoize

Docs.rs Crates.rs CI

A #[memoize] attribute for somewhat simple Rust functions: That is, functions with one or more Clone-able arguments, and a Clone-able return type. That's it.

Read the documentation (cargo doc --open) for the sparse details, or take a look at the examples/, if you want to know more:

// From examples/test2.rs

use memoize::memoize;

#[memoize]
fn hello(arg: String, arg2: usize) -> bool {
  arg.len()%2 == arg2
}

fn main() {
  // `hello` is only called once here.
  assert!(! hello("World".to_string(), 0));
  assert!(! hello("World".to_string(), 0));
  // Sometimes one might need the original function.
  assert!(! memoized_original_hello("World".to_string(), 0));
}

This is expanded into (with a few simplifications):

// This is obviously further expanded before compiling.
lazy_static! {
  static ref MEMOIZED_MAPPING_HELLO : Mutex<HashMap<String, bool>>;
}

fn memoized_original_hello(arg: String, arg2: usize) -> bool {
  arg.len() % 2 == arg2
}

fn hello(arg: String, arg2: usize) -> bool {
  let mut hm = &mut MEMOIZED_MAPPING_HELLO.lock().unwrap();
  if let Some(r) = hm.get(&(arg.clone(), arg2.clone())) {
    return r.clone();
  }
  let r = memoized_original_hello(arg.clone(), arg2.clone());
  hm.insert((arg, arg2), r.clone());
  r
}

Further Functionality

You can choose to use an LRU cache. In fact, if you know that a memoized function has an unbounded number of different inputs, you should do this! In that case, use the attribute like this:

// From examples/test1.rs
// Compile with --features=full
use memoize::memoize;

#[derive(Debug, Clone)]
struct ComplexStruct {
  // ...
}

#[memoize(Capacity: 123)]
fn hello(key: String) -> ComplexStruct {
  // ...
}

Adding more caches and configuration options is relatively simple, and a matter of parsing attribute parameters. Currently, compiling will fail if you use a parameter such as Capacity without the feature full being enabled.

Another parameter is TimeToLive, specifying how long a cached value is allowed to live:

#[memoize(Capacity: 123, TimeToLive: Duration::from_secs(2))]

chrono::Duration is also possible, but would have to first be converted to std::time::Duration

#[memoize(TimeToLive: chrono::Duration::hours(3).to_std().unwrap())]

The cached value will never be older than duration provided and instead recalculated on the next request.

Contributions

...are always welcome! This being my first procedural-macros crate, I am grateful for improvements of functionality and style. Please send a pull request, and don't be discouraged if it takes a while for me to review it; I'm sometimes a bit slow to catch up here :) -- Lewin

Dependencies

~1.5MB
~41K SLoC