Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

actionscript 3 - What are the major performance hitters in AS3 aside from rendering vectors?

In ActionScript 3, using vector graphics is a guaranteed way to cause massive damage to the performance of your project.

Using a single Bitmap for all graphics by using .copyPixels() through its BitmapData object 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Documents I've found helpful are:

Some highlights:

Choose appropriate display objects

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.

Use getSize() to benchmark code

getSize() returns the size in memory of a specified object.

Choose appropriate primitive types to conserve memory

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.

Reuse objects

Optimize memory by reusing objects and avoid recreating them whenever possible.

Use object pooling

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.

Free memory

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.

BitmapData class includes a dispose() method, although the dispose method removes the pixels from memory, the reference must still be set to null to release it completely.

Use bitmaps

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.

Avoid filters, including filters processed through Pixel Bender

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 to scale large images

Use mipmapping sparingly. Although it improves the quality of downscaled bitmaps, it has an impact on bandwidth, memory, and speed.

Use Text Engine for read-only text, TextField for input text

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 word-wrap.

Use callbacks instead of events

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_FRAME event, 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.

Freeze and unfreeze objects on added / removed from stage

Even if display objects are no longer in the display list and are waiting to be garbage collected, they could still be using CPU-intensive code.

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.

Use Event.ACTIVATE and Event.DEACTIVATE events to detect background inactivity

Event.ACTIVATE and Event.DEACTIVATE events allow you to detect when the runtime gains or loses focus. As a result, code can be optimized to react to context changes.

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.

Disable mouse interaction when possible

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.

Use Timers for non-animated content

Timers are preferred over Event.ENTER_FRAME events for non-animated content that executes for a long time.

A timer can behave in a similar way to an Event.ENTER_FRAME event, 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 tweening

Limit the use of tweening, which saves CPU processing, memory, and battery life helping content run faster on low-tier devices.

Use Vector vs. Array

The Vector class allows faster read and write access than the <a href="http://help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...