This idea comes from function programming and is really a design pattern I suppose. I’m not aware of whether this has been done before in Obj-C or in any other language other then Haskell. I haven’t done and extensive search. If anyone knows otherwise then please correct me.
Just found this http://www.apl.jhu.edu/~paulmac/c++-memoization.html
!!!!Warning :- most people probably wouldn’t use this because it will not work well with methods which rely on side effects but anyway and IO
A RecultCacher is a proxy which which is passed an object and a list of selectors which should have their results cached. The constructor could read something like:
When the proxy receives a message it hash’s the arguments and the selector and looks to see if it has the result already. If the result exists already it returns the result (copies the result before returning it if it can). If it doesn’t then it forwards the message to the original object. When the object returns it keeps a copy of the result in it’s cache ready for the next thing it’s called.
** Some points ** The number of requests for an argument hash could be recorded. If the limit to the cache is reached only the least requested is removed. The alternative would be to remove the oldest or the one which was accessed the last.
No message which as a void return type would be allowed as they would imply causing a side affect.
** Not as a proxy **
This could be implemented as a class on it’s own and used for caching within messages. So it would simply perform the task of hashing the arguments and discovering them in a hash table. It could also do the coping of the values too. An interface something like:
The function would have to request it’s cache from a Cache singleton. this singleton would be passed the methods selector ideally. ** Time for an example ** Ok I don’t think this example would work but I hope it illustrates the idea.
… static ResultCacher* cachedFib = [ResultCacher initWithObject:[fib class]]; // Lets assume the we want all the messages to have there values cached. …
@implementation fib
** Non-proxy example **
@implementation fib
I would appreciate constructive criticism on this.
Stefan Pantos