Вы находитесь на странице: 1из 74

ActionScript 3.

0 and
AVM2:
Performance Tuning
Gary Grossman
Adobe Systems

1
2006 Adobe Systems Incorporated. All Rights Reserved.
Agenda

ƒ Two goals:
ƒ Cover some techniques that can help performance
ƒ Pop the hood and talk about how the new VM works

2
2006 Adobe Systems Incorporated. All Rights Reserved.
Classes and
Type Annotations

3
2006 Adobe Systems Incorporated. All Rights Reserved.
Runtime natively supports classes

class A
{
var a:Number = 3.14;
var b:String = “a string”;
var c:int = -1;
public function A()
{
trace(“Constructor”);
}
public function method()
{
trace(“A.method”);
}
}

4
2006 Adobe Systems Incorporated. All Rights Reserved.
What that compiles to in AS2...

_global.A = function ()
{
this.a = 3.14;
this.b = “a string”;
this.c = -1;
trace(“Constructor”);
}
_global.A.prototype.method = function ()
{
trace(“A.method”);
}

5
2006 Adobe Systems Incorporated. All Rights Reserved.
Atoms

ƒ Atoms are the most primitive value in the AS1/AS2 system – a single
dynamically typed value
ƒ Atoms still exist in AS3, but only when type is unknown

null null
undefined undefined
number 3.14159
string “Hello, world”
boolean true
object

6
2006 Adobe Systems Incorporated. All Rights Reserved.
AS3 Object Model: Traits

7
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits represent objects: a sample class

class Shape
{
var id:int;
var name:String;
}
class Circle extends Shape {
var radius:Number;
var color:uint;

public function Circle(radius:Number)


{
this.radius = radius;
}

public function area():Number


{
return Math.PI*radius*radius;
}
}

8
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits describe objects

class Shape
{
var id:int;
var name:String;
}

class Circle extends Shape {


traits for class Shape
var radius:Number; base class: Object final: false dynamic: false
var color:uint;
methods
public function Circle(radius:Number)
{ name method id type return type params
this.radius = radius; $construct 0 final Void (none)
}

public function area():Number


{ properties
return Math.PI*radius*radius; name type offset
}
id int 12
}
name String 16

9
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits describe objects

class Shape
{
var id:int;
var name:String;
} traits for class Circle
class Circle extends Shape { base class: Shape final: false dynamic: false
var radius:Number;
var color:uint;
methods
public function Circle(radius:Number) name method id type return type params
{
$construct 0 final Void radius:Number
this.radius = radius;
} area 1 virtual Number (none)

public function area():Number


{
return Math.PI*radius*radius; properties
}
name type offset
}
id int 12
name String 16
radius Number 24
color uint 20

10
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits describe objects

traits for class Circle instance of class Circle


0 vtable
base class: Shape final: false dynamic: false
4 traits
methods
8 __proto__
name method id type return type params
12 id
$construct 0 final Void radius:Number
16 name
area 1 virtual Number (none)
20 color

24 radius
properties
name type offset
id int 12
name String 16
radius Number 24
color uint 20

11
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits describe objects

traits for class Circle instance of class Circle


0 vtable
base class: Shape final: false dynamic: false
4 traits
methods
8 __proto__
name method id type return type params
12 id
$construct 0 final Void radius:Number
16 name
area 1 virtual Number (none)
20 color

24 radius
properties
name type offset
id int 12
name String 16
radius Number 24
color uint 20

12
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits describe objects

traits for class Circle instance of class Circle


0 vtable
base class: Shape final: false dynamic: false
4 traits
methods
8 __proto__
name method id type return type params
12 id
$construct 0 final Void radius:Number
16 name
area 1 virtual Number (none)
20 color

24 radius
properties
name type offset
id int 12
name String 16
radius Number 24
color uint 20

13
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits describe objects

traits for class Circle instance of class Circle


0 vtable
base class: Shape final: false dynamic: false
4 traits
methods
8 __proto__
name method id type return type params
12 id
$construct 0 final Void radius:Number
16 name
area 1 virtual Number (none)
20 color

24 radius
properties
name type offset
id int 12
name String 16
radius Number 24
color uint 20

14
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits describe objects

traits for class Circle instance of class Circle


0 vtable
base class: Shape final: false dynamic: false
4 traits
methods
8 __proto__
name method id type return type params
12 id
$construct 0 final Void radius:Number
16 name
area 1 virtual Number (none)
20 color

24 radius
properties
name type offset
id int 12
name String 16
radius Number 24
color uint 20

15
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits describe objects

traits for class Circle instance of class Circle


