In ActionScript 3, using vector graphics is a guaranteed way to cause massive damage to the performance of your project.
Using a single
Bitmapfor all graphics by using
BitmapDataobject in place of all vector graphics will yield a ridiculous performance boost and is essential for people like myself developing games within Flash.
Beyond this I’m not really sure what the next major things that I should be targeting and attempting to optimize are. I do use a lot of the inbuilt trigonometry functions, but they don’t seem to affect it all that much. I know there are some libraries that optimize mathematics with approximation methods and similar, but so far I haven’t found these necessary.
Are there any other massive known points I should be looking at? I’m more referring to the inbuilt things that I should be careful of (like avoiding vector rendering) rather than how to improve my own coding style.
Documents I’ve found helpful are:
- Optimizing Performance for the Adobe Flash Platform
- ActionScript 3.0 and AVM2 Performance Tuning by Gary Grossman
- Building High Performance iPhone Applications by Mike Chambers
One of the most simple optimization tips to limit memory usage is to
use the appropriate type of display object. For simple shapes that are
not interactive, use Shape objects. For interactive objects that don’t
need a timeline, use Sprite objects. For animation that uses a
timeline, use MovieClip objects.
getSize()returns the size in memory of a specified object.
All primitive types except String use 4 – 8 bytes in memory. A
Number, which represents a 64-bit value, is allocated 8 bytes by the
ActionScript Virtual Machine (AVM), if it is not assigned a value.
The behavior differs for the String type. Benchmark code and
determine the most efficient object for the task.
Optimize memory by reusing objects and avoid recreating them whenever
Reusing objects reduces the need to instantiate objects, which can be
expensive. It also reduces the chances of the garbage collector
running, which can slow down your application.
To make sure that an object is garbage collected, delete all
references to the object. Memory allocation, rather than object
deletion, triggers garbage collection. Try to limit garbage
collection passes by reusing objects as much as possible. Also, set
references to null, when possible, so that the garbage collector
spends less processing time finding the objects. Think of garbage
collection as insurance, and always manage object lifetimes
explicitly, when possible.
Setting a reference to a display object to null does not ensure that
the object is frozen. The object continues consume CPU cycles until it
is garbage collected.
Using vectors, especially in large numbers, dramatically increases the
need for CPU or GPU resources. Using bitmaps is a good way to optimize
rendering, because the runtime needs fewer processing resources to
draw pixels on the screen than to render vector content.
When a filter is applied to a display object, the runtime creates two
bitmaps in memory. Using externally authored bitmaps helps the
runtime to reduce the CPU or GPU load.
Use mipmapping sparingly. Although it improves the quality of
downscaled bitmaps, it has an impact on bandwidth, memory, and speed.
For read-only text, it’s best to use the Flash Text Engine, which
offers low memory usage and better rendering. For input text,
TextField objects are a better choice, because less ActionScript code
is required to create typical behaviors, such as input handling and
Using the native event model can be slower and consume more memory
than using a traditional callback function. Event objects must be
created and allocated in memory, which creates a performance slowdown.
For example, when listening to the
Event.ENTER_FRAMEevent, a new
event object is created on each frame for the event handler.
Performance can be especially slow for display objects, due to the
capture and bubbling phases, which can be expensive if the display
list is complex.
Even if display objects are no longer in the display list and are
waiting to be garbage collected, they could still be using
The concept of freezing is also important when loading remote content
with the Loader class.
unloadAndStop()method allows you to unload a SWF file,
automatically freeze every object in the loaded SWF file, and force
the garbage collector to run.
The activate and deactivate events allow you to implement a similar
mechanism to the “Pause and Resume” feature sometimes found on mobile
devices and Netbooks.
Detecting mouse interaction can be CPU-intensive when many interactive
objects are shown onscreen, especially if they overlap. When
possible, consider disabling mouse interaction, which helps your
application to use less CPU processing, and as a result, reduce
battery usage on mobile devices.
Timers are preferred over
Event.ENTER_FRAMEevents for non-animated
content that executes for a long time.
A timer can behave in a similar way to an
Event.ENTER_FRAMEevent, but an
event can be dispatched without being tied to the frame rate. This
behavior can offer some significant optimization. Consider a video
player application as an example. In this case, you do not need to use
a high frame rate, because only the application controls are moving.
Limit the use of tweening, which saves CPU processing, memory, and
battery life helping content run faster on low-tier devices.
Array element access and iteration are much faster when using a Vector
instance than they are when using an Array.
In strict mode the compiler can identify data type errors.
Runtime range checking (or fixed-length checking) increases
reliability significantly over Arrays.
Taking advantage of the bubbling of an event can help you to optimize
ActionScript code execution time. You can register an event handler on
one object, instead of multiple objects, to improve performance.
When painting pixels, some simple optimizations can be made just by
using the appropriate methods of the BitmapData class. A fast way to
paint pixels is to use the
unlock()prevents the screen from being updated
unnecessarily. Methods that iterate over pixels, such as
setPixel32(), are likely to be slow,
especially on mobile devices. If possible, use methods that retrieve
all the pixels in one call. For reading pixels, use the
method, which is faster than the
getPixels()method. Also, remember to
use APIs that rely on Vector objects, when possible, as they are
likely to run faster.
When a String class method is available, it runs faster than the
equivalent regular expression and does not require the creation of
appendText()method provides performance improvements.
Using the square bracket operator can slow down performance. You can
avoid using it by storing your reference in a local variable.
Calling functions can be expensive. Try to reduce the number of
function calls by moving code inline.
Moving the function call inline results in code that is more than four
Even if the off-stage elements are not shown onscreen and are not
rendered, they still exist on the display list. The runtime continues
to run internal tests on these elements to make sure that they are
still off-stage and the user is not interacting with them.
When a display object uses alpha blending, the runtime must combine
the color values of every stacked display object and the background
color to determine the final color. Thus, alpha blending can be more
processor-intensive than drawing an opaque color. This extra
computation can hurt performance on slow devices.
A higher frame rate expends more CPU cycles and energy from the
battery than a lower rate.
This feature caches a vector object, renders it as a bitmap
internally, and uses that bitmap for rendering. Bitmap caching
improves rendering if the cached content is not rotated, scaled, or
changed on each frame. Any transformation other than translation on
the x- and y-axes, rendering is not improved.
cacheAsBitmapMatrixin the AIR mobile profile you can apply any
two-dimensional transformation to the object without regenerating the
cached bitmap. You can also change the alpha property without
regenerating the cached bitmap.
Using only a single cached bitmap is used in memory and shared by all
This technique saves CPU resources.
Use the bitmap caching feature and the
opaqueBackground property to improve text rendering performance
The bitmap caching feature allows you to cache vector content as
bitmaps to improve rendering performance. This feature is helpful for
complex vector content and also when used with text content that
requires processing to be rendered.
Alpha transparency places an additional burden on the runtime when
drawing transparent bitmap images. You can use the
opaqueBackgroundproperty to bypass that, by specifying a
color as a background.
In order to leverage GPU acceleration of Flash content with AIR for
mobile platforms, Adobe recommends that you use renderMode=”direct”
(that is, Stage3D) rather than renderMode=”gpu”. Adobe officially
supports and recommends the following Stage3D based frameworks:
Starling (2D) and Away3D (3D).
Avoid using wmode=transparent or wmode=opaque in HTML embed
parameters. These modes can result in decreased performance. They can
also result in a small loss in audio-video synchronization in both
software and hardware rendering. Furthermore, many platforms do not
support GPU rendering when these modes are in effect, significantly
Application code in the current execution thread continues executing.
Asynchronous operations are scheduled and divided to avoid rendering
issues. Consequently, it is much easier to have a responsive
application using asynchronous versions of operations. See Perceived
performance versus actual performance for more information.
Unlike bitmaps, rendering vector content requires many calculations,
especially for gradients and complex paths that contain many control
points. As a designer or developer, make sure that shapes are
Cache assets locally after loading them, instead of loading them from the network each time they’re needed
If your application loads assets such as media or data, cache the
assets by saving them to the local device. For assets that change
infrequently, consider updating the cache at intervals.
Use the StageVideo class to take advantage of hardware acceleration to
This approach takes full advantage of the underlying video hardware.
The result is a much lower load on the CPU, which translates into
higher frame rates on less powerful devices and also less memory
The AAC audio format offers better quality and smaller file size than the mp3 format at an equivalent bit rate
Similar to video decoding, audio decoding requires high CPU cycles and
can be optimized by leveraging available hardware on the device.
The AAC format offers better quality and smaller file size than the
mp3 format at an equivalent bitrate.
Initialization function such as constructors are interpreted,
everything else is JIT.