Thursday 1 December 2011

The JIT compiler and constructors

One of the biggest optimisations I did to Bug Tunnel Defense was also the easiest.

ActionScript 3 includes a number of improvements to the language, but perhaps more significantly includes a new runtime, AVM2, which incudes a just in time (JIT) compiler. This speeds up all code significantly but with one notable omission. It doesn't speed up constructors.


So one simple optimisation is to make sure no significant code is contained in constructors. Simple as all that's needed is another function, called by the constructor, to handle the actual work that needs doing. I call all such functions Make, as seen in the following very typical code.


function Magic(owner:PlayScreen) {
  screen = owner;
 Make();
}

function Make():void {
 gGame  = screen.gGame;
 gGame.gMagic = this;
 abAvailable = new Vector.<Boolean>(eSpellNum, true);
 afCosts  = new Vector.<Number>(eSpellNum, true);
}



When I did this optimisation I saw a large speed up in particular to level start time which did significant initialisation in constructors. Now I do this as a matter of course for all classes, even though it probably only makes a difference for some of them.


This information is from the following presentation (page 43), which contains many other tips for writing efficient ActionScript.


No comments:

Post a Comment