Stuff I am building:


Ted Patrick - Events & Community @ Adobe Systems


Note: This is the personal blog of Ted Patrick. The opinions and statements voiced here are my own.



Optimizing _Global lookups

DIGG IT!     0 Comments Published Saturday, June 21, 2003 at 4:59 AM .

Whenever you look-up a variable in _global, the Player uses scoping rules to find an answer as follows:

1. Look locally
2. Look up the inheritance chain recursively ("follow the __proto__")
3. Look in _global
4. Else return undefined

As _global is last location where something can exist, if you know it is there, it makes sense to use _global to avoid steps 1 and 2.

Here is an example. This code is called within a MovieClip say _level0:

trace(Math)

1. Is Math local? No
2. Is Math in Movieclip.prototype? No
3. Is Math in Object.prototype? No
4. Is Math in _global? Yes Return

vs

trace(_global.Math)

1. Done

//////////////////////////////////////////////////////////////////////////////

Another optimization is to reduce the number of bytecode instructions by using slash notation. It is important to recognize that a reduction in the number bytecode instructions will directly improve player performance. Although I personally reserve slash syntax optimizations for performance intensive code like components and system level functionality. It doesn't make sense to use slash syntax all the time. Here is an example:

//ActionScript
_global.Math

//Bytecode via Flasm
push '_global'
getVariable
push 'Math'
getMember

vs

//ActionScript
/_global:Math

//Bytecode via Flasm
push '/_global:Math'
getVariable

//////////////////////////////////////////////////////////////////////////////

I conducted some tests to challenge my conclusions. This code loops over a _global lookup for the Math Object 10000 times 4 different ways. The tests were timed to compare different syntax usage. Here are some typical results:

Results
a:245 Math.a
b:263 _global.Math.a
c:212 /math:a
d:209 /_global/math:a

Code:


a=10000
b=10000
c=10000
d=10000

a0=getTimer()
while(a--){
Math.a = 23
}
a1=getTimer()

b0=getTimer()
while(b--){
_global.Math.a = 23
}
b1=getTimer()

c0=getTimer()
while(c--){
/math:a = 23
}
c1=getTimer()

d0=getTimer()
while(d--){
/_global/math:a = 23
}
d1=getTimer()

trace("Results")
trace('a:'+(a1-a0)+' Math.a')
trace('b:'+(b1-b0)+' _global.Math.a')
trace('c:'+(c1-c0)+' /math:a')
trace('d:'+(d1-d0)+' /_global/math:a')


Cheers,

ted ;)

0 Responses to “Optimizing _Global lookups”

Post a Comment

Where to find me:

Ted on Twitter - @AdobeTed
Ted on Adobe Groups
Ted on LinkedIn
Ted on Facebook
Ted at Adobe


Latest

Lists

Links

Jobs

Flex Jobs
city, state, zip

Archives