Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure application startup performance

I have written a few small utility applications in Clojure that I compile into self-contained executable JAR files ("uberjars") using Maven and the maven-shade-plugin. These uberjars contain unpacked versions of clojure.jar and other libraries (i.e.: commons-cli) that the application depends on. They are convenient because I can send them to a customer without requiring that the customer install Clojure (all customers already have the JRE installed).

I have found that the Clojure applications take several seconds to start up, whereas similar applications written in Java start in sub-seconds on the same machines (time to show a usage message, for example).

I suspect that it is because Clojure is on-the-fly compiling some of the code in the clojure.core library as there is source code (.clj files) in the clojure.jar file.

Is there any way to precompile this source code? Can anything else be done to speed up startup performance? I have heard complaints from the customers about how long the startup takes (and they don't know or care that the application is written in Clojure or Java or Foobar).

like image 409
Ralph Avatar asked May 16 '11 11:05

Ralph


2 Answers

JVM (at least Oracle's HotSpot) makes very tricky thing to reduce startup time. It doesn't load to memory all program's classes and methods, it loads only resources it needs right now. There are not so many code needed to show a usage message or something like that, so only few functions are actually loaded and Java program gets started quickly. Moreover, HotSpot doesn't even compile these few functions - it uses JIT compilation (and other optimization) for the code, which is executed repeatedly. There's no reason to spend time to compile functions that will be executed only once, e.g. almost all startup methods, and HotSpot doesn't.

So, what about Clojure? I don't think you would like to rewrite Clojure's core to add similar functionality. Nevertheless, you can use same approach inside of your Clojure code. You said your utilities use several libraries, that can slow down startup. So, load libraries lazily as much as you can. For example, you can exclude :use option from your namespace definition and call explicit use in your principal functions instead. This won't reduce total time, but it will shift dalay to the moment, when it isn't so appreciable. You can even write small part of your program in Java and call Clojure code only when it is actually needed.

like image 172
ffriend Avatar answered Sep 23 '22 03:09

ffriend


On the Clojure site there is a nice description of AOT compilation. This will already shave off some startup time.

Edit: there have been some efforts to run Clojure programs on a persistent JVM, thus reducing the start-up time. Look-up jark + jvm. However the site seem to have disapeared :(

like image 32
Maurits Rijk Avatar answered Sep 22 '22 03:09

Maurits Rijk