views:

71

answers:

1

Hi,

I'm new to Actionscript (v3), 20 years C++ though, and I'm just trying to get my head around some of the performance caveats I'm reading.

I checked out this site:

http://www.nbilyk.com/optimizing-actionscript-3

and was scared to death of the 'code' section saying that Actionscript has to do a dynamic lookup when trying find static methods in a package. The point being that it is inefficient to put your util-type functions (typically math functions) in their own package as static functions, because it has to lookup the namespace and method dynamically each time you call it.

Is this true? It's just so not what I expected, coming from C++, and it really changes how I structure my code and libraries. I wonder if it's better to have a singleton util library that is new()'d once, containing the methods as normal public methods, than have them static in another package.

Are there any other obvious gotchas like this?

Cheers,

Shane

+2  A: 

Yes its true. Calling static functions from outside the class is slow. I recently tested this for myself, over 10,000 iterations it was 220ms for static vs 160ms for calling the function on an instance.

Other tips would be to avoid the Adobe compiler. I have recently started using HaXe which converts to LLVM and then to ABC code (actionscript byte code). There are some hidden opcodes for dealing with memory that HaXe takes advantage of (same as Alchemy).

Speaking of alchemy it will compile C++ --> LLVM --> ABC. Might be useful for you if you have lots of c++ libraries. (someone ported Doom, Hexen, and Heritic over to Flash this way)

EDIT: for anything performance related http://lab.polygonal.de/ and http://blog.joa-ebert.com are good places to start.

Allan
good resources. Not sure, but this one might come in handy too: Grant Skinner's talk on optimization: http://gskinner.com/talks/quick/ it has some interesting comparisons
George Profenza
Thanks for the resources. I came across HaXe the other day, and it looked pretty cool, but I just didn't want to have to learn yet another language. Are many developers using it?
Shane
Hard to say but my feeling is that HaXe is more of a niche at the moment. That being said its the more experienced flash devs that are turning towards it as they tend to be the ones seeking to gain more performance and demand more from a language (inlining, generics etc). The language is *very* similar to AS3 so it is easy to learn. A good way to use it is to write any performance type libraries in HaXe and compile it to SWC (a precompiled library). Then the SWC can be imported into any old AS3 project.
Allan
Ah, good idea. Thanks Allan. :)
Shane