Fixed point available in JBox2D?

I was looking at this thread: viewtopic.php?f=9&t=36&hilit=fixed+point&start=190

...And noticed that JJC1138's efforts toward an Android point produced (presumably) at least some progress toward a fixed point implementation for Java, and that his changes (optimisations, actually) were partly integrated into JBox2D according to ewjordan. But it's my impression that there is presently no actual supported fixed point "setting" for Java as in C++ Box2D?

Is this correct?
Doesn't support fixed point as far as I am aware. I didn't think Android required that though (or are you looking to just optimize?). Somewhat related discussion here: http://www.box2d.org/forum/viewtopic.php?f=5&t=124&p=2455. Hope this helps.
Hey,

No idea why I wasn't notified of thread reply... thanks for responding.

I don't know whether Android requires it or not, I was just saying that some fixed point work had been done there. IIRC Android's floating point support is none too cool.

Actually I was asking re the fixed point for the purposes of cross-platform deterministic networked physics, which is probably the biggest thing that fixed point brings to the table for systems that aren't in some way float-handicapped. They say (around here and elsewhere) that it makes life vastly simpler; you can actually rely on calculations being the same across all clients (or peers, since p2p becomes more feasible under these conditions), instead of the more standard way of doing some dead-reckoning on each client and then correcting that using the server's data every X timesteps.
Fixed point math is easy to implement in c++ because you have much greater control over math operations and such. In java, it would turn every math operation into a method, which as you can imagine would be both difficult to accomplish and ugly as hell. I think ewjordan was going to look into this at some point though
Ahh yes I saw this in a discussion, re the lack of operator overloads. But was that the only reason? I think there others, too.

Anyway, thanks for responding.
toucansam wrote:
I think ewjordan was going to look into this at some point though

Yes, I have quite a bit of interest in getting JBox2d running well on Android, now that I finally have an Android phone to develop on (a Nexus One).

#1 priority for that is getting garbage collection down to near-zero, because especially on the earlier Android devices, GC is really, really slow, from what I'm told. Though I haven't checked yet, I would be seriously surprised if anything other than GC was bottlenecking JBox2d on Android right now - on the desktop the best speed gains have always come directly from reducing GC traffic, no other attempt at optimization has ever come close. Even now, with all the low hanging GC fruit long gone, by spending a couple hours just now doing absolutely nothing but replacing some of the few remaining allocations here and there with pooled accesses I was able to tweak another 10% out of the engine (piston benchmark went from 750 to 825 fps).

But Handcrafter is right, according to Google, even on the Nexus One, integer math is supposedly twice as fast as floating point, and that has an FPU. There will be even bigger differences on earlier phones, which do not. So there's a huge incentive to use fixed point in an Android version, at least until those processors start to look more like desktop processors across the board (on the desktop, fixed point is at best break-even today, and sometimes it's even a speed loss). This will happen, but maybe not for a while.

What I'm planning to try, ultimately, is more or less what we did with GC - rather than try to do it "right" from the start (and almost certainly burn out before finishing up), I'll start by touching up hotspots in the code, moving those over to fixed point as needed, and slowly spreading out those conversions around until performance reaches an acceptable level. I suspect by converting the right few hundred lines of code, performance could see a 400% boost on Android pretty easily. Box2d has some methods where there is huge bang-for-the-buck because they're called so often, so even just a few hours of work should be able to cut the floating point calculations down by a great margin.

I'll keep you posted.
Rocking. 👿 I'll be keeping my eyes open for any updates on this.
According to Google's optimization guide, http://developer.android.com/guide/practices/design/performance.html, float point is about twice as slow.

Quote:
As a rule of thumb, floating-point is about 2x slower than integer on Android devices. This is true on a FPU-less, JIT-less G1 and a Nexus One with an FPU and the JIT. (Of course, absolute speed difference between those two devices is about 10x for arithmetic operations.)

