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
-
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) );
-
}
-
}
in the .fla I simply add:
-
var c1:Class1 = new Class1();
-
c1.init();
16 Comments so far
Leave a reply
I had a nice long reply but apparently I mistyped they CAPTCHA and it was gone when I hit the back button. So instead I'll give you the recap. For having lots of live objects take a look at the Flyweight pattern. Also take a look at how Flex renders grids and list boxes.
Sam, yes, I'm aware of flyweight patterns and this engine uses them where possible, the instances only hold minimal data (world coordinates and altitude) the rest is handled via references to other objects.
Erm... What if the constructor calls the init method itself. I've never really been comfortable with code in constructors (but they all call their own init) because of the way sub-classes automatically called 'super' in their constructors. My love to Gordon.
Alright liam,
If you call init() from the constructor, it would be JIT as it's a proprietary method, but I generally call init externally because I want to call it after I have added a DisplayObject as a child so that I can access the stage or root properties, which aren't accessible until it has been "parented"
Gordon appreciates your love..
Ah, that makes sense. I've not done nearly enough AS3. You fellas all coming to FOTB?
Yeah, we'll be at FOTB! see you there innit
thanks very good site.
hi ...
i looked for a different way to contact you but only found this one - so sorry for commenting the wrong post.
i've been using your MouseWheel-Script for a while now ... and right now i'm trying to implement it using AS3. have you done this already?
greets, phil
Hi there... I was wondering if you could share the wrapper code for getting the flash to work as a screen saver on the Mac?
Thanks!
I used screentime for flash, you have to buy it
http://www.screentime.com
Thanks, really a great site
thanks
Erm... What if the constructor calls the init method itself. I've never really been comfortable with code in constructors (but they all call their own init) because of the way sub-classes automatically called 'super' in their constructors
realy good thanks
Alright liam,
If you call init() from the constructor, it would be JIT as it's a proprietary method, but I generally call init externally because I want to call it after I have added a DisplayObject as a child so that I can access the stage or root properties, which aren't accessible until it has been "parented"
Gordon appreciates your love..
Tekstil - Promosyon - Şapka - Yağmurluk - Atkı - Bere - Forma - İmalat - üretici - Ambalaj - Çanta
Şapka imalat
Tekstil imalat