Friday, July 6, 2012

The new GWT 2.5 closure compiler

Although GWT 2.5.0 has not been officially released yet, the release candidate has been uploaded to central maven repositories.

GWT 2.5.0 comes with many useful features, and one of them is that the compiler produces  much less code than in the previous version.

I've re-compiled two Talkwheel applications (desktop and mobile) with the new version, and without changing a single line of code, I've got a 20% of reduction ! .

But one of the hidden features in the new GWT is that gwt guys have added a new experimental compiler which reduces a bit more the size of javascript. The compiler is the well known google closure compiler which compiles from javascript to javascript producing a better code,  not only compressing the code but checking the syntax and variable references, removing death code, etc.

I've been experimenting with it and I've got a reduction of a 25% on the projects I mentioned, which  improves the normal GWT compiler in a 5%.

What I've done as well, is to compare the size of the compressed js in both cases, and surprisingly the code produced by the closure code compressed is bigger than the gwt one.
This is not a big issue since the difference is just an 1%, but it demonstrates how the gwt compiler prepares the output so as it is optimized for compression, this was a nice feature introduced in 2.0.

I've been playing with these applications compiled with the default and the closure compilers, and I've not realised any noticeable difference in performance.

Finally, If you are using maven and you want to enable the closure compiler you can either: wait until gwt-maven-plugin 2.5.0 is released, or use this workaround in your pom.xml file:

<plugin>
 <groupid>org.codehaus.mojo</groupid>
 <artifactid>gwt-maven-plugin</artifactid>
 <version>2.4.0</version>
 <configuration>
  <style>OBF' -XenableClosureCompiler '-XenableClosureCompiler</style>
  ...
 </configuration>
 ...
</plugin>
Note the single quote, it works in linux and mac, but in windows maybe you have to replace them with double quotes.