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!!!

Advertisements

Filed under: actionscript

4 Responses

  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

  2. Levan says:

    Hi,
    can you elaborate on following comment:

    “activation object which has some performance and memory impact.”

    is it relative to? ?
    what does it mean “some” is it a lot or insignificant?

    “length of the scope chain” – so what would be the length of scope chain if that function would be outside of the outer function would it be significantly less?

    IMHO “performance implication outlines above regarcing function closures” are such insignificant that parsing a “The quick brown fox jumps over the lazy dog” into byte array would be slower that that.

    For example:
    have you looked at usage of closures in Flex Framework? do you see performance impact in flex applications vs pure AS based apps? I personally don’t see and if there is any tradeoff greatly outweighs magnitude of performance impact.
    especially now when average citizen has dual core CPU Machines.

  3. Levan says:

    ont he side Note, (sorry for additional commment, I just realized that point now )
    to achive any arbitrary usecase functionality which requires or “is good candidate” to use closures, without them it will most likely require some additional, helper,transfere etc … variables, or even functions, so from that angle margin of difference isability vs performance of closures is even smaller.

  4. Marcy says:

    I don’t like the sound of all those lists he’s making – it’s like fetching too multitudinous notes at philosophy; you feel you’ve achieved something when you haven’t.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: