Does the @inline annotation in Scala really help performance?

夙愿已清 提交于 2019-11-28 16:47:53

I have yet to find a case where it improves performance, and I've tried in quite a few different spots. The JVM seems to be quite good at inlining when it's possible, and even if you ask for @inline in Scala, it can't always do it (and sometimes I've noticed that it doesn't even when I think it ought to be able to).

The place where you expect to see a bytecode difference is in something like this:

object InlineExample {
  final class C(val i: Int) {
    @inline def t2 = i*2
    @inline def t4 = t2*2
  }
  final class D(val i: Int) {
    def t2 = i*2
    def t4 = t2*2
  }
}

when compiled with -optimise. And you do see the difference, but it generally doesn't run any faster since the JIT compiler can notice that the same optimizations apply to D.

So it may be worth a try in the final stages of optimization, but I wouldn't bother doing it routinely without checking to see if it makes a difference in performance.

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