Experimental Scala port

Discuss issues specific the Scala port of Box2D
Villane
Posts: 101
Joined: Mon Sep 01, 2008 6:32 am

Re: Experimental Scala port

Postby Villane » Sun Jun 14, 2009 1:11 pm

Aargh... I think I may have to roll back this change. It complicates user code a bit too much: implicit conversions from (Float,Float) to Vector2 won't work when using Doubles, and it seems you can't define multiple implicit conversions from Pairs of primitives (it will say that all of them apply and so can't compile)

Also, it's a bit of a pain to remember to add the :Scalar type annotation to Floats everywhere to support other number types.

What do you think? Is the additional complication worth it to be able to really easily switch from Float to Double or Fixed Point types?

Will anyone actually need Double? Maybe Box2D doesn't gain so much from that?
Will anyone actually need Fixed Point? From what I've read, Android's software Float may be faster than fixed point.

ewjordan
Posts: 803
Joined: Sun Sep 23, 2007 2:35 pm

Re: Experimental Scala port

Postby ewjordan » Sun Jun 14, 2009 3:06 pm

That's strange about the conversions from primitives...it looks like if you type the conversion as Tuple2[Double,Double] => Vector2 it will also work for Floats, but if you have both something blows up. In fact, playing around a little bit, it seems that any multiple conversions from a tuple of the same length to some type will cause problems. I guess this is probably JVM type erasure causing problems in Scala?

Villane wrote:Will anyone actually need Double? Maybe Box2D doesn't gain so much from that?

I don't think so. I've never seen an floating point resolution issues in Box2d, has anyone?
Will anyone actually need Fixed Point? From what I've read, Android's software Float may be faster than fixed point.

Can't comment there, don't have an Android device. I've never heard that particular claim, I was always under the impression that the software float emulation was really slow, but I could definitely be wrong.

With JBox2d I'm not aiming anything at Android at least until I have one of the devices; it's so hard to guess at what performance will be like without being able to test it.

Villane
Posts: 101
Joined: Mon Sep 01, 2008 6:32 am

Re: Experimental Scala port

Postby Villane » Sun Jun 14, 2009 3:54 pm

ewjordan wrote:That's strange about the conversions from primitives...it looks like if you type the conversion as Tuple2[Double,Double] => Vector2 it will also work for Floats, but if you have both something blows up. In fact, playing around a little bit, it seems that any multiple conversions from a tuple of the same length to some type will cause problems. I guess this is probably JVM type erasure causing problems in Scala?


I think you are right about the same length of Tuple to same result type conversion. And yes, it's a type erasure thing.

Anyway, if doubles are not useful, I think I'm not merging this change until someone can try it on Android. It's not terribly difficult to reproduce later (a text replace on the whole source and maybe a couple of hours of manually adding type annotations :)).

Villane
Posts: 101
Joined: Mon Sep 01, 2008 6:32 am

Re: Experimental Scala port

Postby Villane » Tue Jun 16, 2009 9:29 am

Some more performance numbers with different VM settings on Java 1.6 Update 14 (pyramid test 1000 steps):

ScalaBox2D

Client VM (no args)
7313 ms
first 100: 891 ms
last 900: 6422 ms
vectors created:32748944

Server VM (-server)
2468 ms
first 100: 421 ms
last 900: 2047 ms
vectors created:32748944

Server VM with Escape Analysis (-server -XX:+DoEscapeAnalysis)
2141 ms
first 100: 391 ms
last 900: 1750 ms
vectors created:32748944

I don't remember ever seeing such differences between -client/-server in JBox2D (or in fact any Java application). Does this mean the Scala code/immutable vectors is actually more optimizable by JIT? Probably more that ScalaBox2D is wasteful, but the Java version is now internally so optimised that JIT can't improve it that much.

JBox2D

Client VM (no args)
2703 ms
first 100: 282 ms
last 900: 2421 ms
Vectors created: 6919510

Server VM (-server)
1906 ms
first 100: 266 ms
last 900: 1640 ms
Vectors created: 6919510

Server VM with Escape Analysis (-server -XX:+DoEscapeAnalysis)
1859 ms
first 100: 250 ms
last 900: 1609 ms
Vectors created: 6919510

Zzzzrrr
Posts: 103
Joined: Sat Mar 15, 2008 5:49 am
Location: Washington, DC
Contact:

Re: Experimental Scala port

Postby Zzzzrrr » Tue Jun 16, 2009 9:37 am

Nice job! The server VM performance difference between jBox2D and scalaBox2d is pretty much negligible... Hopefully as Scala matures the client VM gap will also close.

Any hint in regard to the disparity between client and server?

Villane
Posts: 101
Joined: Mon Sep 01, 2008 6:32 am

Re: Experimental Scala port

Postby Villane » Tue Jun 16, 2009 12:28 pm

Zzzzrrr wrote:Nice job! The server VM performance difference between jBox2D and scalaBox2d is pretty much negligible... Hopefully as Scala matures the client VM gap will also close.

Any hint in regard to the disparity between client and server?


The Scala port creates A LOT of temporary objects. Not only vectors, but also Manifolds and some other objects. I'm pretty sure this is the cause (Garbage collection), although I haven't looked at GC time. But if its so close to Java on the server VM at least, I don't really want to go changing it.
If I start closely copying what JBox2D does, then this port becomes pointless as people could just use JBox2D from Scala. The fact that most things are immutable means I have much less things to keep in mind (like when is it ok to mutate an object) and that helps me a lot.

Once there is a release of ScalaBox2D, I think I would recommend people to start with ScalaBox2D if they like Scala, but switch to JBox2D if they run into performance issues. And on mobile devices, JBox2D is probably a lot more sensible.

ewjordan
Posts: 803
Joined: Sun Sep 23, 2007 2:35 pm

Re: Experimental Scala port

Postby ewjordan » Tue Jun 16, 2009 12:45 pm

Zzzzrrr wrote:Any hint in regard to the disparity between client and server?

On OS X with Java 6 (beta) there is almost no difference between the two, but IIRC that's because Apple decided they didn't want to maintain the client VM separately from the server one, so it probably doesn't mean anything at all.

To me there's not even any need for the client VM anymore, the original theory was that the server VM caused too many pauses to compile methods, but these days the pauses are imperceptible and the overall speed gains are huge when you use -server mode. Unfortunately for applets it's not even an option, which is a real shame, but generally speaking I tune things to the server VM, not the client one (my bet is on the server VM taking over completely within a few years).

If I had to guess, I'd say that part of what's happening here is that the Scala compiler generates a lot of extra stuff in the bytecode (not surprising since Scala is a fully functional language), which the server compiler can easily get rid of or at least optimize very well. But when the client compiler hits it, it does its minimal optimization pass, touches a few things, and then gives up (the client's "give up" threshold is very low, from what I remember).

Have you tried turning on the Scala compiler optimizations? I've never experimented there so I don't know how well they work, but it might help a bit.

Villane wrote:And on mobile devices, JBox2D is probably a lot more sensible.

Well, not yet, at least until I either get a mobile device worth programming for other than an iPhone or someone else does a fixed point port. We'll see...my current feeling is that maybe worrying about fixed point may be short sighted anyways, since the next few gens of phones are going to be trying to duke it out with the iPhone and will probably start including floating point units and GPUs as standard features.

Villane
Posts: 101
Joined: Mon Sep 01, 2008 6:32 am

Re: Experimental Scala port

Postby Villane » Wed Jun 17, 2009 2:12 am

ewjordan wrote:Have you tried turning on the Scala compiler optimizations? I've never experimented there so I don't know how well they work, but it might help a bit.


Yes, I've tried. No measurable change (with -server, I'll give it a shot with -client). Initially, I forgot to make methods final so the compile time inlining didn't work at all. I still need to add final to a lot of methods/classes.

Villane
Posts: 101
Joined: Mon Sep 01, 2008 6:32 am

Re: Experimental Scala port

Postby Villane » Wed Jun 17, 2009 12:46 pm

ewjordan wrote:
Zzzzrrr wrote:(my bet is on the server VM taking over completely within a few years)


That would be good. Another thing I think would really help JVM games is value types. I think it was proposed for Java 7 (or maybe some people just said they wanted it), but didn't make it in. It should be great for things like vectors and matrices.

ewjordan
Posts: 803
Joined: Sun Sep 23, 2007 2:35 pm

Re: Experimental Scala port

Postby ewjordan » Wed Jun 17, 2009 12:56 pm

Villane wrote:Another thing I think would really help JVM games is value types. I think it was proposed for Java 7 (or maybe some people just said they wanted it), but didn't make it in. It should be great for things like vectors and matrices.

I 100% agree, but I don't see it happening unless there's a major philosophy shift high up in the organization (possible, since Sun is getting swallowed by Oracle). All the official responses on that issue make it clear that the people deciding these things think it's a solved problem, and even if it's not (which they will not admit) it's not an important enough problem to modify the language itself for.

Personally, I'd be happy if they merely provided immutable big ints, tuples, and maybe matrices as another primitive type (with the appropriate operator overloads), since it's mostly just complex numbers, vectors, matrices, and big integers/decimals that make people want this stuff so desperately to handle.

Still, vote for the proposal if you haven't already, at least make them realize that as long as they're ruling against it they're inconveniencing a lot of people!


Return to “Scala”



Who is online

Users browsing this forum: No registered users and 1 guest