From: Robert Feldt Date: 2001-03-05T23:04:26+09:00 Subject: [ruby-talk:12014] ANN: Memoize 0.1.2 Hi, This is to announce Memoize: "for speeding up methods/functions by caching their results; on subsequent calls the cached result is returned instead of being recalculated". This has speeded up some of my calculation-intensive stuff and at the recent GOTRUG meeting Aleksi encouraged me to submit it to RAA. So, here it is... You'll find the README below. Regards, Robert ************************************************* * Memoize - caching method results for speed-up * ************************************************* Version: 0.1.2 Release date: 2001-03-05 Available from: http://www.ce.chalmers.se/~feldt/ruby/extensions/memoize Donwload: http://www.ce.chalmers.se/~feldt/ruby/extensions/memoize/memoize-0-1-2.tar.gz Author: Robert Feldt, feldt@ce.chalmers.se README version: $Id: README,v 1.3 2001/03/05 13:59:46 feldt Exp $ What is it? ----------- Directive for making your functions faster by trading space for time. When you "memoize" a method/function its results are cached so that later calls with the same arguments returns results in the cache instead of recalculating them. Synopsis -------- require 'memoize' # From top-level memoize(:my_slow_function) # In class def class C def m # Heavy calc end memoize :m end # my_slow_function and C#m may now return results faster Optionally you can supply a cache to be used for caching results: memoize(:my_slow_function, BoundedLruCache.new(50)) Cache is any object that responds to [] and []= (basically Hashes). You can implement different expiration policies by doing your own cache. One example is in resultcache.rb: * BoundedLruCache - Hash with fixed size, that will delete least-recently-used values when full. Installation? ------------- 1. unpack tarball (if you haven't already) 2. install: ruby install.rb 3. If you've got RubyUnit installed you can also run some tests... Requirements? ------------- None; plain Ruby >= 1.6.2. I've successfully used Memoize with Ruby 1.7.0 (2001-01-23) and cygwin 1.1.8 (gcc version 2.95.2-6) on Windows NT 4.0 Workstation. NOTE THAT THIS IS AN ALPHA RELEASE SO THERE WILL LIKELY BE BUGS. RubyUnit is needed to run unit tests. Known bugs? ----------- * Does not work on class methods. Pretty easy to fix. Will be soon... * Blocks/procs do not work as expected since Ruby will create unique block for each method call? So following won't give no speed-up even though the same block is passed both times: p = proc{|i| 2*i} r1 = my_memoized_method(&p) r2 = my_memoized_method(&p) (matz will fix this in future Ruby version, see ruby-talk:12001) Special things to note? ----------------------- DO NOT memoize METHODS THAT * have side effects (printing output, altering files etc.), * depend on state other than its arguments * return a data structure that is modified by its caller It mostly makes sense to use on "stand-alone", computationally expensive (especially recursive) methods. However, we might alleviate some of these situations in the future by incorporating a normalization proc to use to get a hash for the arguments. License and legal issues? ------------------------- Memoize is copyright (c) 2001 Robert Feldt, feldt@ce.chalmers.se. All rights reserved. Memoize is distributed under LGPL. See LICENSE and COPYING-LESSER. Acknowledgements? ----------------- Thanks to Aleksi Niemel� for pointing out that what I called "result caching" has been called "memoize" in the perl community and that there is a Memoize extension to perl in CPAN. I'm still not sure "memoize" is a better name than "result caching" but if the former is already in use I'll comply... ;-) Do you have comments or questions? ---------------------------------- I'd appreciate if you drop me a note if you're successfully using Memoize. If there are some known users I'll be more motivated to packing up additions / new versions and post them to RAA. Please give feedback! Happy coding! Robert Feldt, feldt@ce.chalmers.se