memoeyes

Crates.iomemoeyes
lib.rsmemoeyes
version0.2.1
sourcesrc
created_at2023-11-05 19:41:01.711844
updated_at2023-11-13 08:49:40.353183
descriptionProcedural macros for automatic memoization
homepage
repositoryhttps://github.com/fruit-bird/memoeyes
max_upload_size
id1026146
size12,965
Hamza Hraiche (fruit-bird)

documentation

https://docs.rs/memoeyes

README

MemoEYES 👁️

Two procedual macros for automatically implementing memoization for your functions, making recursive function calls FAST

Might update this sometime to merge both macros into a single one with different args

#[lru_cache]

This macro creates a global static variable and uses it for memoization. It's also an LRU cache which makes it more convenient

#[lru_cache(max = 10)]
fn fib(n: u128) -> u128 {
    if n < 2 {
        return n;
    }
    fib(n - 1) + fib(n - 2)
}

let result = fib(186);
// result: 332825110087067562321196029789634457848

#[memo]

This macro is more explicit (which better follows Rust's philosophy) and does not use unsafe code. It modifies the function so that it has an extra argument that's a HashMap<TUPLE_OF_INPUT_TYPES, OUTPUT_TYPE>

Using this allows you to directly access the lookup table without having to go through unsafe blocks and implicit code

#[memo]
fn fib(n: u128) -> u128 {
    if n < 2 {
        return n;
    }
    fib(n - 1) + fib(n - 2)
}

let mut memo = HashMap::new();
let result = fib(186, &mut memo);
// result: 332825110087067562321196029789634457848

Contribution

Please do

What is This?

Wanted to get more familiar with function memoization, and wanted to learn attribute-like macros. Worked itself out. Unimportant but name inspiration

Commit count: 26

cargo fmt