Tuesday, August 30, 2011

Running Scala via Nailgun

Recently a friend of mine got me interested in Scala, a modern programming language for JVM. Scala reached a wide audience in 2009 when Twitter announced that they use Scala in the most critical elements of their backend. It became popular for its efficiency and scalability, and for joining in a very pragmatic manner the concepts of functional and object oriented programming. It also implements some very powerful constructs, like foldLeft, which allows you to compose your own reduction methods operating on lists (see this page for some very interesting examples).

I decided to give it a try on my Acer Aspire One netbook, which I use often to do some daily tasks. Unfortunately, with 1.6 GHz Intel Atom and 1GB RAM running Fedora 14 it took Scala REPL almost 20 seconds to start. I was wondering if I could make it faster and found out that some people recommended Nailgun for this task. In short, Nailgun preloads the whole JVM into the memory and than uses its own bootstrap loader to connect your programs to it, so that they load much faster, without JVM startup overhead. However, besides some good advice I couldn't find any working example how to do it, so I decided to do it myself. Fortunately it turned out to be a quite easy task. First, I copied nailgun-0.7.1.jar to Scala's lib directory, then I took the original bin/scala script and created a modified version. I changed line 161 from
scala.tools.nsc.MainGenericRunner  "$@"
to
com.martiansoftware.nailgun.NGServer  "$@"
and line 113 from
CPSELECT="-Xbootclasspath/a:"
to
CPSELECT="-cp "
The last change was necessary for Nailgun to load properly. Then I started regular Scala REPL through
ng scala.tools.nsc.MainGenericRunner
just to learn that it still takes 20 seconds to start. Conclusion? Modern JVM loading overhead is so small that reducing it further makes little sense, even on slow machines.
Lessons learned:
1) Using Nailgun to speed up Scala won't get you anywhere.
2) Don't listen to forum "experts" who recommend solutions which they have obviously never tried in practice.

1 comment:

scalable said...

The funny thing is that on a recent MacBook Pro, it really does speed things up significantly -- REPL shows up almost instantly! So, to your morale of the story, I'd add: test on diverse systems before giving up! :)