actionscript-3performanceuntyped-variables

AS3 Untyped Variable Efficiency


I'm (still) trying to solve an issue in my 2D Flash game where my framerate is dropping to unacceptable lows. I have a class with the following member variable and method:

protected var value:*;
public function getValue () :* {
    return this.value;
}

Right now, that method is being called seemingly a few hundred times per frame and 2.23% of my application time is spent in that method.

Do you think that I'll notice a significance performance increase by making value a typed variable? Making that variable typed will make code all over the application break so I'd like to be more sure that it will actually help.

Also, here's the top few records of the Performance Profile for my application, sorted by Self Time, which led me to suspect this getValue() method in the first place:

Method                             Calls    Self Time (ms)
------                             -----    --------------
[pre-render]                       0        2137 (19.68%)
[reap]                             0        727 (6.7%)
[enterFrameEvent]                  0        464 (4.27%)
[mouseEvent]                       0        352 (3.24%)
[mark]                             0        327 (3.01%)
State.getValue                     792356   242 (2.23%)
[verify]                           0        209 (1.93%)
[render]                           0        159 (1.46%)
CollisionManager.detectCollisions  584      156 (1.44%)
Entity.updateAllStates             30227    154 (1.42%)
Entity.getStateValue               392412   143 (1.32%)
GSVector.set y                     156244   141 (1.3%)
State.update                       659738   123 (1.13%)

Solution

  • In a word, yes. Strong typing is one of the principle reasons why AS3 is faster than AS2 (because in AS2 all variables are untyped, and every time you reference an untyped variable Flash has to internally check what type it is).

    Normally for a question like this I'd hem and haw about the pitfalls of performance tuning, about how architectural changes are usually more effective than code-level tuning, and how most content is bottlenecked by rendering rather than code execution... but in this case, using typed variables is something you should be doing anyway, as it makes your code more robust, more readable, easier to maintain, and less prone to bugs.

    There's really no good reason for using untyped variables in AS3. Take the time to make this change, and it will pay off in the long run even if you don't see a performance increase (though I think you will).