[scala-tools] Compiler optimisations/inlining

View: New views
4 Messages — Rating Filter:   Alert me  

[scala-tools] Compiler optimisations/inlining

by Erkki Lindpere-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: [scala-tools] Compiler optimisations/inlining

by Erkki Lindpere-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
>

Re: [scala-tools] Compiler optimisations/inlining

by Erkki Lindpere-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
>>
>

Parent Message unknown Re: [scala-tools] Compiler optimisations/inlining

by Erkki Lindpere-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the info, seems to work in Scala 2.8

However, I'm not noticing much impact (maybe 1%, but that's hard to
measure). I guess my bottlenecks are elsewhere or I've already
hand-inlined enough to make this not have much of an impact.

Also, noticed that in the generated bytecode, for
final case class Vector2f(final val x: Float, final val y: Float) {...},
x and y are inconsistently accessed with either getfield or
invokevirtual. This most likely gets optimized away by JIT, though? I'll
report a bug once I have a complete test case.

Erkki

Iulian Dragos wrote:

> On Wed, May 20, 2009 at 2:10 AM, Erkki Lindpere <erkki@...> wrote:
>  
>> Are the Scala compiler's -optimise and -Yinline options actually supposed to
>> do some inlining at the moment?
>>    
>
> Yes, the @inline annotation is used to override the heuristics for
> deciding when to inline a method. Of course, if it's not safe it won't
> do it regardless of the annotation.
>
> 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.
>>    
>
> That's not what they were supposed to do, of course. Damn bugs.
>
> The trunk version has many bug fixes in the optimiser. The
> 'IllegalAccessError' you report is such a bug, but I cannot tell
> unless you provide us with a complete example or a ticket.
>
> Thanks,
> iulian
>
>