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();
50 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
Very good site. Thanks
hi, good jop so thanks.
nice howto i test this on my wp homepage thanx
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.
I miss you, we'll have to go for a pint soon
Thanks :p
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"
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....
}
Thank you. You have helped someone more than you could know.
Sohbet Konya chat
i definately loved this thank you for the post it was very informative
sohbet | sohbet
thanks
Thank you.
forma, atki, mont, yelek, isci elbisesi Polar
Thanxs for this sharing with us
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.
Saçlarınızın sağlığı için yılanyağı kullanın.
Bebeğiniz sağlıklı olsun
Önce sağlık diyenler
good
Denizli Rehber
Thanks for this article
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.
thanks
thank you.
Thanks Pixel Breaker
thanks pixel breaker for sharing.
Thanks so much.
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
OKEY
Great information! Very useful for me. Thanks a lot.
The idea is awesome. Congrats.
promosyon tekstil ürünleri imalatı yapan firma
promosyon tekstil ürünleri imalatı yapan firma
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 ...
promosyon etkinlikler ve taraftar gurupları için atkı imalatı yapan firma. polar atkı, dokuma atkı, triko atkı, taraftar atkısı
süper site..Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.
Güzel olmuşş ellerinize sağlık;)
Thanks
Kral Fm
KralFm