Gesticulations and Other Geekery » Posts in 'Scala' category

Ray casting in Scala

Ray casting is an old rendering technique used in early “3D” games like Wolfenstein 3D. Today it is nothing much, but implementing it will test your knowledge of trig and binary rounding issues. The real reason I wrote it though was because I have tried to implment a raycasting engine several times before and failed. Until now! So it was a matter of geek pride to go back to this problem and defeat it.

I implemented the engine in as functional a way as possible. I think it came out fairly well. I did not optimize anything for performance. So I used Double values for the most part. This is very different than most implementations you see online that implement the entire algorithm with ints and use look up tables for the trig functions. One issue that I had run into before is that because parts of the algorithm are fundamentally quantized (the map cells) floating point can actually cause problems like disappearing walls or holes in walls. I was able to work around these issues through the creative use of rounding and a fudge factor (to make sure the value was on the correct side of a boundry). Regardless it works now and I’m glad I defeated the algorithm.

The project is available in github:

Posted in Scala

Java for Google App Engine

When Google App Engine first came out I was really surprised it didn’t support Java, because Google was (and is) really pushing GWT, so to have them release a web deployment platform that did not support their own favorite framework was odd. However they have now released a preview version of Google App Engine running Java. It doesn’t do everything I might want, for instance Lift doesn’t run on it out of the box and it doesn’t allow you to spawn your own threads (these problems are directly related). There is some work going on to make Lift work on it though (see the email thread).

Overall though I’m really excited. I have been looking for a platform that will allow me to run Java based web apps for a reasonable price and I think this is the first one that provides something that I may actually use. For a low traffic site it is free and it looks to me that the prices are pretty good even as the traffic increases.

Posted in Java, Scala

Inlining “objects” in the JVM

[EDIT: It has been pointed out that the correct term for what I am talking about is Scalar Replacement. Also it seems that Java 7 already has some code to support it in the JIT compiler. Thanks.]

One of the interesting features of Scala is the ability to use implicit conversions to “add” methods to existing classes. A call to “x.func” will try to convert x to a type with a method func if it doesn’t naturally have one. The problem is that each time this happens it ends up allocating a temporary wrapper object. This is similar to Java idioms like:

long time = new Date().getTime();

In both cases an object is allocated and then is immediately ready to be garbage collected. Whenever I think about this I feel that the JVM could optimize it away. Maybe something like this.

The JVM discovers (during escape analysis, which I think it already does) that the object is short lived and local. It then inlines the small number of method calls (often just one, especially in the case of scala implicit conversions). When it sees that every method call on the object is inlined it inlines the constructor and converts any fields of the class into local variable and removes the allocation entirely.

This wouldn’t be easy. But it doesn’t seem like it would be too incredibly hard. Though I’m not a JVM programmer so I don’t really know.

Maybe I should give it a shot. I’ve never hacked on Hotspot and I’ve always kinda wanted to.

Any thoughts on this?

Posted in Java, JVM, Scala