Optimising Actionscript 3

7th January 2008 @ 11:28 pm
nowhere
This is the first in a series of posts about actionscript 3 that I announced earlier. Here's the feed of posts tagged as3.

Even with the great speed improvements that actionscript 3 and Flash 9 offer over actionscript 2 and Flash 8, I've occasionally had the need to revisit a project and look for places to optimise.

Miscellaneous Tips

Here's a list of suggestions I recently made to Gabe, who has his own actionscripting notes, as he was revisiting an old project looking to reduce CPU usage.

Tweaking the framerate

Simply dropping the framerate of an application might work as far as CPU usage goes too.

(Be careful with this though - if you're watching exclusively watching the CPU meters, and not the app, you might be optimising for the wrong audience!)

You can do that with the SWF metadata tag above your main app class in Flex Builder. The main bit is [SWF(frameRate='15')] but check the help file as you might have background colours and width/height in there already.

Memoization

You probably won't need it, but the trick used in Digg Arc (for calculating arc points before redrawing them) was to use what's called memoization — it helps with space-time trade-offs.

The idea is to remember the results of calling a function with certain arguments and only recalculate when the arguments change.

e.g. You might have a function that you're calling it a lot with the same values e.g. calculatePoints(1,2,3,4) that looks a bit like this:

function calculatePoints(a,b,c,d):Array { var points:Array = []; /* lots of heavy math with a,b,c,d and points */ return points; }

Instead of calculating every possible variation, you can create a hash of the common results:

private var results:Object = {};

function calculatePoints(a,b,c,d):Array { var key:String = [a, b, c, d].join(); var points:Array = results[key] as Array; if (points) { return points; } else { points = []; } /* lots of heavy math with a,b,c,d and points */ results[key] = points; return points; }

Then if calculatePoints is called again with 1,2,3,4 you grab the answer from your results Object and return that instead of calculating it again. You probably want to set a limit on the number of things you cache though, so don't use this technique without thinking about that, and only consider this if your performance bottleneck is definitely one maths-heavy function!

Caveat Optimisor!

The only way to be sure about optimisations is to time things, manually or in your development environment, and bear in mind that even to experienced coders some performance issues can be unintuitive.