This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Compiler optimisations/inlining

2 replies
Erkki Lindpere
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.

Are the Scala compiler's -optimise and -Yinline options actually
supposed to do some inlining at the moment? I have tried this with
numerous Scala versions but nothing seems to get inlined. With some
older versions it used to even crash the compiler.

Note: I'm compiling from the Eclipse IDE and set the optimise and inline
as per-project options.

For example, for the following class, nothing seems to get inlined:

case class Vector2f(x: Float, y: Float) {
@inline def +(a: Float) = Vector2f(x + a, y + a)
@inline def -(a: Float) = Vector2f(x - a, y - a)
@inline def *(a: Float) = Vector2f(x * a, y * a)
@inline def /(a: Float) = Vector2f(x / a, y / a)
...
}

I recently noticed the @inline annotation and added those to all
methods, but even that doesn't seem to cause those methods to actually
get inlined.

Erkki

Erkki Lindpere
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Compiler optimisations/inlining

Sorry, I am stupid. Of course the methods need to be final for them to
be inlined...

Erkki

Erkki Lindpere wrote:
> Are the Scala compiler's -optimise and -Yinline options actually
> supposed to do some inlining at the moment? I have tried this with
> numerous Scala versions but nothing seems to get inlined. With some
> older versions it used to even crash the compiler.
>
> Note: I'm compiling from the Eclipse IDE and set the optimise and
> inline as per-project options.
>
> For example, for the following class, nothing seems to get inlined:
>
> case class Vector2f(x: Float, y: Float) {
> @inline def +(a: Float) = Vector2f(x + a, y + a)
> @inline def -(a: Float) = Vector2f(x - a, y - a)
> @inline def *(a: Float) = Vector2f(x * a, y * a)
> @inline def /(a: Float) = Vector2f(x / a, y / a)
> ...
> }
>
> I recently noticed the @inline annotation and added those to all
> methods, but even that doesn't seem to cause those methods to actually
> get inlined.
>
> Erkki
>

Erkki Lindpere
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Compiler optimisations/inlining

It doesn't seem to work correctly though (with 2.7.4):

Exception in thread "main" java.lang.IllegalAccessError: tried to access
field Vector2f.x from class X

Erkki

Erkki Lindpere wrote:
> Sorry, I am stupid. Of course the methods need to be final for them to
> be inlined...
>
> Erkki
>
> Erkki Lindpere wrote:
>> Are the Scala compiler's -optimise and -Yinline options actually
>> supposed to do some inlining at the moment? I have tried this with
>> numerous Scala versions but nothing seems to get inlined. With some
>> older versions it used to even crash the compiler.
>>
>> Note: I'm compiling from the Eclipse IDE and set the optimise and
>> inline as per-project options.
>>
>> For example, for the following class, nothing seems to get inlined:
>>
>> case class Vector2f(x: Float, y: Float) {
>> @inline def +(a: Float) = Vector2f(x + a, y + a)
>> @inline def -(a: Float) = Vector2f(x - a, y - a)
>> @inline def *(a: Float) = Vector2f(x * a, y * a)
>> @inline def /(a: Float) = Vector2f(x / a, y / a)
>> ...
>> }
>>
>> I recently noticed the @inline annotation and added those to all
>> methods, but even that doesn't seem to cause those methods to
>> actually get inlined.
>>
>> Erkki
>>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland