Friday, July 2, 2010

Value types in java

Value types are crucial feature overlooked by the jvm and the java language.

With value types writing high performance, low allocation, cache friendly code is much easier.

For instance: in a physics application you cannot allocate Vector3d on the heap every time you need one. In java, to prevent 1.000.000 allocations per second one has to write convoluted code that reuses vectors every time. With value types this is not necessary, the new keyword does not allocate value types on the heap but on the method stack.

And don't believe when someone says that the JIT will optimize this for you, it is just not true. There are several dataflow patterns that the JIT cannot follow to optimize. Also immutable classes do not fully solve the problem (if you trust that the JIT will understand them), they just create more problems for the programmer.
An immutable Matrix4d is more expensive to update than a mutable one.

I've come across this problem writing game code (physics and rendering) in java.

No comments:

Post a Comment