In speed terms, there's no difference between float and double on the more modern hardware. Space-wise, double is 2x larger. As with desktop machines, assuming space isn't an issue, you should prefer double to float.

Also, even for integers, some chips have hardware multiply but lack hardware divide. In such cases, integer division and modulus operations are performed in software — something to think about if you're designing a hash table or doing lots of math.


Anyway, I'm assuming it will improve over time. I will hopefully get my droid x this week, so will see if the 1Ghz processor helps.
Edge wrote:
According to Google's optimization guide, http://developer.android.com/guide/practices/design/performance.html, float point is about twice as slow.

Yes, Google's optimization guide has a lot of good tips that I'm going to investigate - I'm particularly surprised at the speed differences between various styles of loop, I'm certain that some of those optimizations will be very helpful. Unfortunately, a lot of these optimizations arethings that for several years have been 100% useless (in some cases even counterproductive) in Hotspot, so a lot of things that we did to clean up the internals a bit will have to be rolled back. One of the first things I'll have to do is strip getters and setters with abandon:

Quote:
Without a JIT, direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter. This is true in Froyo, but will improve in the future when the JIT inlines getter methods.


Yikes! A 3-7x performance penalty for using getters/setters could almost exclusively be at fault for the performance issues here, since in the latest versions of JBox2d most direct field accesses are gone. Certainly if we could gain 3-7x the speed across the board the engine would at least be usable on more recent phones...

Quote:
Anyway, I'm assuming it will improve over time. I will hopefully get my droid x this week, so will see if the 1Ghz processor helps.

Certainly - I expect that the Dalvik improvements will be very important, I've already seen speed gains in many apps upon update to Android 2.2. But unfortunately in the meantime, there are still phones coming out now that are going to be stuck indefinitely with 1.5 or 1.6, which is a shame. So the fight continues...

I'm pretty sure the Droid is about on-par with the Nexus One, and on my Nexus, JBox2d is still quite slow (for example, I get 60 fps on the pyramid demo on my Macbook, but on the phone, it crawls along at 2-3 fps). I'm tracking down the issues, though - I profiled some things on the phone, and the bottlenecks seem to be very different from where they are on the desktop. For one, I discovered some serious object creation issues in the broadphase (fixing now) that cause a lot of lag when initializing a scene. After that I'm going to start digging into the bottlenecks while running.

FWIW, to get things running quickly, I started with the AndroidBox2d project at Google Code (http://code.google.com/p/androidbox2d/), which is not exactly active, and uses an old version of JBox2d (I pulled it out and worked in my local development copy), but at least has a debug renderer set up. If I can get things going, I may try to contact pngliu about reviving (either effectively taking over or joining, depending on if he wants to remain involved) that project, it seems like a good place for this to live - keeping it inside the base JBox2d project would be a huge mistake, because the code changes will be quite drastic and make development there much more difficult. I also suspect that some of the constraints may have to be quite different - for instance, on Android, I'm not sure that we'll be able to afford using ThreadLocals (which are used a lot in JBox2d instead of statics to try to keep things mostly threadsafe for people that might run multiple worlds at once).

I'll keep you posted...in the meantime, there's always the Emini engine (http://emini.at), which supposedly runs very well on all sorts of mobile devices. But it's not free (couple hundred bucks or so, depending on the version), and it's not anywhere near as feature-packed as Box2d, so...
ewjordan wrote:
Quote:
Without a JIT, direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter. This is true in Froyo, but will improve in the future when the JIT inlines getter methods.


Yikes! A 3-7x performance penalty for using getters/setters could almost exclusively be at fault for the performance issues here, since in the latest versions of JBox2d most direct field accesses are gone. Certainly if we could gain 3-7x the speed across the board the engine would at least be usable on more recent phones...


I had changed most getter methods to direct field access in the svn version, but this is bad news for vector math, as most math calls are method calls. However, in most of the hot areas of the engine this is all optimized out to basic math calls (still floating point though).

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