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

2

13

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.

Bracci answered 7/2, 2013 at 22:17 Comment(0)
F
13

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

Fulguration answered 19/2, 2013 at 14:8 Comment(1)
Good answer. The part that I missed from the wording in the release is that they are independent features. So even if using Java 7 with -indy, CompileStatic can give a perf boost (as long as the code can actually be type checked by the compiler). We did do a quick test of the different permutations with indy and CompileStatic with Fibonacci and found the following:Bracci
L
5

To boil it down, @CompileStatic removes some Groovy dynamic runtime functionality in favor of speed.

So, theoretically:

  • JDK7 should be faster than JDK6 because of invokedynamic
  • @CompileStatic should be faster than Standard because some overhead and features are removed.

All that with the caveat that it will largely depend on what you are doing, as various features are optimized to different degrees.

Laurielaurier answered 21/2, 2013 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.