You are on page 1of 17

ActionScript 3 Performance Tuning

Ted Patrick
Flex Evangelist Adobe Systems

2006 Adobe Systems Incorporated. All Rights Reserved.

Overview

Premature Optimization How Flash Player and ActionScript work Strong Typing - variables, events, return types Measuring Performance

2006 Adobe Systems Incorporated. All Rights Reserved.

Premature Optimization More Computing sins are committed in the name of e ciency than any other reason including blind stupidity W.A. Wulf Premature Optimization is the root of all evil. - Hoare and Knuth Bottlenecks occur in surprising places, so dont try to second guess and put in a speed hack until you have proven thats where the bottleneck is. - Rob Pike

2006 Adobe Systems Incorporated. All Rights Reserved.

How Flash Player works One Frame Cycle

ActionScript Execution

Graphics Rendering

ActionScript Frame & Network Events


2006 Adobe Systems Incorporated. All Rights Reserved. 4

AVM2 Architecture

2006 Adobe Systems Incorporated. All Rights Reserved.

How Flash Player works


Flash Player presents a single thread of execution Flash Player is multi-threaded!!! Rendering and ActionScript are executed sequentially. The player will do everything you ask it to do. Do to much in one frame cycle a script-timeout occurs. Defer AS execution to next frame cycle with:
mx.core.UIComponent.callLater()

2006 Adobe Systems Incorporated. All Rights Reserved.

Typing optimizes bytecode!

Typing optimizes bytecode! Typing optimizes bytecode! Typing optimizes bytecode!

2006 Adobe Systems Incorporated. All Rights Reserved.

Typing optimizes bytecode!


Typing a ects member access. Two Performance Cases for member access

Strong references are equivalent to C++ member access


Must use dot operator for member Must use strong typing No typing Use of [member] access

Weak references are a hash lookup


2006 Adobe Systems Incorporated. All Rights Reserved.

Member Access Examples

instance.x instance[3] This stu happens a lot!!!

2006 Adobe Systems Incorporated. All Rights Reserved.

Typing Examples

function a ( o ) { return o; } function b ( o ) { return o.x; } function c ( o:Object ) { return o.x; }

// RETURN WEAK // WEAK // RETURN WEAK // WEAK // RETURN WEAK // WEAK

2006 Adobe Systems Incorporated. All Rights Reserved.

10

Typing Examples
class Base extends Object { var x:int; // STRONG var y:int; // STRONG var z; // WEAK } function a ( o:Base ) { return o.z; } function b ( o:Base ):int { return o.x; } // RETURN WEAK // WEAK // RETURN STRONG // STRONG

2006 Adobe Systems Incorporated. All Rights Reserved.

11

Typing Examples

dynamic class Base extends Object { var x:int; // STRONG var y:int; // STRONG } function a ( o:Base ) { return o.z; } function a ( o:Base ):int { return o.x; } // RETURN WEAK // WEAK // RETURN STRONG // STRONG

2006 Adobe Systems Incorporated. All Rights Reserved.

12

Typing Examples
dynamic class Base extends Object { var x:int; // STRONG } class Example extends Base { var z:int //STRONG } function a ( o:Example ):int { return o.y; } function a ( o:Example ):int { return o.x; } // RETURN STRONG // WEAK // RETURN STRONG // STRONG

2006 Adobe Systems Incorporated. All Rights Reserved.

13

Array Member Access Arrays include fast path for dense portion Member access is as fast as C++ in dense portion var a:Array = [1,2,3,4,5]; a[1000] = 2010; a[1001] = 2011; a[2]; a[1000];

//FAST PATH //SLOW PATH

Demo

2006 Adobe Systems Incorporated. All Rights Reserved.

14

Typing with int, uint, Number


Avoid implicit type conversion Math operations can induce type conversion Sometimes just using Number is more e cient.

2006 Adobe Systems Incorporated. All Rights Reserved.

15

Strong Typing improves productivity


Simpli ed debugging Compiler will throw compile-time errors Code hinting in Flex Builder is based on Strong Typing

2006 Adobe Systems Incorporated. All Rights Reserved.

16

Contact Info

Ted Patrick Flex Evangelist http://www.on ex.org ted@adobe.com

2006 Adobe Systems Incorporated. All Rights Reserved.

17

You might also like