Does the @inline annotation in Scala really help performance?

performance, scala, scala-2.8

Solution

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.

Problem

Or does it just clutter up the code for something the JIT would take care of automatically anyway?

Original source