Archive for the 'Tips' Category
AS3.0 Memory monitoring
I just came across the property System.totalMemory. It's very useful, especially for the engine I am currently working on, and dealing with AS3 garbage collection.
It returns the memory currently being used by the Flash Player. It's in bytes so it pays to clean it up slightly.
-
var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb';
-
trace( mem ); // eg traces "24.94Mb"
It seems this takes account of all instances of the flash player. For example, I have a logger/debug panel running in the sidebar of firefox, built in flex. As soon as I open this and it's initialised, it uses an extra 5 - 10mb of ram. So be warned, if you see a massive increase in memory usage, check you don't have loads of sites open with ad banners or other flash based stuff.
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.
AS3.0 Better Singletons
I've found a better more robust way of enforcing singletons in AS3.0
-
package
-
{
-
public class Singleton
-
{
-
public static var instance:Singleton;
-
-
public static function getInstance():Singleton
-
{
-
if( instance == null ) instance = new Singleton( new SingletonEnforcer() );
-
return instance;
-
}
-
-
public function Singleton( pvt:SingletonEnforcer )
-
{
-
// init class
-
}
-
}
-
}
-
-
internal class SingletonEnforcer{}
Note: The class "SingletonEnforcer" is actually placed within the same .as file as the Singleton class, but placed outside the package{} parenthesis, therefore, the class "SingletonEnforcer" can only be accessed from within this .as file, so if the Singleton's constructor is called from anywhere else, you'll get an error.
AS3.0 Reversed for loop
Today I came across an easy mistake to make when new to AS3.
I set up a for loop, to count backwards through an Array. But for some strange reason the loop was never terminating! It was simple, here's the original code
-
var i:uint;
-
for( i=arr.length-1; i>=0; i-- )
-
{
-
}
The mistake was simple, I had set i as a uint, so it could never go below 0, so just had to make i an int. It sounds stupid once you realise it, but it wasted enough of my time!
Looping within a range
Something I have to do very often is looping a number within a range. This could be for a carousel navigation, a rotating banner or image slideshow, etc. The concept is simple, have a range, say 1 - 20, and simply call next() / previous() on it and return the new number, if it goes above 20, return to 1, if below 1, return to 20.
Read more
RSS/Atom syndication made easy
A while ago adobe released their as3syndicationlib for easy access to RSS & Atom feeds by converting either RSS 1.0/2.0 or Atom feeds into generic data accessible via the IFeed interface. These libraries also require adobe's corelib.
It literally took me 5 minutes to write a simple wrapper class to handle the loading of the feeds, and simply trace out the headlines of each item.
Read more
AS3.0 Abstracts
Another thing I needed was a strict abstract class in AS3, again, public only constructors, and the absence of the "abstract" keyword from ActionScript meant a workaround.
-
package
-
{
-
import flash.utils.getDefinitionByName;
-
import flash.utils.getQualifiedClassName;
-
-
public class AbstractExample
-
{
-
public function AbstractExample()
-
{
-
if( Class( getDefinitionByName( getQualifiedClassName( this ) ) ) == AbstractExample ) throw new Error( 'AbstractExample must be extended' );
-
-
// rest of constructor here...
-
}
-
}
-
}
I like this solution best as it doesn't rely on strings for comparison as in Tink's example. His method is shorter, and easier for typing. It's down to personal preference really.
AS3.0 Singletons
While I've been getting up to speed with my AS3 over the past few months, there have been a few things which are really useful to know and took a bit of digging about to find.
As class constructors in ActionScript 3.0 must be public, there's not a really clean way to force a class to be a singleton as there is in ActionScript 2.0 or Java say.
-
package
-
{
-
public class Singleton
-
{
-
public static var instance:Singleton;
-
-
public static function getInstance():Singleton
-
{
-
if( instance == null ) instance = new Singleton( getInstance );
-
return instance;
-
}
-
-
public function Singleton( caller:Function )
-
{
-
if( caller == Singleton.getInstance )
-
{
-
// instantiate class here
-
}
-
else
-
{
-
throw new Error( 'Singleton is a singleton, use getInstance();' );
-
}
-
}
-
}
-
}
It's pretty simple, you can't call new Singleton() because the compiler will be expecting an argument, and if you do pass an argument, it will throw an Error because the caller is not the Singleton.getInstance method.
So you can only access this class by calling Singleton.getInstance().