AS3.0 JIT vs. Interpreted


I’ve been developing an application recently that requires large numbers of classes to be initialised as soon as the application is loaded. It’s an isometric display engine, a la “Sim City”, with a world map of 800 x 600 tiles, and multiple layers ( terrain, objects, transport networks etc) there’s potential for over 100,000 instances!!!

I found ActionScript 3.0 and AVM2: Performance Tuning by Gary Grossman, which covers many aspects of how AVM2 is different from AVM1. The part I was interested in was about $init and $cinit (class constructor) functions being interpreted and everything else being JIT.

The initial setup of the engine runs over a timed loop (to stop script timeouts), and this was typically taking 20 to 30 seconds. After reading the above PDF, it became clear that class constructors are interpreted, not JIT compiled, so all I had to do was move the code out of the constructor, into an init() function, and call if after the constructor, this shaved a huge amount of time off the initial build of the landscape.

Some initial tests, with a for loop running from 0 to 10000 showed that in the constructor it took about 350ms, and called via another method, it took only 240ms. It is not always faster, but 90% of the time it’s faster.


here’s my test example
[as]
public class Class1
{
import flash.utils.getTimer;

public function Class1()
{
var t:int = getTimer();
var a:Array = [];
var i:int;
for( i=0; i< 1000000; i++)
{
var x:int = i*i;
a.push( x );
}
trace( ‘constructor: ‘ + (getTimer() – t) );
}

public function init():void
{
var t:int = getTimer();
var a:Array = [];
var i:int;
for( i=0; i< 1000000; i++)
{
var x:int = i*i;
a.push( x );
}
trace( ‘init method: ‘ + (getTimer() – t) );
}
}
[/as]

in the .fla I simply add:
[as]
var c1:Class1 = new Class1();
c1.init();
[/as]

Comments are closed.