0 vtable
base class: Shape final: false dynamic: false
4 traits
methods
8 __proto__
name method id type return type params
12 id
$construct 0 final Void radius:Number
16 name
area 1 virtual Number (none)
20 color

24 radius
properties
name type offset
id int 12
name String 16
radius Number 24
color uint 20

16
2006 Adobe Systems Incorporated. All Rights Reserved.
How traits describe objects

traits for class Circle instance of class Circle


0 vtable
base class: Shape final: false dynamic: false
4 traits
methods
8 __proto__
name method id type return type params
12 id
$construct 0 final Void radius:Number
16 name
area 1 virtual Number (none)
20 color

24 radius
properties
name type offset
id int 12
name String 16
radius Number 24
color uint 20
total 32 bytes / instance

17
2006 Adobe Systems Incorporated. All Rights Reserved.
Objects in AVM1

4
id
8 0
000
12

16
color
20 0xFF0000
000
instance of class Circle 24

0 vtable 28

4 __proto__ 32 radius
8 flags 36
001
12 call 40 2.7
16 variables 44

20 ... 48 name
name
52
010
56 MyCircle
60

18
2006 Adobe Systems Incorporated. All Rights Reserved.
Runtime natively supports strong types

ƒ In ActionScript 2.0:
ƒ Type annotations were a compiler hint
ƒ Type information did not reach all the way down to the runtime
ƒ All values were stored as dynamically typed atoms
ƒ Type annotations were a “best practice” for developer productivity

ƒ In ActionScript 3.0:
ƒ Type annotations are employed to efficiently store values as native machine
types
ƒ Type annotations improve performance and reduce memory consumption
ƒ Type annotations are essential to getting best performance and memory
characteristics

19
2006 Adobe Systems Incorporated. All Rights Reserved.
The Power of “int”

20
2006 Adobe Systems Incorporated. All Rights Reserved.
Numeric Types

ƒ int: 32-bit signed integer


ƒ uint: 32-bit unsigned integer
ƒ Number: 64-bit IEEE 754 double-precision floating-point number

21
2006 Adobe Systems Incorporated. All Rights Reserved.
Without Type Annotations

var x = -1;
int atom,
4 bytes 111111111111111111111 110

var y = 0xFFFFFFFF;
number atom,
111
4 bytes
number,
8 bytes 0xFFFFFFFF

var z = 3.14159;
number atom,
4 bytes 111

number,
8 bytes 3.14159

22
2006 Adobe Systems Incorporated. All Rights Reserved.
With Type Annotations

var x:int = -1;


4 bytes
-1

var y:uint = 0xFFFFFFFF;


4 bytes 0xFFFFFFFF

var z:Number = 3.14159;


8 bytes 3.14159

23
2006 Adobe Systems Incorporated. All Rights Reserved.
Promotion of
Numeric Types

24
2006 Adobe Systems Incorporated. All Rights Reserved.
Promotion of Numeric Types

ƒ The semantics of ECMAScript require that ints often be promoted to Number

var i:int = 1;

// i+1 here will be a straight


// integer addition
var j:int = i+1

// i+1 here will require


// promotion to Number
print(i+1)

25
2006 Adobe Systems Incorporated. All Rights Reserved.
Promotion of Numeric Types

ƒ Putting in a coerce to int/uint can help performance, if the compiler cannot


infer that int/uint is what you want
ƒ Array access has fast paths for int/uint, so coercion of index can help
performance

var i:int;
// i*2 gets promoted to Number
for (i=0; i<10000; i++) {
a[i*2] = 0;
}
// Goes through fast path
for (i=0; i<10000; i++) {
a[int(i*2+1)] = 1;
}
26
2006 Adobe Systems Incorporated. All Rights Reserved.
CSE

27
2006 Adobe Systems Incorporated. All Rights Reserved.
CSE

ƒ The VM does perform common subexpression elimination


ƒ However, language semantics sometimes get in the way:

for (var i:int=0; i<a.length; i++)


{
processRecord(a[i]);
}

ƒ Because “length” might be overridden and have side effects, the VM cannot
factor it out of the loop

28
2006 Adobe Systems Incorporated. All Rights Reserved.
CSE

ƒ The VM does perform common subexpression elimination


ƒ However, language semantics sometimes get in the way:

for (var i:int=0; i<a.length; i++)


{
processRecord(a[i]);
}

ƒ Because “length” might be overridden and have side effects, the VM cannot
factor it out of the loop

29
2006 Adobe Systems Incorporated. All Rights Reserved.
CSE

ƒ So, some hand CSE is still needed:

var n:int = a.length;


for (var i:int=0; i<n; i++)
{
processRecord(a[i]);
}

