Box2dx xbox performance

Hi Guys,

I am developing a game for the XNA platform using the Box2dx port of box2d. However I'm having some serious performance issues on the xbox 360.

With about 50 shapes and 20 revolute joints, the performance drops to 1 frame every couple of seconds on the xbox, but runs at a steady 60fps (the max) on my computer (2.2ghz processor).

I used the awesome CLR Profiler to optimise Box2dx memory. I introduced memory pools, turned simple classes into structs and replaced method local arrays with static arrays (so its not allocated on the heap every time). This reduced my GC time on the Xbox from about 300ms a second to 40ms (GC latency * GC collections). But only resulted in a small increase performance.

I also tried inlining the maths and vector methods in the tight loops, to reduce struct copying and method invocation overhead. This (unsurprisingly really) resulted in no performance increase, I guess the JIT is inlining most of it anyway.

So I'm running out of ideas. I was expecting it to be slower on the xbox, but 60x slower!?

Has anyone here managed to get good performance on the xbox 360? What tricks did you use? Am I just missing the "be fast" option somewhere?

Thanks!
I'm not really sure how much this would help on XBox/CLR, but you might want to take a look at what JBox2D has done recently: adding *ToOut methods that directly modify another vector/matrix/... instead of returning the result: viewtopic.php?p=16077#p16077

It might be quite a lot of work to do this optimization, though. But it should reduce GC a lot!
I've never done any XBox dev, but just a thought: is it possible you're running in some sort of debugging mode or something like that? GC should generally be the bottleneck in any GC language with Box2d, from what I've seen, and if you're down to 40 ms per second on that, there's got to be something else sucking up most of the time. I'd scour your build settings to make sure there's nothing odd.
I am a frequent reader of the creators club forums and an indie xbox developer, and I think I may know what your problem is.

First thing though, the JIT compiler does no optimizations... this is very well documented on the creator's club forum. Any loop unrolling and function inligning must be done by you. Additionally, even small functions can cause problems if the data isn't contigious, since it can cause a cache miss.

However, since you didn't see much of a performance gain doing this that is not likely to have been a significant bottleneck for you. More likely it is the poor floating point performance of the .net runtime. Note, this isn't the XBox that has poor floating point performance... in fact it has very sophisticated vector units built into it. The problem is that XNA is based on the compact .net framework that was designed for embedded devices... as a result it can not use the vector units at all. Think on a factor of 10 or worse for floating point calculations compared to a desktop.

However, you do have access to the GPU and if you can parallelise the operations you can do them on the GPU. Sorry, not my area so I can't help you other than point it out.

My suggestion for box2d on xbox is to port the fixed point version that was on DS. it might be faster.
Thanks guys.

After reading your replies and looking around a bit more (http://forums.xna.com/forums/p/13212/69848.aspx) it looks like the culprit is floating point maths.

I'll try moving to fixed point maths and see how that goes.

I'll let you know of the results when I'm done (i dont have access to my xbox until next week though).
So am back with some results.

Fixed point arithmetic didn't help at all. The JIT on the Xbox is no good at inlining overloaded operators, so any performance gained from using fixed point was hugely overshadowed by the overloaded operator overhead.

What I did get some better results from however was replacing Vec2 with C# XNAs Vector2, and in the performance hot spots for my game I rewrote the vector maths to use the C# vector maths methods instead of the overloaded operators.

This actually gave a very decent performance boost and Box2dx is now very usable for my game. It is still much slower than my comp though.

I also implemented multi-threading by solving the islands in different threads. It wasnt hard to do, but I haven't really tested its performance gain yet, but I'm not expecting it to be significant.
Have you included this in the SVN version of box2DX?
No, as I went through various different experiments and hacks its a bit of a mess and will take time to clean up. Also because I use the Vector2 class, the project now depends on the XNA library.

Also nobody in the Box2DX forum has expressed any interest.

I would instead suggest that the next version of Box2dx which is being worked on now uses memory pooling, and doesn't use overloaded operators.

If you are interested in my code, I can upload it. But I don't make guarantees on its correctness, and also I only optimised the areas important to my game (contact solvers and revolute joints)

I think Farseer engine is well optimised for the xbox, but I didn't want to use it because the different interface makes future porting to other platforms more difficult.
I would be interested actually. The contact solver and joint optimizations would be a big help to my game as well and save me some time. Maybe we can share optimizations as we come up with them.

Interesting thing though, Farseer is based on box2d lite. Plus, the creators have started work on version 3.0 which is going to be a port and then overhaul of box2dX...
Quote:
Interesting thing though, Farseer is based on box2d lite. Plus, the creators have started work on version 3.0 which is going to be a port and then overhaul of box2dX...


Yep, that is exactly what we are doing at the moment. Xbox360 will also be a targeted platform in 3.0 which means that we will be doing a lot of low level optimizations to make it run better on Xbox360.

As Convolous concluded it is all the small things that makes it perform well on Xbox360. We will of course remove all garbage possible and make sure the JIT optimizations can be run as optimal as it can (static methods, good candidates for inlining, XNA math library etc.).
We also have multithreading in mind and Matthew from our project has experimented with the exact kind of multithreading you mentioned. He has also looked at work-stealing and dynamic allocated threads. Something we will look much more into in Farseer Physics 3.0.

Our main goal right now is to get a stable foundation. That being said we are in the progress of converting Box2D SVN into C# (and update Box2DX at the same time - we have ihar3d working with us) and get it to simulate as close to Box2D as possible.
We have been haunted by some bugs in the broadphase and TOI (Time Of Impact), hopefully all the TOI bugs are gone now (Big thanks to dinedal for helping us with this) and the change in broadphase to dynamic tree will eliminate the last bugs remaining.

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