Compiling Java Expressions In Java

In my previous blog post I mentioned that with JCols I explored evaluating user specified JavaScript expressions in Java with Rhino. In order to make JCols as fast as possible I recently experimented with evaluating user specified Java expressions that are compiled using Java 6's JavaCompiler (that's just the name of the interface, but that's what I'll refer to it as). I've concluded the following about the JavaCompiler:

  • Although the JavaCompiler provides a means of compiling classes in a virtual filesystem (see the getStandardFileManager() method) I found the virtual filesystem to be highly abstracted out and difficult to work with. It's easier to just work with temporary files.
  • Although the JavaCompiler allows errors to be captured and analyzed in my case it was simpler to pass the errors to standard error.
  • A helpful example of compiling Java classes programmatically can be found here.

My next concern was splitting each line into an array of strings. I found that String.split("\\s+") was slower than using a precompiled expression. But I also found that either technique is slower than custom code that does the splitting by stepping through the line a character at a time.

There are probably additional performance gains to be made, but JCols is close to GNU AWK in performance now. GNU AWK is surprising hard to beat considering that the interpreter does not support JIT compilation.