Search code examples
scalaannotationsinlinescala-2.10

Requesting more information about @inline from the compiler?


The documentation for @inline states:

An annotation on methods that requests that the compiler should try especially hard to inline the annotated method.

However, unlike the similar @tailrec annotation, the compiler does not (by default) offer any information about whether or not it managed to inline a method.

Is there any way to determine if the compiler managed to inline an annotated method?

Specifically, I'd like for the compiler to tell me, for example, that in all reasonable cases it will be able to inline a method I marked. (Some situtations I can think of where it would warn me that it cannot inline a method is if it is not final, and hence requires a vtable lookup if the class is subclassed)

Related questions:


Solution

  • First, you need to remember that Scalac will only try to inline things when you compile with -optimise (or -Yinline I think).

    Consider the following simple case:

    class Meep {
      @inline def f(x: Int) = x + 19
    }
    
    object Main extends App {
      new Meep().f(23)
    }
    

    If I compile that with -optimise, Scalac will gives me a warning: there were 1 inliner warnings; re-run with -Yinline-warnings for details. Now, apart from the grammar giggle, this didn't give me much.

    So let's recompile with -Yinline-warnings. Now I get: At the end of the day, could not inline @inline-marked method f. Uh, OK, that was not very helpful either, but I guess that's what I get for using a private compiler flag. :) Some of the inline warnings are a tiny bit more helpful, by the way - like: Could not inline required method f because bytecode unavailable. (which happens in the REPL)

    The compiler help explains -Yinline-warnings as Emit inlining warnings. (Normally surpressed due to high volume), so I guess it has to be used on a case-by-case basis.

    Anyway, if we change the definition of f in the above snippet to @inline final def f(x: Int) = x + 19, the inline warning will go away and the method will be properly inlined.

    Hope that helped a bit.