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

Actionscript:
  1. public class Class1
  2. {
  3.     import flash.utils.getTimer;
  4.    
  5.     public function Class1()
  6.     {
  7.         var t:int = getTimer();
  8.         var a:Array = [];
  9.         var i:int;
  10.         for( i=0; i<1000000; i++)
  11.         {
  12.             var x:int = i*i;
  13.             a.push( x );
  14.         }
  15.         trace( 'constructor: ' + (getTimer() - t) );
  16.     }
  17.    
  18.     public function init():void
  19.     {
  20.         var t:int = getTimer();
  21.         var a:Array = [];
  22.         var i:int;
  23.         for( i=0; i<1000000; i++)
  24.         {
  25.             var x:int = i*i;
  26.             a.push( x );
  27.         }
  28.         trace( 'init method: ' + (getTimer() - t) );
  29.     }
  30. }

in the .fla I simply add:

Actionscript:
  1. var c1:Class1 = new Class1();
  2. c1.init();

16 Comments so far

  1. Sam C / September 11th, 2007 9:39 pm

    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.

  2. gabriel / September 12th, 2007 3:46 pm

    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.

  3. liam / September 14th, 2007 12:49 pm

    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.

  4. gabriel / September 14th, 2007 2:52 pm

    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..

  5. liam / September 14th, 2007 3:56 pm

    Ah, that makes sense. I've not done nearly enough AS3. You fellas all coming to FOTB?

  6. gabriel / September 14th, 2007 5:11 pm

    Yeah, we'll be at FOTB! see you there innit

  7. evden eve nakliyat / October 3rd, 2007 11:12 am

    thanks very good site.

  8. Philipp / October 9th, 2007 2:12 pm

    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

  9. Jan / October 11th, 2007 8:39 pm

    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!

  10. gabriel / October 12th, 2007 3:08 pm

    I used screentime for flash, you have to buy it

    http://www.screentime.com

  11. Werbeagentur / December 8th, 2007 10:45 pm

    Thanks, really a great site

  12. radyo / December 23rd, 2007 7:11 pm

    thanks

  13. kale kapi / January 12th, 2008 11:09 am

    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

  14. Duman / February 24th, 2008 1:38 pm

    realy good thanks

  15. ISO 9001 / March 25th, 2008 11:05 pm

    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..

  16. promosyon / April 30th, 2008 3:13 pm

Leave a reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image