The NEXT thing to try!!

Icon

Random rants and tips about NETCF, WinCE, Emacs, Powershell etc.

Performance Tuning in ActionSctipt 3.0

Yesterday I went through this nice document by Gray Grossman (creator of ActionScript) which talks about some easy tips and tricks to boost the runtime performance of your code. Too my surprise I was not following any of the three tips he has mentioned.

Promotoion of Numeric types – An array in Actionscript is nothing but a hash so an access to any element in the array is used as a key to find the value. This happens everytime you write something like array[f]. But in AS an array access is faster if the key is a int/uint. To take advantage of this one can just force the access to be index based rather then key based by type coercion of the index. This can be done by accessing the value as array[(int(f)]. Isn’t this a cool, neat and nice way?

Common Subexpression Elimination – Common Subexpression elimination is compiler optimization techinique which is employed by almost all the matured compilers. What they do is they cache some of frequently occuring common subexpressions and provide the result from the cached values but sometimes the language semantics do not allow compiler to deploy this optimization while generating code. As rightly pointed out by the author because in actionscript we have concept of getters and setters a getter/setter subexpression cannot be eliminated.Let’s say you have a property p in your AS class C and you have defined getters/setters for the property like this:

public function get p():*
{
return _p;
}


public function set p(value:*):void
{
_p = value;
pDirty = true
}

In the code if you access this property using the getter the compiler is bound not to eliminate the subexpression. But if you are using _p you are safe.

One more such example is of array.length

for (var i:int=0; i<array.length; i++)
{

}

Here also compiler can’t eliminate a.length and thus it is advisable to use:

var n:int = array.length
for (var i:int=0; i<n; i++)
{

}

Method Closures- AS is based on ECMAScript specification and thus it allows nested functions. An example could be:

public function f()
{
var g:Function = (function (args:Array)
{
trace(“I am in g”);
});

But having a nested function costs. For each nested function, the outer function has to create an activation object which has some performance and memory impact.
Now what are these memory impacts? Whenever a function is executed a activation object is created for the call. In lay man terms it is actually pushing all the arguments, the return address on the stack. Thus an activation object stores all these things along with all the local variable declarations. Along with this a scope chain is also created that contains an ordered list of objects that Flash Player checks for identifier declarations. Thus each function has a scope chain property (internal property) associated with it. The scope chain goes up until it reaches the global object.

Coming back to nested functions, in case of such declarations not only there is an overhead of an extra storage for activation object, the increase in length of the scope chain increases the scope of the function and thus player’s job of scope-resolution increases which hurts performance.

Thus, next time your code execution can become really slow if you just don’t bother!!!

Filed under: actionscript

One Response

  1. aioakr says:

    Sorry for interrupting you… buy clomid http://www.playlist.com/blog/entry/12526097155 buy clomid See you soon!

    Good morning! buy clomid buy clmid
    buy clomid I’ve enjoyed seeing you

Leave a Reply