How to implement call caching (Memoization)

Issue

I want to implement call cache(memoization) in non-intrusive way with the metadata annotations.

Hopefully, it will work like this:

class A{
  @Cached
  foo(msg) {
    return msg;
  }
}

void main() {
  @Cached
  var foo = ()=>"hello";
}

Can it be achieved with only dart:mirrors ?

Solution

I wrote a whole blog post on this topic a while ago. It’s too long to copy here, so here’s the link:

http://dartery.blogspot.com/2012/09/memoizing-functions-in-dart.html

The upshot is that you can write higher-order memoizing functions, but they’re limited in generality by Dart’s lack of flexible args functions. Also, if you want to use dynamic programming with recursive functions, you need to write your function with memoization in mind – it needs to take itself as an argument, so you can pass in the memoized version.

Answered By – Justin Fagnani

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *