Ted Patrick > { Events & Community } > Adobe Systems


MAX - ActionScript 3 Performance Tuning - Strong Typing

At MAX 2006, I will be speaking on ActionScript 3 Performance Tuning. This past week I sat down with some engineers that worked on the new ActionScript Virtual Machine (AVM2) and did a deep dive on typing. Typing is very very very important in AS3 because it directly affects compiler output. Unlike AS1 and AS2, AS3 bytecode changes when typing information is added and the performance gains are substantial.

I sat down with Peter Grandmaison last week and he explained some of fine details of the new AVM2 in regards to member lookups.

Example Member Lookup:

myObject.x //find the value of x on myObject instance

There are essentially two types of member lookup routines within AVM2 as follows:

1. Strong references are equivalent to C++ member access (the cost of dereferencing an offset from a base pointer).

2. Weak references are a hash lookup.

Basically a weak reference is about 10X slower than using a strong reference in terms of clock cycles. Considering that member access is the single most used feature within any application, you can see how using weak references can add up quickly.

Here is a nice AS3 example showing use of Strong and Weak typing for function arguments and return types. If the reference is strong, the compiler will emit different bytecode with the SWF output file.

Strong and Weak Typing in AS3:

dynamic class Base extends Object {
var x:int;
var y:int;
}

class Example extends Base {
var z:int;
}

function a(o) //return weak
{
return o.x; // weak
}

function b( o ):int //return strong
{
return o.x; // weak
}

function c( o:Object ) //return weak
{
return o.x; // weak
}

function d( o:Object ):int //return strong
{
return o.x; // weak
}

function e( o:Base ) //return weak
{
return o.x; // strong
}

function f( o:Base ):int //return strong
{
return o.x; // strong
}

function g( o:Example ) //return weak
{
return o.x; // strong
}

function h( o:Example ):int //return strong
{
return o.x; // strong
}


function i( o:Example , prop:String ) //return weak
{
return o[prop]; // weak
}

function j( o:Example , prop:String ):int //return strong
{
return o[ prop ]; // weak
}

function k( o:Object ) //return weak
{
return o.z; // weak
}

function l( o:Object ):int //return strong
{
return o.z; // weak
}

function m( o:Example ) //return weak
{
return o.z; // strong
}

function n( o:Example ):int //return strong
{
return o.z; // strong
}

function o( o:Base ) //return weak
{
return o.z; // weak
}

function p( o:Base ):int //return strong
{
return o.z; // weak
}



Strong Typing also provides many benefits in terms of productivity. Flex Builder code hinting is based on typing information and compiler warning/errors are emitted using -strict compiler option. These productivity features are invaluable in lager projects and result in dramatically higher code quality with higher levels of performance.

As a Macromedia customer, I have been working with AS3 for the past 22 months since October of 2004, long before I was an Adobe employee. Somehow (Thanks Gary, Edwin, Werner, Chambers, Downey!!!) I was selected along with 5 other customers to play with a very early version of the AS3 compiler and AVM+ runtime (now AVM2). We were handed a command line compiler for AS3 and a modified Flash Player that could read and process ABC bytecode files. We had to bootstrap the ABC files into the player and workflow was quite a bit different from today. During the testing of the early builds of AVM2, I ported a 2D Tile Engine to AS3 to see if it would perform better. I got it to support 18,000 Tile Objects and a TileMap 50,000 X 50,000 pixels square with fluid motion and gameplay at 50FPS. I am going to port the tile engine to the release version of AS3 and optimize it for my MAX session on AS3. I will be showing the tile engine working in both Flash Blaze and Flex Builder so this session is important for both Flex and Flash developers.

There are tons of great AS3 performance tips and workflow. Come to my session at MAX and make your AS3 applications fast, fast, fast!!!

See you at MAX 2006!


Cheers,

Ted :)

7 Responses to “ MAX - ActionScript 3 Performance Tuning - Strong Typing ”

  1. # Blogger Ted Patrick

    Correction:

    Basically a weak reference is about 10X slower than using a strong reference in terms of clock cycles.  

  2. # Anonymous Anonymous

    In these 2 examples one of the return staements is weak & one is strong. What is the difference? Both function declarations have the return type set and both are referencing the property "z" in the same manner.

    function n( o:Example ):int //return strong
    {
    return o.z; // strong
    }

    function p( o:Base ):int //return strong
    {
    return o.z; // weak
    }

    Thanks, Jim  

  3. # Anonymous Anonymous

    Ignore my last post, i just saw that the passed object type was why there was a difference, me being dumb.

    Jim  

  4. # Blogger Ted Patrick

    Jim,

    The key with that case is that Base is a dynamic class where "z" is not defined. For non-defined dynamic members access is always weak.

    If a member variable is defined in a subclass then it will be a strong reference for that member lookup. Here is the case for that:

    //Note: x is not defined in Example
    //Note: x is defined in Base

    function q( o:Example ):int //return strong
    {
    return o.x; // strong
    }

    Cheers,

    Ted :)  

  5. # Anonymous pet-theory

    Any chance of releasing that presentation? My curiosity is piqued, to say the least. Besides strong typing, what else can we do to tune demanding AS3 apps?  

  6. # Anonymous Anonymous

    agree w/ pet-theory

    ted,could you please put your max silde on the blog?

    thanks ,david  

  7. # Anonymous Anonymous

    Is it possible to make the lookup in function i strong?

    For example, would the following work, and would it be weak or strong?

    function ii( o:Example , prop:int ):int //return strong
    {
    return o[ prop ]; // ??
    }  

Post a Comment



Jobs


Flex Jobs
city, state, zip


© 2008 Ted On Flex