Top 10 Performance Tips for 2012
It’s a new year and it’s time to make some New Years resolutions for Flash performance. Today’s article is a collection of what i consider 10 top tips for improving the performance of your Flash apps. Read on for the list!
- Use native code instead of AS3
Generally, it’s better to let Flash Player’s native code do the heavy lifting rather than using AS3. The reason for this is that the VM that AS3 code runs in necessarily has some overhead to it compared to the functions you can call in the Flash Player API (everything in the
flash.*packages). One exception to this rule is when the API does something you want to avoid such as allocate memory.
- Eliminate allocations to reduce GC
In addition to the allocations that you expect—such as those that you trigger by using the
newoperator—there are tons of hidden allocations such as
Stringobjects from concatenation, objects the Flash Player creates such as
Events. These allocations are slow and collecting them when you’re done with them is even slower, so try to get rid of them.
- Reuse objects to reduce GC
When you’re done with objects, Flash Player’s garbage collector will reclaim their memory for reuse later. Unfortunately, this process is very slow and it’s hard to control when it will happen. Instead of making new objects, consider reusing existing ones. One technique that can help with this is called free lists.
- Don’t use anything dynamic
That includes dynamic functions (e.g. anonymous functions and functions declared as local variables), objects such as
operator for accessing fields, and untyped (
*) variables. All of these are much slower than their static equivalents such as regular methods, non-dynamic classes, the dot operator, and typed variables.
- Offload to the GPU
These days in Flash you have the ability to use video cards’ GPUs in addition to the main CPU. Using a combination of these is the key to high performance with 3D graphics (
Stage3D) and high-definition video (
- Reduce function calls
Function calls are, very unfortunately, quite slow in AS3. This includes getters and setters, which occur all the time (e.g.
Array.length). Try caching the results of functions instead of calling them more than once, especially with getters. In extreme cases, manually inline the body of functions into one larger function.
- Use speciality functions and classes rather than general purpose ones
- Use fewer static accesses
Accessing static variables, constants, and functions is slower than accessing their non-static counterparts. Consider using non-static alternatives or caching those static accesses as either non-static variables/constants or local variables/constants.
- Prefer local variables to fields
Reading and writing class and object variables (a.k.a. fields, member variables) is much slower than accessing local variables. Cache field accesses as local variables when you use them a lot.
- Eliminate pointless code
It’s common to see variables initialized to their default value, which slows down the creation of objects and the execution time of functions. Get rid of this code that does nothing as a matter of good habit and the performance advantages will add up across your entire app.
Stay tuned for more articles with even more Flash performance tips.