Why casting vector indexes to uint?

Software: Other

rdoi, Member
Posted: 09 April 2014 11:42 PM   Total Posts: 86

Not really related to Away3D, but this have been puzzling me for a while:

In the away code, its not rare to find literal vector indexes explicitly casted to uint.

var vec:Vector.<Number>= new Vector.<Number>(10);
// ...
var anyvar:Number= vec[ uint(2) ];
// ...

What would be the technical reason to do this? Is there a performance gain doing so?

   

Avatar
theMightyAtom, Sr. Member
Posted: 10 April 2014 06:27 AM   Total Posts: 669   [ # 1 ]

What would you cast them to? They ARE uints. They can’t be negative, so you want as many positive ones available as possible.

Cheers!

   

rdoi, Member
Posted: 10 April 2014 10:23 AM   Total Posts: 86   [ # 2 ]

@theMightyAtom, the point is that they are NOT casting a variable, they are casting a plain simple literal number.

The number 2 will never be negative and won’t be “as many positive ones available”. Its just 2.

Take as example the code in Camera3D.updateFrustum():99

c11 raw[uint(0)];
 
c12 raw[uint(4)];
 
c13 raw[uint(8)];
 
c14 raw[uint(12)];
 
c21 raw[uint(1)];
 
c22 raw[uint(5)];
 
c23 raw[uint(9)];
 
c24 raw[uint(13)];
 
c31 raw[uint(2)];
 
c32 raw[uint(6)];
 
c33 raw[uint(10)];
 
c34 raw[uint(14)];
 
c41 raw[uint(3)];
 
c42 raw[uint(7)];
 
c43 raw[uint(11)];
 
c44 raw[uint(15)]

Again, why is it needed to explicitly cast a literal number to uint?

   

Avatar
theMightyAtom, Sr. Member
Posted: 10 April 2014 10:33 AM   Total Posts: 669   [ # 3 ]

Well, that’s a different question to

its not rare to find literal vector indexes explicit casted to uint

If they are/could be floats (Number), than presumably to save memory?
What do you see as the disadvantage? If the coder knows it should be an uint, is it not better to get an error?

   

ranoya, Newbie
Posted: 10 April 2014 12:02 PM   Total Posts: 29   [ # 4 ]

May be a disadvantage cause casting has a price. The question is what is priority: memory saving or timing.

But i donĀ“t know exactly the timing price when casting Number to uint. Someone knows?

   

Avatar
theMightyAtom, Sr. Member
Posted: 10 April 2014 12:30 PM   Total Posts: 669   [ # 5 ]

Good question.
There are of course many different contributors to Away3D and I’m sure some code has been translated from other languages/sources. Maybe one of the devs can chip in, is this a protocol you follow, or just incidental?

   

gonchar, Newbie
Posted: 12 April 2014 09:38 AM   Total Posts: 7   [ # 6 ]

manual casting saves the performance

   

rdoi, Member
Posted: 12 April 2014 10:34 PM   Total Posts: 86   [ # 7 ]
gonchar - 12 April 2014 09:38 AM

manual casting saves the performance

Would it cover the cost of the additional cast call? Because I assume am2 already does it internally.

Anyway, could you provide a link or any source that details this practice?

If this is true, can’t we assume if we cast all vector indexes to uint in entire code we could get a super performance boost in Away?
It would be pretty easy to solve this with a simple regexp.

   

gonchar, Newbie
Posted: 12 April 2014 10:55 PM   Total Posts: 7   [ # 8 ]

I don’t remember links. it saves performance if you use old compiler. Yep it’s not a huge performance saver, but it is macro optimization, why not?
if you use new ASC2.0 compiler, it compiles ok swf bytecode and also ignores uint() conversion

   

rdoi, Member
Posted: 22 April 2014 01:17 PM   Total Posts: 86   [ # 9 ]

Well, I did a quick benchmark tests using this uint casting concept (AIR 4.0/ASC2).

I’ve confirmed if you cast a vector index to uint, it (somehow) fasten really slightly the process, only noticeable in a really intensive loop.

Other strange thing I’ve discovered is that if you “as” cast to uint ( v[n as uint] ), instead of using the function casting ( v[uint(n)] ) you worse a lot the performance in Win desktop (doesn’t happen in Mac…).

So weird… I gotta have more time to test all this properly…

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X