Skip to content

Rust crate that memoizes your function using derive annotations.

Notifications You must be signed in to change notification settings

y2kappa/memoize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust memoize proc macro

#[memoize]
fn add(x: i32, y: i32) -> i32 {
    x * y
}

fn main() {
    for _ in 0..3 {
        println!("{:?}", add(100, 200));
    }
}
Calculated "100-200" with value 20000
20000
Loaded "100-200" from cache with value 20000
20000
Loaded "100-200" from cache with value 20000
20000

async mode as well:

#[tokio::main]
async fn main() {
    for _ in 0..10 {
        println!("{:?}", get_employee(1).await.data.employee_name);
    }
}

#[memoize]
#[timed]
async fn get_employee(employee_id: i32) -> Employee {
    let url = format!(
        "http://dummy.restapiexample.com/api/v1/employee/{}",
        employee_id
    );
    println!("Calling {}", url);

    reqwest::get(&url)
        .await
        .unwrap()
        .json::<Employee>()
        .await
        .unwrap()
}

Output:

Calling http://dummy.restapiexample.com/api/v1/employee/1
function=get_employee duration=1.18539639s
"Tiger Nixon"
function=get_employee duration=5.084µs
"Tiger Nixon"
function=get_employee duration=2.806µs
"Tiger Nixon"
function=get_employee duration=1.658µs
"Tiger Nixon"
function=get_employee duration=1.502µs
"Tiger Nixon"
function=get_employee duration=1.559µs
"Tiger Nixon"
function=get_employee duration=1.529µs
"Tiger Nixon"
function=get_employee duration=1.485µs
"Tiger Nixon"
function=get_employee duration=1.502µs
"Tiger Nixon"
function=get_employee duration=1.496µs
"Tiger Nixon"

TODO

  • allow/test function with no args
  • measure performance
  • ensure noop when function does not have an output
  • TTL cache maybe
  • cache prints with debug mode
  • async test
  • dynamically rename cache to be custom to that function

About

Rust crate that memoizes your function using derive annotations.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages