Like I've said earlier, I would like to optimize ScalaBox2D (mostly to reduce vector creations), while leaving the math code as readable as possible. Unfortunately, so far I've gotten most improvement from manually inlining the math.
I was really pleasantly suprised how much JBox2D could reduce vector creations by using *ToOut methods in calculations. But I don't want to do the same in Scala, I want to keep the math code as clean as I can, still use binary operators (and get rid of the manual inlining at some point). I'm not really happy that the current choice in the Scala port is to either write clean math code or to write fast math code, not both at the same time.
So I think there are basically 3 options:
What do you think? I started playing with ASM, but while it is easy to get started with it, it looks like too low level and I'm currently leaning towards compiler plugin.
The Lazy calculations would look something like this:
So this is currently even slower. But this might be faster with the inlining the compiler/JIT does (haven't tested yet):
I was really pleasantly suprised how much JBox2D could reduce vector creations by using *ToOut methods in calculations. But I don't want to do the same in Scala, I want to keep the math code as clean as I can, still use binary operators (and get rid of the manual inlining at some point). I'm not really happy that the current choice in the Scala port is to either write clean math code or to write fast math code, not both at the same time.
So I think there are basically 3 options:
- Write an optimizing compiler plug-in which does roughly the same optimizations as manual inlining. This means getting to know the Scala compiler. Might be a lot of work, but in the end it also might be easier than the other options.
- Write a byte-code manipulator (with ASM?) that does the same thing that the compiler would. This is initially easier (more documentation & samples available) but might turn out more difficult in the end due to working on a lower level than the result of some intermediary compiler phase.
- Try to come up with some mutable vector API that remains similar to the current one. The only one I've come up with so far uses Lazy calculations structures, but they can easily create even more temporary objects and may end up having worse performance in some cases.
What do you think? I started playing with ASM, but while it is easy to get started with it, it looks like too low level and I'm currently leaning towards compiler plugin.
The Lazy calculations would look something like this:
Code: Select all
trait V2 {
def x: Float;
def y: Float;
def +(v: V2) = AddVV(this, v)
def -(v: V2) = SubVV(this, v)
def +(a: Float) = AddVF(this, a)
// ...
}
abstract class BinOpVV(v1: V2, v2: V2)
case class AddVV(v1: V2, v2: V2) extends BinOpVV with V2 {
def x = v1.x + v2.x
def y = v1.y + v2.y
}
abstract class BinOpFV(a: Float, v: V2)
class BinOpFV(a: Float, v: V2)
case class CrossFV(a: Float, v: V2) extends BinOpFV with V2 {
def x = -a * v.y
def y = a * v.x
}
// ... lots of calculation classes
class MutableV2 extends V2{
var x = 0f; var y = 0f
def :=(v: V2) = { x=v.x; y = v.y; }
def :=(_x: Float, _y: Float) = { x= _x; y = _y; }
}
val dv = new MutableV2
// this expression
dv := v2 + w2 × ccp.r2 - v1 - (w1 × ccp.r1)
// would then turn into this lazy calculation structure:
dv := SubVV(SubVV(AddVV(v2, CrossFV(w2, ccp.r2)), v1), CrossFV(w1, ccp.r1))
So this is currently even slower. But this might be faster with the inlining the compiler/JIT does (haven't tested yet):
Code: Select all
dv := v2
dv += w2 × ccp.r2
dv -= v1
dv -= w1 × ccp.r1