Scratch Objects and performance. comments( 3)

So Actionscript 3 is fast…. er than actionscript 2. But performance is still a big bottleneck. One well known fact is object creation is slow. Adobe knows this and is addressing it
@see Tamarin Bugzilla

But I was crunching some code tonight and was startled at just how slow it really is.
Take The following function :

/**
* rotates a point by this quaternion.
*/
public function rotatePoint(p : Point3d) : void {
   // copy of origin p
   var _:Point3d = new Point3d();
   _.x=p.x*2, _.y=p.y*2, _.z=p.z*2;

   p.x -= (y*y + z*z)*_.x - (x*y - z*w)*_.y - (x*z + y*w)*_.z;
   p.y += (x*y + z*w)*_.x - (x*x + z*z)*_.y - (x*w - y*z)*_.z;
   p.z += (x*z - y*w)*_.x + (x*w + y*z)*_.y - (x*x + y*y)*_.z;
}

But if we change this apparently math heavy function to use a scratch object instead of creating a new instance of Point3d each call …

/**
* rotates a point by this quaternion.
*/
public function rotatePoint(p : Point3d) : void {
   // '_' is a scratch Point3d private to this instance
   _.x=p.x*2, _.y=p.y*2, _.z=p.z*2;

   p.x -= (y*y + z*z)*_.x - (x*y - z*w)*_.y - (x*z + y*w)*_.z;
   p.y += (x*y + z*w)*_.x - (x*x + z*z)*_.y - (x*w - y*z)*_.z;
   p.z += (x*z - y*w)*_.x + (x*w + y*z)*_.y - (x*x + y*y)*_.z;
}

// scratch Point3d for quick private use.
// maintains no significance
private static const _ : Point3d = new Point3d();

rotatePoint now executes a stunning 5(FIVE!) times faster.

and since we’re talking performance…
Static variables are usually slow
@see Adobe Jira
But since this scratch object is local there is no HT lookup cost. We’re simply creating one scratch Point3d to be used by all instances of the class. Call it a dirty trick, but we have one thread and its fair game.

Leaving Beantown. comments( 2)

Big changes. Recently I was given an opportunity to go to San Diego and make games with Areae. I have a lot of friends and family in the Boston area and I know I’ll miss them a lot while I’m tanning my irish skin on the pacific coast beaches in February.

But besides the grumpy people, nasty cold, maze-like streets I’m also going to miss the Flash community of Boston. I hope you guys dont quiesce in my absence, because you can rate a city by how many programmer’s drink Kamikaze shots on a work night. See you guys at Flash on Tap and the Ugly Sweater Party.

Leaving my current job at Visual-io is a tough one too. Visual-io is a great place to work Doug, John, Brett, my duck and all the dinosaurs have been great developers to work with. P.S. Duck is coming with me to San Diego. If you timebox it with some HLD and LLD I’m sure you can frob up another one in a few cycles.

Man, I would write on but I am tearing up. You rock Boston.

Editor version 0.2 comments( 1)

http://www.jlgauthier.com/blogSup/stick/edit2/bin-release/StickEdit1.html

Moving along. I was hoping things would go fast tonight but calculating where a vertex should go to based on the x,y of a view port proved to be a lot of code.

Rendering a point in 3d means

  1. place the point in scene space (points are stored initially in figure space)
  2. translate its position to camera space (relitive to the eye of the camera)
  3. rotate its position relative to the eye of the camera’s view direction (the angle between the camera eye and the camera target)
  4. translate the position again into view space (if your render region is 400 by 400 you need to move things 200 pixels right and down)
  5. scale the final xy coordinates based on the z axis and your perspective multiplier

And that is what gets rendered in the view port… so moving a point in a view port means going through all of those steps backwards to calculate what your really doing to the vertex. Talk about fun.

The next step is easy, allow export and import of figures. But after that its going to get hard again because animation means adding state to the figures, which could lead to some refactoring on the rendering side of things and a lot more math.

Editing the people. comments( 0)

Another free night… kinda and started working on an editor. It’s rather useless thus far… but it looks cool and has a lot of buttons that do things :)
editor
See it.

I decided it would be more fun to do animation than texturing for a few reasons.

  1. Paper vision already does kick ass texturing.
  2. The figures have no flat faces so texturing well means using displacement maps all over the place.
  3. My estimation is that effective texturing will run at least 2 times slower

So animation is going to be way more fun anyway.. The editor shell came along pretty quick, hopefully Thrs I’ll have some time to play with it and get dragable vertexes, and parent / child rotation.

3D stick people. comments( 1)

people
Take them for a spin

I’ve wanted to build this for a long time, luckily I’ve had trouble sleeping this week.
The engine defines figures with vertexes in 3d space, then draws lines between them instead of using triangular faces. scaling the vetexes makes the people tall, thickening the lines makes them fat. The hope is to get some texturing on them and do some animations, but who knows. Cool concept I think.

SubSpace in flash comments( 3)

I’ve been working on simple Subspace engine in flash. It’s just getting to the point where it is fun to fly in, so I thought it was time to show it off.

