Thursday 20 August 2009

Groovy Precompiling



Maybe anybody who is interested in Groovy can make a few comments about this.


The requirement is that we offer functionality that extends what exists in core functionality of a java web application surrounding the persistence lifecycle.

So I added functonality which, will execute a groovy script which has been associated with an event and object type EG preupdate and Widget. The script also needs to be updateble within the session lifecycle.

This works well and adds loads of flexiblity but there will be an obvious performance hit owing to the creation of a groovy shell and execution of the script during the persistence lifecycle.

So my collegue suggested we look into precompilation of the scripts into classes and executing these in a native java environment rather than creating a Groovy shell and running the script, which will need to be dynamically compiled every time the script executes.

This was achieved in essence by creating an instance of the compiler , compiling the script to a class file and storing it somewhere pre-execution, and then when the script needed to be executed, Loading the class (Will always be a subclass of the Script class) and calling the run() method


So to compile we we do this and the resultant output is a .class file.


CompilerConfiguration cc = new CompilerConfiguration();
cc.setTargetDirectory(destdir);
cc.setScriptBaseClass("groovy.lang.Script");

Compiler c = new Compiler(cc);

c.compile(myClassName,myScript);


Now we need to retrieve the class and for that we need a ClassLoader, so I extended the ClassLoader to create a FileClassLoader which finds the file on the file system, creates a byte array from the file, which it passes into the defineClass method of the ClassLoader class definition.

We create the script by creating a new instance of the class and clalling the run() method on it:


FileClassLoader fcl = new FileClassLoader();

start = System.currentTimeMillis();

Class cls = fcl.loadFromFile(DIR + "//DIR//" + classname + ".class", classname);

Binding b = new Binding();
b.setProperty("obj","compiled");

Script scr = (Script) cls.newInstance();
scr.setBinding(b);
scr.run();

The result of this is the as doing this :

GroovyShell gs = new GroovyShell();
gs.setVariable("obj","interpreted");
gs.evaluate(script);

The theory is that every time you pass the script in a string variable to a shell it needs to compile and then execute the script and precompiling will prevent the need for this, thus providing a performance gain as essentially, it not executing a script which needs to be dynamically compiled, but is executing precompiled code.

I tested for performance and since I have the groovy plugin installed in my Eclipse IDE, the script is compiled and cached even when using the shell so there is not real performance gain when I test in the IDE, but will this be a more efficient way of using Groovy on the webserver?

Will update this post when I have an answer

No comments: