AS3 performance and optimization

Heya.

Look, I'd like to start off this post by graciously thanking everyone involved in making the library. It's filled with brilliant ideas and the different language ports all contain their individual genius. In no way am I trying to lessen the massive kudos everyone involved should receive by writing this post.

That being said.

Version 2.01 is pretty far from AS3 optimized. There's a myriad of places where very simple code optimization sweep can improve the cycle time by quite a lot. It ranges from small details like multiplication instead of division, to iterating over accessors, to iterating and accessing chain accessors, to iterating and accessing static methods (or properties), not typecasting properly, overuse of the var keyword and in general just not quite sound coding practices. Compared to the insane complexity of the box2d engine, there's no rocket science involved.

I'm currently involved in a somewhat ambitious project involving box2d and after having spent maybe 3-4 days just optimizing simple things I'm fairly convinced that a rigorous optimization sweep could improve the overall performance by at least 50%. Over the course of the last few days I have improved the lowest FPS dips with around 15 frames per second. And it feels like I'm just scratching the surface.

Everything that I've changed is clearly marked by the way, no need to get nervous about libraries being intact.

Now, I guess where I'm going with all this is the open source nature of the project. I would love to contribute with everything I've gone through and optimized but as far as I can see there's no real good channel to do so.

If not, I would graciously ask that the project originators maybe shoot me a message and I'll list all the issues I've found so that you guys can sort them out in any way you deem okay.

Cheers for your time.
Are you working off of a pretty recent version of SVN? When I profiled my game, I found that the rendering was taking 20x longer than the world.step. I have a bitmapData.scroll + dirty rect technique that I use, and the performance of the renderer is still the long pole in the tent. Agreed, no reason to lose free speed once you take the time to optimize it, but the AS3 version of Box2D is struggling hard to keep up with C++ and is constantly in flux, so Boris hasn't been focussed on optimizing what is there.

Additionally, if you want a speed boost, replace the dynamic tree with spatial hashing (check out Chipmunk's implementation for a decent reference). Spatial hashing, with proper tuning for your game will far outperform all the tiny optimizations.
pTymN wrote:
Additionally, if you want a speed boost, replace the dynamic tree with spatial hashing (check out Chipmunk's implementation for a decent reference). Spatial hashing, with proper tuning for your game will far outperform all the tiny optimizations.


Have you profiled this? The broad-phase should be a very small portion of the time step. Also the dynamic tree should be faster than the spatial hash when shape sizes are not all the same. Ray-casts should always be faster in the dynamic tree.
Sorry, I'm late to the party.

I've made headway with optimizing box2d, as it does have some issues. But we've moved on from 2.0.1, so I'm not sure your changes are useful. Also, I haven't been committing changes unless they give substantial performance benefits - 5% or so. There have been many optimizations I've read about and tried, only to find no benefit (bafflingly, Array->Vector seems to be one). I don't want to make the library so obscure it's unreadable - that would lose a major benefit imho, and make future porting impossible.

I can put up your copy as a special release, but if you want them checked into SVN head, you will have to redo your changes against it, and have them reviewed by me (the AS3 porter/dev). I would like that, as I have found performance optimization a messy process.

To address specific issues you've raised, here's my understanding of performance breakdowns:
Iterating over linked lists is one of the fastest iterations methods I've seen.
Extra local variables (var) never hurt - the flash compiler is incapable of detecting common subexpressions, and I don't trust the JIT to do it (though allegedly it can).
I am not aware of any typecasting issues. Only minimal areas of the library aren't fully typed (for use of Function). In most cases, (like the C++) we don't rely on dynamic typecasting - there's GetType() instead.

The two areas I've been focusing on as the best source for gains:
Cutting down allocations. The engine pools everything that is dynamically created per frame, and uses private static members where the C++ uses objects on the stack (assuming it's not a recursive function, but virtually none of them are).
Manually unlining all vector operations. All inlining is good, but vector ones can save on allocations as well. That's why there absurd amounts of pX,pY type stuff.

Erin:

I actually spent some time this weekend profiling the AS3 engine. For standard cases, the profile is the same as Box2D proper - SolvePositionConstraints dominates costs (probably because I have the iterations set too high by default, I could do with some recommendations on that).
But the broadphase can flare up a bit in some configurations. Box2DFlash has the old SaP broadphase as a drop in replacement, so I can see that the dynamic tree itself is responsible for some of this slowness.
Erin Catto wrote:
I actually spent some time this weekend profiling the AS3 engine. For standard cases, the profile is the same as Box2D proper - SolvePositionConstraints dominates costs (probably because I have the iterations set too high by default, I could do with some recommendations on that).
But the broadphase can flare up a bit in some configurations. Box2DFlash has the old SaP broadphase as a drop in replacement, so I can see that the dynamic tree itself is responsible for some of this slowness.


You typically need 6-8 velocity iterations and 2-3 position iterations.

The dynamic tree should have better average and worst case performance than SAP and spatial hashing. But some tuning may still be needed. Nevertheless, it should be less than 5% of the step cost. That said, it has been a while since I have profiled Box2D.
I sandwiched 10 thin rectangles together in one body along the y axis, and this is sufficient to drop the framerate by a factor of 10 when moving in the y axis at moderate speeds. It's still very bad for the SaP, but only about half so (judging by eye, I have not profiled).
That is bizarre. Such a small number of shapes shouldn't drop the frame rate under any circumstances.
This is AS3 remember, so it's many times slower, and may have a different performance profile (though I do not think so in this case). This means problems are more noticable, but the same thing could well apply to a larger bunch of shapes. Most of the test cases involve 10-50 shapes, so 10 is a significant fraction of that.
I was creating a "smooth" landscape out of thin polygons, so more than 10 could be possible in some other sort of game.

I'm not filing an issue or something, I'm just saying.

Original thread: https://box2d.org/forum/viewtopic.php?t=4545