int/uint Coercion and Fast Pathing Arrays

This is something to consider for intensive loops in AS3 and is probably not a bad idea to use in general although the gains may be minimal. I was reading about the benefits of strict type annotations in Actionscript 3 and how they are handled in the new VM. There have been other studies that show 20% increase in speed for loops that take advantage of Array() fast pathing. So, let’s see how it works…

The slower way:
Notice that within the loop ‘n’ is multiplied by 4 and therefore promoted to a Number.

[as]
var n:int;
for(n=0;n<1000;n++){
_array[n*4] = 2;
}
[/as]

The faster way:
Notice how the result of the operation is cast back to the int type to take advantage of fast path indexing in the array.

[as]
var n:int;
for(n=0;n<1000;n++){
_array[int(n*4)] = 2;
}
[/as]

More information about this topic can be found here: http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf

About John Pencola

Hello, my name is John Pencola. I can't get enough of exploring new technologies, discussing software principals, creating programs and having fun with interface design. I will share my experiences here and hope Liquid Language adds some useful information to the vast sea that is the web.
This entry was posted in ActionScript 3 and tagged , , , , , , , , , , . Bookmark the permalink.