Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the 'optimise' scala compiler flag do?

Tags:

I've tried using scalac -optimise with version 2.7.7. At that time I never got any performance improvements, but the compilation took longer.

Is the situation better in Scala 2.9.0 ? What optimisations are currently covered by the flag ?

like image 959
paradigmatic Avatar asked Jul 15 '11 08:07

paradigmatic


People also ask

What does optimization flag do?

Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program. The compiler performs optimization based on the knowledge it has of the program.

What does an optimizing compiler do?

In computing, an optimizing compiler is a compiler that tries to minimize or maximize some attributes of an executable computer program. Common requirements are to minimize a program's execution time, memory footprint, storage size, and power consumption (the last three being popular for portable computers).

What is used to optimize compiler?

The code optimization in the synthesis phase is a program transformation technique, which tries to improve the intermediate code by making it consume fewer resources (i.e. CPU, Memory) so that faster-running machine code will result.

What does gcc optimization do?

The compiler optimizes to reduce the size of the binary instead of execution speed. If you do not specify an optimization option, gcc attempts to reduce the compilation time and to make debugging always yield the result expected from reading the source code.


1 Answers

Strange, there was a similar question on the scala-user group:

Rex Kerr answered at the time (May):

I have never found a case where production code was significantly sped up by using -optimise, at least when using the Sun JVM. I grant that in some cases it could happen, but it seems to apply similar optimizations to what the JVM does already. Perhaps if there are limits on optimization depth, using -optimise would remove a few layers and then allow the JVM to get a few more. But I mostly don't even bother testing it any more, given how many cases (dozens) I've tried it where the runtime hasn't changed measurably.

I expect it would have an impact on VMs that are more conservative (e.g. JRockit) or less sophisticated (e.g. Dalvik).


Ismael Juma added:

The scala distribution is actually compiled with -optimise, but indeed it's not on unless the argument is passed to scalac.

Is it not mature enough, or could it lead to bugs, changed semantic, etc ?

As far as I understand, the team decided to be conservative and enable it only for the scala distribution as an initial step (in Scala 2.8.0). Maybe it's a good idea to consider extending that in the next major release.


For the influence of '-optimise' (amongst other factors) in Scala2.9, see this scala-language thread called "Scala2.9 slower?".

I'm a bit concerned. After seeing that -optimize really does optimize "for comprehensions" on 2.9.0, I then benchmarked the code, and discovered it was about 2.5 times slower than 2.8.1.

The results leave a... mixed feeling.

like image 102
VonC Avatar answered Sep 23 '22 01:09

VonC