Box2D Java

toucansam wrote:
Optimizations wise, the changes are:
  • Basic java optimizations, like finalizing methods and reducing method calls (esp in the math library)

From my experience, I would say that method calls hardly have any effect. At least not with the -server VM. With -client it might be a different case and that reminds me I should do some benchmarks with the client VM.

I tried some compile time inlining of vec/mat/xform/etc. method calls with Scala 2.8 and did not even gain 1% in performance, or can't even say that I gained anything at all -- even the results of each run of my tests may be a few percents different.
Villane wrote:
toucansam wrote:
Optimizations wise, the changes are:
  • Basic java optimizations, like finalizing methods and reducing method calls (esp in the math library)

From my experience, I would say that method calls hardly have any effect. At least not with the -server VM. With -client it might be a different case and that reminds me I should do some benchmarks with the client VM.

I tried some compile time inlining of vec/mat/xform/etc. method calls with Scala 2.8 and did not even gain 1% in performance, or can't even say that I gained anything at all -- even the results of each run of my tests may be a few percents different.



Well, then that might be Scala automatically inlining those calls for you, because it made a difference on the JVM, esp with hot methods like Mat22.mul and XForm.mul. Actually I guess I didn't word it quite the way I meant it, which is removing unnecessary or expensive method calls that can be inlined. Anyways, those methods had ended up calling numerous other math methods, which most of them created multiple new Vec2 or Mat22 objects. By inlining the other math calls and only creating one object (or none for the ToOut methods), there was a significant difference, especially in the Vec2 creations per frame.
Java optimization is a bit of a black art - it depends quite critically on JVM quirks (and version/VM flags) a lot of the time. I highly recommend taking a look through the Hotspot source code at the optimization bits to see how messy it gets, esp. if you're interested in this stuff; it won't likely help you write much faster Java code, but it will give a good sense why nobody seems able to offer solid optimization advice. It's almost impossible to know without trying something whether it will help or not.

I've seen many cases where inlining method calls doesn't matter at all, and I've seen a few where it does. Generally speaking, marking methods as final shouldn't do much, as the JVM is able to determine at runtime (more precisely, at JIT compile time) whether or not a method could have been marked final and thus avoid virtual dispatch and be eligible for inlining.

However, there are a lot of quirks. I've been told that in long methods the optimizing compiler actually gives up after a certain point; Box2d has some methods that are very long, so it's definitely possible that the optimizer is stopping early. Then again, I've never actually looked into this, so I don't know for sure what the length cutoff is or if this really happens at all.

There are also lots of other situations that might trigger optimizer punts, basically anything that confuses it too much could cause it to give up and roll back an optimization. So it's not always as straightforward as the JVM either applying an optimization wherever it can or not, there can be a lot of weird edge cases.
In my opinion, you should do as much as you can to help out the compiler and hot-spot compiler make the code as fast as possible, even though it might not matter. Also, based on what I've seen, if you mark a simple static method final, then the javac complier will inline the method right in the bytecode (but i'm not sure about complicated methods, as ewjordan was saying, it might get too "confusing").

But I really don't consider myself too much of an optimization specialist, it's just that most optimization is just common sense. So I could be wrong about the whole reducing method calls and adding final, but it won't hurt to make those changes. (of course, final methods you can't override, so you have to be careful in terms of expandability). If you know any other things we could do that would make the engine faster, by all means mention them.
I think common sense can be overriden by the JIT in many cases. But of course if in some cases making methods final and reducing calls helps, then it should probably be done.

I don't have many other suggestions for optimization, except to rewrite the Matrix implementation to use 4 floats instead of 2 vectors (this means changing a lot of code though). It gave me 10+% increase in ScalaBox2D (server VM) and reduced new vector creations 17%. I think ewjordan already said he wanted to do this too.

The Scala Matrix2f is here:
http://github.com/Villane/scalabox2d/bl ... ix2f.scala
toucansam wrote:
So I could be wrong about the whole reducing method calls and adding final, but it won't hurt to make those changes. (of course, final methods you can't override, so you have to be careful in terms of expandability).

Absolutely, I agree, and I certainly don't mind putting this stuff in.

There's a fairly reasonable school of thought that says that in Java, anything that's not explicitly designed to be extended should be final, the idea being (more or less) that the true public interface of a class that you may extend consists of a lot more than merely its public methods, and that it's really a tricky business to create classes that do anything useful and may be safely extended by client code. Taking that view, very few, if any, of the leaf-level (in the inheritance tree) classes in JBox2d should not be marked final, as almost none of them can reasonably be extended by the user.

Where I tend to draw the line optimization-wise is things like bit twiddling to avoid integer arithmetic, and manually unrolling loops. Even when effective (though I've never seen cases where a replacement of a multiplication by a shift helps at all, at least in Java, loop unrolling really can make a difference in a real tight loop), these things tend to obscure code so much that it's not worth it.
Wow. Nice work with the optimizations! I just updated to the latest version and the vector creation count is now incredibly low. Probably an order of magnitude less that an older version I was using so far.

Difference between Java / Scala is now about 12% (adventage to Java) in the first 1000 steps of the pyramid tests, running with -server -XX:+DoEscapeAnalysis (JVM 6 u 14), but look at the difference in vector creation count 😮

Code:
Java: 1859 ms
first 100 steps: 250 ms
last  900: 1609 ms
Vectors created: 6919510

Scala: 2094 ms
first 100 steps: 406 ms
last  900: 1688 ms
vectors created:32821915


Also, Java seems to have a big advantage in the beginning of the test
(the bodies fall asleep at about 400 steps)
Villane wrote:
Wow. Nice work with the optimizations!

Seriously - thank you for doing the rest of this, Daniel, I never would have gotten to all of it myself.

Quote:
Difference between Java / Scala is now about 12% (adventage to Java) in the first 1000 steps of the pyramid tests, running with -server -XX:+DoEscapeAnalysis (JVM 6 u 14), but look at the difference in vector creation count 😮

That's extremely close, and given that JBox2d has gotten a lot of attention placed on the Vec2 creation, that says wonderful things about Scala. Given the logic of the engine you're probably not using too much of the heavy functional stuff that really makes Scala easier to use, which is no doubt a bit slower, but still - it's awesome to know that you can get within 10% of the same performance of Java without writing a line of Java code!

Villane, do you use an IDE to do Scala? If so, which one? I've had major problems with each of the plugins (Eclipse, Netbeans, IntelliJ), have you found that any of them accurately identify errors and give you the syntax help that you'd expect from a Java IDE?

Code:
Java: 1859 ms
first 100 steps: 250 ms
last  900: 1609 ms
Vectors created: 6919510

Scala: 2094 ms
first 100 steps: 406 ms
last  900: 1688 ms
vectors created:32821915


These tests have graphics turned off, I assume? I'd be interested to see where the C++ and AS3 versions come out, as well, if I have a free hour this weekend I'll give all of them a shot.
BTW, I just switched from rev to rev, and between the start and end of toucansam's optimizations, my timing test goes from 390 fps to 470 fps (both of which are lower than some of the numbers I've posted before, but that's because my laptop is in power saving mode right now).
ewjordan wrote:
Villane wrote:
Wow. Nice work with the optimizations!

Seriously - thank you for doing the rest of this, Daniel, I never would have gotten to all of it myself.


I'm happy to do it 😀
I'm almost done, I just have the rest of the joints to tackle (and probably the examples as well). Yeah, Vec2 creations have been cut by half in most cases, sometimes less than that. I think I want to work a little more with the contacts, as some stuff seems like it's getting created when it doesn't have to be. But I'll look into all that in my second pass of optimizations.

ewjordan wrote:
BTW, I just switched from rev to rev, and between the start and end of toucansam's optimizations, my timing test goes from 390 fps to 470 fps (both of which are lower than some of the numbers I've posted before, but that's because my laptop is in power saving mode right now).

Thats a crazy difference. When I would test it, it would get maybe 10 or 20 fps faster, but thats just because I'm on a old macbook. I wonder how it compares to the original version now. Also, does the original version have a pool for contact points too? Because contact points are another hot object (who also store 3 Vec2s), and if we can just make a simple stack pool (Just a static Stack<ContactPoint> somewhere, we don't need to get fancy with JStackAlloc), then object creations will go down a whole lot.

Also, ewjordan, is there a reason why the last domino on the second row of the domino tower isn't created? I went ahead and commented the if statement (line 125)
Code:
if (j != 1)
out so now it doesn't start collapsing. But if it wasn't supposed to be there for a reason then........whoops.

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