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();

50 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
  17. Kfz Versicherung / June 3rd, 2008 6:40 pm

    Very good site. Thanks

  18. orhan / June 19th, 2008 2:02 pm

    hi, good jop so thanks.

  19. radyolarburada / June 21st, 2008 9:36 am

    nice howto i test this on my wp homepage thanx

  20. des / June 21st, 2008 10:25 am

    Shouldn't "and call if after the constructor" be "and call it after the constructor". Having a read through your blogg cos I'm swatting up on AS3 shizzle. Great blog man!

    I miss you, we'll have to go for a pint soon.

  21. Şarkı Sözleri / July 6th, 2008 2:59 pm

    I miss you, we'll have to go for a pint soon

  22. Chat / August 31st, 2008 11:21 pm

    Thanks :p

  23. porno izle / September 14th, 2008 9:57 am

    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"

  24. gabriel / September 16th, 2008 10:08 am

    if you want to call a method after a DisplayObject has been added to the stage, I generally use the Event.ADDED_TO_STAGE event.

    in the constructor put:
    addEventListener( Event.ADDED_TO_STAGE, _init );

    private function _init( e:Event=null ):void
    {
    // initing code
    stage.something....
    }

  25. kamilow / September 24th, 2008 11:13 pm

    Thank you. You have helped someone more than you could know.

    Sohbet Konya chat

  26. sewimsiz / September 24th, 2008 11:38 pm

    i definately loved this thank you for the post it was very informative

    sohbet | sohbet

  27. sevgiradyo / September 28th, 2008 4:31 am

    thanks

  28. polemik / September 28th, 2008 4:48 am

    Thank you.

  29. vmatex / October 1st, 2008 5:02 pm

    forma, atki, mont, yelek, isci elbisesi Polar

  30. radyo dinle / November 27th, 2008 10:42 pm

    Thanxs for this sharing with us

  31. Estetik / December 5th, 2008 1:22 am

    You may enjoy writing an article about Bagdasarian Productions, a company that makes claims to have a trademark over the letter "A" of the alphabet, in any shape, color or form.

  32. saglik / December 28th, 2008 2:08 pm

    Saçlarınızın sağlığı için yılanyağı kullanın.
    Bebeğiniz sağlıklı olsun
    Önce sağlık diyenler

  33. perde / January 14th, 2009 8:58 pm

    good

  34. Denizli Rehberi / January 29th, 2009 9:10 am

    Denizli Rehber
    Thanks for this article

  35. burun estetiği / March 26th, 2009 1:21 am

    Also, I too am getting the warning that yutaka is along with a few other similar ones about “Group”, “Link”, “Entry”, etc. It doesn’t seem to be breaking anything, just throwing a warning.

  36. evden eve nakliye / April 5th, 2009 11:48 am

    thanks

  37. Sohbet / April 13th, 2009 10:30 am

    thank you.

  38. Gebäudereinigung München / April 18th, 2009 2:19 pm

    Thanks Pixel Breaker

  39. radyo dinle / April 23rd, 2009 6:18 am

    thanks pixel breaker for sharing.

  40. radyo / May 1st, 2009 2:04 am

    Thanks so much.

  41. Dekorasyon / May 14th, 2009 4:46 pm

    I wonder what affect this will have on Googlebot visiting the site - will it interpret the body{display:none} as it being a blank page

  42. pier / May 26th, 2009 10:39 am

    OKEY

  43. Özel Hastaneler İstanbul / June 5th, 2009 2:44 pm

    Great information! Very useful for me. Thanks a lot.
    The idea is awesome. Congrats.

  44. atkı imalat / June 13th, 2009 10:51 pm

    promosyon tekstil ürünleri imalatı yapan firma

  45. promosyon tekstil imalat / June 13th, 2009 10:54 pm

    promosyon tekstil ürünleri imalatı yapan firma

  46. neon / June 17th, 2009 5:51 pm

    Your site is very easy in terms of expression and open. I think everyone who enters your site is very gratifying, but also sharing a very nice opportunity to give ...

  47. atkı imalat / June 18th, 2009 10:09 pm

    promosyon etkinlikler ve taraftar gurupları için atkı imalatı yapan firma. polar atkı, dokuma atkı, triko atkı, taraftar atkısı

  48. neon tabela / June 20th, 2009 9:34 pm

    süper site..Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.

  49. çanta / June 26th, 2009 11:52 am

    Güzel olmuşş ellerinize sağlık;)

  50. nasi / July 1st, 2009 9:57 pm

    Thanks
    Kral Fm
    KralFm

Leave a reply

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