AS3 optimations & suggestions

AS3 rocks, but hey (arg), I still need more performance. First of all some findings.

Do not overload condition and statements as in AS2

for(;;)
{
	ax = x - ( ( ay = ry < < 6 ) - y ) / hta;
	if ( tile = m[ --ry ][ ax >> 6 ] )
	{
		if ( tside = tile[ 0 ] )
		{
			nearest = ( dx = ax - x ) * dx + ( dy = ay - y ) * dy;
			side = tside;
			offset = ax % 64;
			break;
		}
		break;
	}
	if ( tile == undefined ) break;
}
[...]

This is an optimized (ugly) code clip from a raycaster, written in AS2. But in AS3 it’s slower. You have to test several cases. In most cases, it’s better to extract computed values to variables before testing them in condition as below.

var x: Number = 0;
while( x++ < 1000 )
{
	// do something
}

var x: Number = 0;
while( x < 1000 )
{
	x++;
	// do something faster
}

if( ( z = iz * 100 ) < 0 )
{
	// do something
}

z = iz * 100;

if( z < 0 )
{
	// do something faster
}

Do not use Objects, if you know, which properties will be finally involved.
I have converted an old 3d engine to AS3. My vertices was written as objects {x:0,y:0,z:0}. After writing the objects as a Vertex class with x,y,z named as public properties, my movie fps increases dramatically. AS3 seems to register a memory space for them and will have faster access.

Cast instances, while reading from an Array
A Raycaster have different textures, stored in an Array. While reading pixels from the texture, do not use: images[index].getPixel(x,y);. Before you call the method, store the instance in a typed variable: var image: BitmapData = images[index];. After that, flash finds the instance associated method x times faster.

Try to use Integers (int) for all possible cases
There is really often a possebility to work with Integers instead of Numbers(float). The lost resolution can be neglected, when working with higher values than with floats before. see also

Bitoperators are lightning fast
There are a lot of cases, where you can use bit-operators for computing expressions. Mostly while using heavily x^2 values. Try to change your boundings from bitmaps and Arrays to accomplish it.

Suggestions:

performance

I want typed, length-fixed Arrays. Since I found the Vertex vs Object thing above, it's clear that AS3 cannot reserve memory for Arrays, while they can change their length during runtime. But this would increase the speed once again.

Please, a faster solution to read/write pixels on a bitmap. No problem for me to bunch the RGB(A) values into a simple typed byteArray with a fixed length to work with. Unfortunatley reading from ByteArrays is slower than from an Array, though they are not typed. So the getPixels, setPixels method do not increase the speed.

cosmetic

I wonder, why we still have to write the 'function' and 'var' keywords to define Functions and Variables. That really begins to bother me. It's a time waster :)

Feel free to add your suggestions.

22 thoughts on “AS3 optimations & suggestions”

  1. Awesome Feedback! Each of your findings is right on. The key to writing fast code is making sure Flash can figure out the types of things.

    * AS3 Array’s are optimized for dense access. if you use integer indexes from 0..N with no holes, we internally use a flat packed array. If there are holes or non-uint keys, then internally it’s a hashtable. The real cost of Array (assuming you treat it as a dense array) is from boxing and unboxing the values that are stored, since Arrays are untyped. This isn’t exactly what you want but it may help you in tuning.

    * If you point us to a sample comparing ByteArray to Array, we’d love to look at it. ByteArray should be faster than Array, for byte/short/int values.

  2. Great, now that’s some interesting stuff! Since you found out that there is no memory reserved for arrays I agree with your suggestions.
    Unfortunately I haven’t looked close enough at AS 3 to come up with anything new.
    And yea, remove that “var”. I wouldn’t mind keeping the “function” tho, but the “var”… a pain in the ass.

  3. Oh man, typed arrays would be really great!!! I tend to add a ‘generic like’ comment to all array declarations like that

    public var cList : Array; //

    … hoping that one day we will get real typed arrays.

  4. The html parser killed the rest of my comment, i meant something like that:
    public var cList : Array; // (Customer)

  5. Great tips, thanks Andre.

    About your remark “AS3 seems to register a memory space for them and will have faster access.”, I attented at the session Gary Grossman did at MAX 2005 and he explained that the new way of keeping track of the structure of a class, called “traits”, indeed tells the player at what byte-offset it can find class properties (Gary, crrect me if I’m wrong :-), so that dramatically speeds up things, but also required you to type your variables, so the player can optimize memory access.

  6. “Bitoperators are lightning fast”

    I like the sound of that, however I’m one of the many who have real diffculty getting my head around binary – I get what it is and how it works, but I get confused easily when it comes to relating ‘normal’ values to their binary counterparts for bitwise operations.

    So hopefully either you or somebody else can take up the challenge and illustrate some useful examples of using bitwise operators for something other than hex color manipulations. That would be so insightful and useful from my point of view.

  7. +1

    and some more suggestions:

    I want native (if possible) core class methods added to String, Array, Object, Boolean, etc.

    if .NET and Java got them I want them too for AS3

    I don’t want to reinvent the wheel or have to use a StringUtil/String2/PuppetString class for such heavyly needed stuff as String.Format, startsWith, endsWith, trim, etc.

    see more explanation here

    seriously it is needed.

  8. I understand that generics are too much of an undertaking, but what about ‘classic’ typed arrays for value types
    like int[] byte[] etc.? This would eliminate the cost of boxing/unboxing.

  9. Hi,I also use getTimer() to calculate the time, and run each function 50 times to get the average time.However, the results were not stable.

    In addition, I run the .as in Adobe Flash CS3 Professional and Flex separately, but the results were totally different.

    Hence, my question is :

    Is getTimer() accurate?Are there some other ways to calculate the time?
    why the results are not stable?
    Which is more accuarte, Adobe Flash CS3 Professional or Flex?

    Thank you.

Comments are closed.