Memoization in Actionscript
January 14th, 2007I love this programming technique, mostly because it is so easy to just drop into your code and instantly trade execution time for memory usage. More information can be found here.
-
package{
-
public class Memo{
-
-
static public function Memoize(obj:*,func:Function):Function{
-
-
// *** Each argument to func must provide a unique value when
-
// *** converted to a string. For example, something that just
-
// *** prints out as [Object] will not work.
-
-
var hash:Object = {};
-
-
var f:Function = function(...args):*{
-
-
// Check hash for result.
-
var key:String = args.join(",");
-
-
var result:* = hash[key];
-
-
if(result == null){
-
-
result = func.apply(obj, args);
-
-
hash[key] = result;
-
}
-
-
return result;
-
}
-
-
return f;
-
}
-
}
-
}
Example Usage
-
package{
-
-
import Memo;
-
-
public class MemoTest{
-
-
private var SumSeries_Memo:Function; // Our memoized variant of the SumSeries function.
-
-
public function MemoTest(){
-
-
SumSeries_Memo = Memo.Memoize(null,SumSeries); // Create the memoized version.
-
-
var result:Number;
-
result = SumSeries_Memo(100,1); // Computes result, caches it, and returns it.
-
result = SumSeries_Memo(100,1); // Returns cached result.
-
result = SumSeries_Memo(100,1); // Returns cached result.
-
}
-
-
static public function SumSeries(last:Number,step:Number):Number{
-
var result:Number = (((last * last)/step) + last)/2;
-
return result;
-
}
-
}
This is the simplest version that I could come up with which works in the greatest variety of cases. If you knew that you were only going to use it with integer arguments, then you could create a faster method to generate keys than converting the arguments to strings and combining them with join.
In case that it isn't clear, memoization only helps if you are calling the memoized function many times with the exact same arguments. Otherwise, it will be slower, due to the overhead of creating the keys and storing the result, and it will be a waste of memory. It is a very nice alternative to creating explicit lookup tables, because you are creating the table on the fly instead of beforehand. If you decide later that you would prefer for the computations to be done beforehand, you can simple call the memoized function sooner and let it cache the results.
If you have a different version of this technique, please post it to the comments area.
Emacs mode for AS3
December 31st, 2006I've rewritten my actionscript-mode to support AS3. I've stripped out everything related to AS2, but you can still get the old version from here.
I have been using this new mode for a couple months and I haven't had any problems, but your coding style might differ from mine, so please let me know if you run into any issues.
Update: Here is the other set of functions that I use when I code actionscript. As I said before, you probably will want to just pick and choose some functions from this file since it is highly customized to my personal environment and has a lot of dependencies.