Random Etc. Notes to self. Work, play, and the rest.

Optimising Actionscript 3

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.


4 Comments

Hi Tom, interesting post. Whilst i’ve often precalculated stuff (eg. trig functions) the idea of memoization is new to me and I can see it being profoundly useful.

My experience of optimisation broadly echos your own, i.e. timing things and profiling code is crucial, you might think you know where your app is spending its time but you can never be sure.

Have you tried Adobe’s flex 3 beta? They have a code profiler built in now which is very useful ofr memory optimisation, I’m getting to the point where I don’t know how I lived without it.

Posted by Tom P on 11 January 2008 @ 4pm

Yeah, it’s nice to give a technique a name. It’s rare that it’s worth the coding overhead though!

We have been using that on a couple of projects recently. It is indeed a great addition to Flex Builder. The function timing wasn’t stable enough when we were doing Arc, but it seems to be improving rapidly.

Posted by TomC on 11 January 2008 @ 8pm

[...] Go  get the full story. [...]

Posted by Visual Harmonics » Post Topic » Memoisation: Give me space, and take your time on 3 October 2008 @ 12pm

[...] che si deve realizzare… spero possa esserti utile anche questo post sulle ottimizzazioni in as3: Random Etc. - Optimising Actionscript 3 Cheers __________________ [...]

Posted by [Flash CS3] Fluidità tween - FlepStudio forum on 24 November 2009 @ 7am