Click the image to play
Click to play
The server is written in java and uses tcp/ip (an ill-advised protocol for multiplayer games) so response time is not great but it is handling ok so far. Whats exciting is Flash 10 may have UDP support so games like this may start appearing on flash game web-sites, I’m still looking for proof of this, although I have heard it second hand… whats that worth?

I’m not sure if I will be working more on this engine or not, this was more of an educational project for me, but who knows. Let me know what you think!

A Better Look at uint int and Number Performance Data. comments( 3318)

I’ve written a benchmark swf to time several operations and conversions across all three numeric types. Using the methodology described in my previous post, the test suite compares the time cost of each opperation to the time cost of a single function call. Using function-time as a unit hopefuly minimizes proccessor speed as a factor.

Run the test for yourself (click here)
Please run the swf and comment to this post with your own results (be sure to select all). I’m especially intrested in result sets from varying opperating systems. When I collect enough data I’ll post again with some better analysis.

source code

Debug Development Player
[Adobe Windows, Windows XP]
[ActiveX, WIN 9,0,28,0]
function call time: 929
Array[Index]
uint: 0.079 int: 0.134 number: 0.545
Addition += uint
uint: 0.275 int: 0.276 number: 0.381
Addition += int
uint: 0.029 int: 0.027 number: 0.073
Addition += Number
uint: 0.565 int: 0.242 number: 0.079
subtraction -= uint
uint: 0.920 int: 0.587 number: 0.381
subtraction -= int
uint: 0.561 int: 0.031 number: 0.074
subtraction -= Number
uint: 0.565 int: 0.241 number: 0.079
Addition + 5
uint: 0.031 int: 0.032 number: 0.073
Subtraction - 5
uint: 0.522 int: 0.035 number: 0.071
Bin&ry Op
uint: 0.024 int: 0.025 number: 0.289
M%dulus
uint: 1.321 int: 0.867 number: 0.905
Di/ision
uint: 0.627 int: 0.193 number: 0.411
Multiplication * 5
uint: 0.569 int: 0.151 number: 0.416
Multiplication * .5
uint: 0.548 int: 0.248 number: 0.081
Assingment=uint
uint: 0.025 int: 0.025 number: 0.322
Assingment=int
uint: 0.024 int: 0.027 number: 0.029
Assingment=Number 56
uint: 0.103 int: 0.103 number: 0.022
Assingment=Number 5.6
uint: 0.103 int: 0.101 number: 0.022
Compare==uint
uint: 0.004 int: 0.372 number: 0.347
Compare==int
uint: 0.350 int: 0.004 number: 0.020
Compare==Number
uint: 0.344 int: 0.019 number: 0.012
uint(convert)
uint: 0.025 int: 0.024 number: 0.104
int(convert)
uint: 0.023 int: 0.025 number: 0.105
Number(convert)
uint: 0.324 int: 0.031 number: 0.025
For ( uint: 0.027 int: 0.027 number: 0.443
while (loop--)
uint: 0.442 int: 0.034 number: 0.132
Complete.

After running this test at home and at work I’m so far drawing these conclusions:

Generally mixing types is slow, manually converting types is faster than relying on coersion.
Number performs best in opperations where floats might occur.
uint performs poorly with most arithmetic but best with indexing and binary ops.
int is the most versatile and forgiving. For many opperations inline conversion of uints to int, or Numbers to int will improve performance.

Two Tips for Benchmarking. comments( 0)

In anticipation of throwing a ton of benchmark data at you, I am going to run through two tips on benchmarking in Actionscript. The purpose is to explain my techniques, give credibility to my own benchmark data, and to help point out some not-so obvios pitfalls.

Avoid the debug compile
The debug compile in Flex-builder is severely tainted with debug code. Optimizations that improve performance in a debug compile may not improve performance in a release compile. Depending on your version of flexbuilder you may need to publish a release version to compile with out debug code.

Isolate what your testing
This is a no brainer, but it needs to be said. If you want to know how long it takes to perform an opperation, but the opperation is too fast to measure in milliseconds its obvios to place that opperation in a for loop. The catch here is you must subtract the time of running the foor loop or you will taint your results.

example :
var i:uint=0, t2:int=0, t1:int=0, t0:int=getTimer();
for(i=0;i<_testScale; i++){
}
t1=getTimer();
for(i=0;i<_testScale;i++){
__test1();
}
t2=getTimer();
_out('function call time: '+((t2-t1)-(t1-t0))+'\n');

This is especially important for granular timing as you will see in my next post.

OK a blog. comments( 5)

I’ve had a rough history with blogging and websites… the chances I’ll be sharing my thoughts through this are pretty much zero. Setting it up was fun at least. I even came accross this neat Captcha thing, called re-Captcha. Apparently whenever you use it you are helping the world preserve books.

Which means the more I post and the more you comment the more books our descendants will have access to. electronicly. So please comment, and make books available for our children. To encourage people to participate I’m not going to spell-check. That way if you are stuck with nothing relevent to say but still want to contribute to future generations you can correct my spelling… and I’ll match you with a comment (and another re-Captcha!) thanning you.

Other Important Blogs