30
2006 Adobe Systems Incorporated. All Rights Reserved.
Method Closures

31
2006 Adobe Systems Incorporated. All Rights Reserved.
Method Closures

ƒ Often, developers write event handling code with anonymous function


closures:

class Form
{
function setupEvents()
{
var f = function(event:Event) {
trace(“my handler”);
}
grid.addEventListener(“click”, f);
}
}

32
2006 Adobe Systems Incorporated. All Rights Reserved.
Method Closures

ƒ Nested functions cause the outer function to create an activation object.


ƒ This has some performance and memory impact.
class Form
{
function setupEvents()
{
var f = function(event:Event) {
trace(“my handler”);
}
grid.addEventListener(“click”, f);
}
}

33
2006 Adobe Systems Incorporated. All Rights Reserved.
Method Closures

ƒ Method closures solve the age-old AS2 problem of “this” changing


ƒ Eliminates need for mx.utils.Delegate class from Flex 1.x
import mx.utils.Delegate;
class Form
{
function setupEvents()
{
grid.addEventListener(“click”,
Delegate.create(this, f)); // No more!
}
function f(e)
{
trace(“my handler”);
}
}

34
2006 Adobe Systems Incorporated. All Rights Reserved.
Method Closures

ƒ Method closures are convenient to use, and more efficient, because there
won’t be an activation object created.

class Form
{
function setupEvents()
{
grid.addEventListener(“click”, f);
}
function f(event:Event)
{
trace(“my handler”);
}
}

35
2006 Adobe Systems Incorporated. All Rights Reserved.
Activation Objects

f: function f()
newactivation {
setlocal1 var x:int = 0;
getlocal1 …
pushbyte 0 }
setslot 0

g:
pushbyte 0
setlocal1

36
2006 Adobe Systems Incorporated. All Rights Reserved.
Compound Strings

37
2006 Adobe Systems Incorporated. All Rights Reserved.
Compound Strings

ƒ For awhile, we had a class flash.utils.StringBuilder for fast string


concatenation
ƒ What happened?
ƒ A: We made the + operator super-fast by implementing compound strings
(cords), so StringBuilder was unneeded and removed

38
2006 Adobe Systems Incorporated. All Rights Reserved.
Compound Strings

var s:String = “Hello, ”;


s += “world, ”;
s += “from AS3!”;

39
2006 Adobe Systems Incorporated. All Rights Reserved.
Compound Strings

var s:String = “Hello, ”;


s += “world, ”;
s += “from AS3!”;

40
2006 Adobe Systems Incorporated. All Rights Reserved.
Compound Strings

var s:String = “Hello, ”;


s += “world, ”;
s += “from AS3!”;

41
2006 Adobe Systems Incorporated. All Rights Reserved.
Interpret vs. JIT

42
2006 Adobe Systems Incorporated. All Rights Reserved.
Interpret vs. JIT

ƒ We make a simple “hotspot”-like decision about whether to interpret or JIT


ƒ Initialization functions ($init, $cinit) are interpreted
ƒ Everything else is JIT
ƒ Upshot: Don’t put performance-intensive code in class initialization:
class Sieve
{
var n:int, sieve:Array=[], c:int, i:int, inc:int;
set_bit(0, 0, sieve);
set_bit(1, 0, sieve);
set_bit(2, 1, sieve);
for (i = 3; i <= n; i++) set_bit(i, i & 1, sieve);
c = 3;
do { i = c * c, inc = c + c; while (i <= n) { set_bit(i, 0, sieve); i += inc; } c += 2;
while (!get_bit(c, sieve)) c++; } while (c * c <= n); }

43
2006 Adobe Systems Incorporated. All Rights Reserved.
Garbage Collection

44
2006 Adobe Systems Incorporated. All Rights Reserved.
MMgc Garbage Collector: Overview

ƒ Reusable C/C++ library


ƒ Used by AVM1, AVM2 and Player’s display list
ƒ Not specific to Flash Player
ƒ new/delete (unmanaged memory)
ƒ new w/ optional delete (garbage collection)
ƒ memory debugging aids
ƒ profiling

45
2006 Adobe Systems Incorporated. All Rights Reserved.
Garbage Collection

ƒ Old school tech mainstreamed by Java


ƒ Key to VM performance
ƒ Our algorithm
ƒ Deferred Reference Counting (DRC)
ƒ Backed by incremental conservative mark/sweep collector

A = new Object();
B = new Object();
A.next = B;
B.prev = A;

46
2006 Adobe Systems Incorporated. All Rights Reserved.
Deferred Reference Counting

ƒ All about speed, 20% speedup from 7 to 8


