Should I use Groovy's @CompileStatic if I'm also using Java 7

Scott picture Scott · Feb 7, 2013 · Viewed 8.5k times · Source

I've read through the "What's new in Groovy 2.0" and I'm a bit confused about when to use @CompileStatic. The article mentions that the @CompileStatic annotation was added for developers who weren't able to take advantage of the invoke dynamic part of Java7.

So developers looking for performance improvements would not see much changes in Groovy 2.0, if they aren’t able to run on JDK 7. Luckily, the Groovy development team thought those developers could get interesting performance boost, among other advantages, by allowing type checked code to be compiled statically.

My question is, if I'm using JDK 7 and I follow the instructions to add the --indy flag, do I need to add @CompileStatic to see some performance increases? This blog suggests I would, but I'm not sure he compiled correctly given that he did it within Eclipse.

Update: Here are the stats when running different permutations of the Fibonacci code.

> groovy --indy FibBoth.groovy
..........Fib (non-static indy): 1994.465
..........Fib (static indy): 529.197

> groovy FibBoth.groovy       
..........Fib (non-static): 1212.788
..........Fib (static): 525.671

Note: this question seems a little confusing now that I understand that the features are independent. Since the basis of the question is around the confusion from the notes that made me think the two features were related I think it makes sense not to change the question wording and allow the accepted answer that explain the differences.

Answer

blackdrag picture blackdrag · Feb 19, 2013

The indy version is fully dynamic Groovy, as it is known, only faster thanks to JDK 7 invokedynamic. @CompileStatic means static compilation. While faster than normal Groovy, it can compile only a subset of Groovy and behaves a bit different. Especially all the dynamic features are not available anymore. So if you want to use dynamic features then indy is the only option. If you will be a static guy and only use a small part of the language, then @CompileStatic can be used.

Fibonacci is btw not a test in which invokedynamic can shine. The indy port is not always better. But if you do for example a lot with meta programming, then indy will be better.

You have to decide according to your usage in the end