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

元气小坏坏 提交于 2020-01-01 07:56:11

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/14762094/should-i-use-groovys-compilestatic-if-im-also-using-java-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!