ƒ Only maintain RC for heap to heap references
ƒ Ignore stack and registers (scratch memory)
ƒ Put Zero count items in Zero Count Table (ZCT)
ƒ Scan stack when ZCT is full
ƒ Delete objects in ZCT not found on stack
ƒ Wash and repeat

47
2006 Adobe Systems Incorporated. All Rights Reserved.
Incremental Collection

ƒ Marking limited to 30 ms time slices


ƒ Stop start marking
ƒ Smart pointers for minimal dev impact
ƒ Lazy Sweeping
ƒ DRC tied into write barriers (heap to heap)

48
2006 Adobe Systems Incorporated. All Rights Reserved.
Conservative Collection

ƒ One mark routine for all memory


ƒ False positives are possible but manageable:
ƒ Clean stack
ƒ Keep ‘em separated

ƒ No need to write marking routines

49
2006 Adobe Systems Incorporated. All Rights Reserved.
A peek inside the
JIT

50
2006 Adobe Systems Incorporated. All Rights Reserved.
AVM2 Architecture

51
2006 Adobe Systems Incorporated. All Rights Reserved.
.abc Bytecode: Code Compression

ƒ Constant Data
ƒ Strings, Numbers, etc
ƒ Multinames = {ns set}::name

ƒ RTTI
ƒ Method Descriptors
ƒ Type Descriptors, a.k.a Traits

ƒ Bytecode
ƒ Stack Machine notation

52
2006 Adobe Systems Incorporated. All Rights Reserved.
Bytecode Verifier

ƒ Structural Integrity
ƒ Branches must land on valid instructions
ƒ Can’t fall off end of code
ƒ Constant references valid

ƒ Type Safety
ƒ Dataflow Analysis to track types
ƒ Early Binding

ƒ MIR Code Generation (optional)


ƒ Generate IR while verifying
ƒ Single pass to verify + generate IR

53
2006 Adobe Systems Incorporated. All Rights Reserved.
Interpreter

ƒ Stack Machine, no surprises


for (;;) {
switch (*pc) {
case OP_pushstring: …
case OP_pop: …
case OP_callproperty: …
}

ƒ All values are boxed, 32-bit atoms


ƒ Code executes from verified .abc data in SWF

54
2006 Adobe Systems Incorporated. All Rights Reserved.
MIR: Macromedia Intermediate Representation

ƒ Used in JIT compiler to abstract commonalities between CPU’s

55
2006 Adobe Systems Incorporated. All Rights Reserved.
Just In Time Code Generation

ƒ MIR Code Generation


ƒ Concurrent with Verifier
ƒ Early Binding
ƒ Constant Folding
ƒ Copy & Constant Propagation
ƒ Common Subexpression Elimination (CSE)
ƒ Dead Code Elimination (DCE)

ƒ MD Code Generation
ƒ Instruction Selection
ƒ Register Allocation
ƒ Dead Code Elimination (DCE)

56
2006 Adobe Systems Incorporated. All Rights Reserved.
A Tale of Three Notations

AS3
function (x:int):int {
return x+10
}

.abc MIR
getlocal 1 @1 arg +8// argv
pushint 10 @2 load [@1+4]
add @3 imm 10
returnvalue @4 add (@2,@3)
@5 ret @4 // @4:eax

x86
mov eax,(eap+8)
mov eax,(eax+4)
add eax,10
ret

57
2006 Adobe Systems Incorporated. All Rights Reserved.
JIT Overview

ƒ Conventional: Write program, compile to platform and then execute.


ƒ Program bound to hardware early raises a number of issues, mainly
portability and size.
ƒ JIT idea: write program, but don’t ‘compile’ until code is actually on the
target platform.

58
2006 Adobe Systems Incorporated. All Rights Reserved.
Balance

ƒ The question - compile or execute?


ƒ First generation
ƒ JIT spent quite a bit of time compiling.
ƒ Paid the price in start-up performance.

ƒ Next generation
ƒ 2 JITs for two environments; ‘client’ and ‘server’
ƒ Client - better start-up performance for programs like dynamic GUI apps
ƒ Server – best for apps that can tolerate higher start-up hit

59
2006 Adobe Systems Incorporated. All Rights Reserved.
Balance

ƒ Our objectives
ƒ Fast compile times
ƒ Limited passes
ƒ Cautious with memory

ƒ All this and we kept an eye on portability from the onset.

60
2006 Adobe Systems Incorporated. All Rights Reserved.
Architecture

ƒ Hybrid execution model – allows us to interpret .abc directly or invoke JIT compiler
ƒ JIT compiler translates bytecodes into native machine code in 2 passes
ƒ Only the back-end of JIT compiler is platform-dependent; needs retargeting for each
CPU
ƒ Support for x86, PowerPC, ARM …

61
2006 Adobe Systems Incorporated. All Rights Reserved.
MIR

ƒ What?
ƒ Internal representation of the program that bridges .abc and target instruction set
ƒ 3-tuple; operation + 2 operands

ƒ Why?
ƒ Allows us to perform optimizations that otherwise would be quite difficult using a stack
based notation
ƒ Easier to map to underlying hardware

62
2006 Adobe Systems Incorporated. All Rights Reserved.
Optimizations

ƒ Translation from .abc


ƒ Stack manipulation and local moves become no-ops

1 push
1 dup

ƒ Common sub-expression elimination

a = x + y @3 add @1 @2
… @4 … Instruction not generated.
b = x + y @8 add @1 @2 Instead we place ref ‘@3’

63
2006 Adobe Systems Incorporated. All Rights Reserved.
Early Binding

ƒ Can take advantage of running state of system.


ƒ Some objects and properties already resolved and bound.
ƒ During verification stage, type information is propagated.
ƒ Allows support for native types.

64
2006 Adobe Systems Incorporated. All Rights Reserved.
Field Binding

public final class C {


public var f:int;
}
var o:C = new C();
o.f = 46;

30:pushbyte 46
@90 imm 46
stack: C?@89 int@90
32:setproperty {public,bind$1}::f
@93 st 16(@89) <- @90

65
2006 Adobe Systems Incorporated. All Rights Reserved.
Field Binding

public final class C {


public var f:int;
}
var o:C = new C();
o.f = 46;

o is of
type C
30:pushbyte 46
@90 imm 46
stack: C?@89 int@90
32:setproperty {public,bind$1}::f
@93 st 16(@89) <- @90

66
2006 Adobe Systems Incorporated. All Rights Reserved.
Field Binding

public final class C {


public var f:int;
}
var o:C = new C();
o.f = 46;

46 is an
int
30:pushbyte 46
@90 imm 46
stack: C?@89 int@90
32:setproperty {public,bind$1}::f
@93 st 16(@89) <- @90

67
2006 Adobe Systems Incorporated. All Rights Reserved.
Field Binding

public final class C {


public var f:int;
}
var o:C = new C();
o.f = 46;

o has field
30:pushbyte 46 named f that
@90 imm 46
stack: C?@89 can be resolved
int@90
32:setproperty {public,bind$1}::f
@93 st 16(@89) <- @90

68
2006 Adobe Systems Incorporated. All Rights Reserved.
Field Binding

public final class C {


public var f:int;
}
var o:C = new C(); Location of f
o.f = 46;
resolves to offset
on object and type
is int so no
30:pushbyte 46
coerce needed
@90 imm 46
stack: C?@89 int@90
32:setproperty {public,bind$1}::f
@93 st 16(@89) <- @90

69
2006 Adobe Systems Incorporated. All Rights Reserved.
Machine Code (MD) Generation

ƒ In the next and final pass we translate MIR into platform specific instructions.
ƒ Instruction selection (IS)
ƒ Register allocation (RA)
ƒ Register / stack management

70
2006 Adobe Systems Incorporated. All Rights Reserved.
Machine Code (MD) Generation

ƒ In the next and final pass we translate MIR into platform specific instructions.
ƒ Instruction selection (IS)
ƒ Register allocation (RA)
ƒ Register / stack management

@90 imm 46
active: ecx(89-93)
@93 st 16(@89) <- @90
03A20153 mov 16(ecx), 46

71
2006 Adobe Systems Incorporated. All Rights Reserved.
Machine Code (MD) Generation

ƒ In the next and final pass we translate MIR into platform specific instructions.
ƒ Instruction selection (IA)
ƒ Register allocation (RA)
ecx contains o pointer
ƒ and /IA32
Register stackmov instruction
management
allows immediate (46) as
an operand.

@90 imm 46
active: ecx(89-93)
@93 st 16(@89) <- @90
03A20153 mov 16(ecx), 46

72
2006 Adobe Systems Incorporated. All Rights Reserved.
Other IS / RA notables

ƒ A variant of Linear Scan Register Allocation (LSRA)


ƒ Size/speed requirements made this allocator a good fit
ƒ Register hinting support

ƒ Location of operands feeds instruction selector


ƒ Supports optimal use of stack and registers
ƒ Constants fold directly into instruction

73
2006 Adobe Systems Incorporated. All Rights Reserved.
74
2006 Adobe Systems Incorporated. All Rights Reserved.

Вам также может понравиться