Why have a Java Box2d port, if you can just use JNI/JNA?

Hi guys

Given that I'm quite ignorant on these matters, but I was just looking at this fantastic setup using Adobe Alchemy. For those who don't know, Alchemy allows Flash/ActionScript to access C++ natively compiled DLLs. And although it's not as fast as C++, it does run faster than native ActionScript.

Now, JNI/JNA does pretty much the same thing for Java; in fact these are likely where the idea for Adobe Alchemy came from (since Adobe loves to parrot Java).

Why is it that we even have JBox2d, then? Is it so much slower to access a DLL via something like JNA? OK, given, this is physics we're talking about -- pretty damn intensive computation, potentially... but I suspect that any sub-calls within the DLL itself would not experience latency, only calls to the DLL from Java (?)

❓

Just be interested to know what you lead devs thoughts are on this (especially ewjordan). And just so you don't get me wrong -- I really appreciate the work that's been put into JBox2d; working with Java and such a powerful library together is a pleasure. But the benefits, if JNI/JNA could possibly be fast enough, are quite obvious.

EDIT: I found this, relevant to the topic: How does JNA performance compare to custom JNI?
You are not fully cross platform if you use native compiled libraries...
Java is not considerably slower than c++.

BTW: Adobe Alchemy box2d demo shown at page above is slow as hell (on my machine) - I see it is set to 30fps, but it barely scores that (sometimes drops to about 25-28)
With similiar objects count (same machine):
On pure Java i get about 5-10ms per frame (it gives about 100-200fps)
While using Java + native openGL rendering - I get less than 1ms per frame - it gives above 1000 fps

So I see no point in using JNI for box2d... it is fast enough with java - the rendering is problem, java2d is some kind of fucked...

Still my friends are having some serious issues with openGL on windows7 - probably it is becouse windows7 continiously uses directx while doing nothing... and doing so it interrupds openGL.
I started putting together a list of the reasons I dislike JNI, but honestly? You probably could put together a decently useful JNI/JNA wrapper around the C++ Box2d, and it would work for most people. People do this for ODE, and in Python this is exactly the route that they took with Box2d, and when I used that library, there were only a few hiccups.

I think it's more a matter of what you get from having a pure Java library that you wouldn't from a native one:

- Easy library integration, just drop the .jar or source (I highly recommend using the source directly in case people need to make changes) in the project and you're ready to go
- Smooth debugging, right into the library code
- No code signing necessary for applets (this was probably our original motivation for sticking with pure Java, as silly as applet support probably seems these days...)
- Portability comes for free, you don't have to compile separate Box2d binaries against every target that you want to support
- If you need to edit internals, (for instance, Settings, which often has to be changed), you don't need to do so in C++ and then recompile all those binaries

At this point, JBox2d itself is plenty fast, nipping at the heels of the C++ version, so performance is probably not going to be helped very much by going to a JNI version; feature-parity would be, of course, and updating would be easier, which is why I certainly would encourage anyone with the energy to try it out and see. It never hurts to have options, and I'd definitely be curious to see if Box2d could be comfortably usable from Java without too much trouble.
Thanks for taking the time, ewjordan.

ewjordan wrote:
I started putting together a list of the reasons I dislike JNI, but honestly? You probably could put together a decently useful JNI/JNA wrapper around the C++ Box2d, and it would work for most people. People do this for ODE, and in Python this is exactly the route that they took with Box2d, and when I used that library, there were only a few hiccups.


Interesting...

ewjordan wrote:
I think it's more a matter of what you get from having a pure Java library that you wouldn't from a native one:

- Easy library integration, just drop the .jar or source (I highly recommend using the source directly in case people need to make changes) in the project and you're ready to go


Can't disagree here, I can see the usefulness of having the source available in a language one is actually au fait with. My only other thought on this is that, perhaps those willing to develop in Java, since they are already making the trade-off of not writing their game/sim in C++, might be less-inclined to fiddle around in physics engine source, anyway. That's totally an opinion thing, though.

ewjordan wrote:
- Smooth debugging, right into the library code


Since Box2d is still under a fair amount of development, I agree. If it were very much crystallised though, then I'd say better to focus on ensuring 100% working integration with known-to-be-stable C++ source. Given the nature of the industry, it's hard to foresee the desired features list just settling down any time soon, though.

ewjordan wrote:
- No code signing necessary for applets (this was probably our original motivation for sticking with pure Java, as silly as applet support probably seems these days...)


... (no comment as I have no idea how many people develop java games as applets)...

ewjordan wrote:
- Portability comes for free, you don't have to compile separate Box2d binaries against every target that you want to support


Don't think this is a show-stopper, although sure it can a bit tedious. If one has reliable cohorts able to do this, it's better. Some advice for myself, perhaps 😉

ewjordan wrote:
- If you need to edit internals, (for instance, Settings, which often has to be changed), you don't need to do so in C++ and then recompile all those binaries


Similar to your second point. I do wonder if isn't some other feasible way to do configuration, though, where this wouldn't be an issue? Or do these have to be compiled in, as such?

ewjordan wrote:
At this point, JBox2d itself is plenty fast, nipping at the heels of the C++ version, so performance is probably not going to be helped very much by going to a JNI version; feature-parity would be, of course, and updating would be easier, which is why I certainly would encourage anyone with the energy to try it out and see. It never hurts to have options, and I'd definitely be curious to see if Box2d could be comfortably usable from Java without too much trouble.


Yup, the feature-parity and updating are why I was asking. I can only guess that there must be a fair amount of grinding involved in moving JBox2d forward even a single point revision. If all one had to do was focus, per release, on a fast JNI/JNA implementation along with ensuring some compatibility from the C++ end (as and when necessary), wouldn't that be great. Of course that's without having any idea of what execution times would be like, since that's an irreducible barrier beyond a certain level of interface efficiency (and from that article I posted, it seems like the "static-binding" style JNI would likely be the fastest option -- and also the most tedious to implement and maintain).

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