Skip to content

Commit

Permalink
Enable inlining in constructors.
Browse files Browse the repository at this point in the history
Inlining in constructors has been disabled a long
time ago due to some VerifyErrors. Unfortunately,
@dragos cannot recall what exactly was the problem.
I tried to enable inlining in constructors and I
didn't see any problem.

`Predef.assert` calls in class constructors are one
of the biggest contributors to closure allocation
in a compiler so we better off get rid of it.

Added a test-case that checks if inlining in
constructors works properly.

Review by @magarciaEPFL and @paulp.
  • Loading branch information
gkossakowski committed Aug 7, 2012
1 parent d8b35a1 commit 4caa766
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/scala/tools/nsc/backend/opt/Inliners.scala
Expand Up @@ -247,7 +247,7 @@ abstract class Inliners extends SubComponent {
debuglog("Analyzing " + cls)

this.currentIClazz = cls
val ms = cls.methods filterNot { _.symbol.isConstructor } sorted imethodOrdering
val ms = cls.methods sorted imethodOrdering
ms foreach { im =>
if(hasInline(im.symbol)) {
log("Not inlining into " + im.symbol.originalName.decode + " because it is marked @inline.")
Expand Down
3 changes: 3 additions & 0 deletions test/files/instrumented/inline-in-constructors.check
@@ -0,0 +1,3 @@
Method call statistics:
1 instrumented/Bar.<init>(Z)V
1 instrumented/Foo.<init>(I)V
1 change: 1 addition & 0 deletions test/files/instrumented/inline-in-constructors.flags
@@ -0,0 +1 @@
-optimise
13 changes: 13 additions & 0 deletions test/files/instrumented/inline-in-constructors/assert_1.scala
@@ -0,0 +1,13 @@
package instrumented

object MyPredef {
@inline
final def assert(assertion: Boolean, message: => Any) {
if (!assertion)
throw new java.lang.AssertionError("assertion failed: " + message)
}
}

class Foo(x: Int) {
MyPredef.assert(x > 0, "not positive: " + x)
}
7 changes: 7 additions & 0 deletions test/files/instrumented/inline-in-constructors/bar_2.scala
@@ -0,0 +1,7 @@
package instrumented

/** Class that uses assert compiled in previous compiler run so we check if
inlining in constructors works across different compilation runs */
class Bar(x: Boolean) {
MyPredef.assert(x, "not true: " + x)
}
15 changes: 15 additions & 0 deletions test/files/instrumented/inline-in-constructors/test_3.scala
@@ -0,0 +1,15 @@
import scala.tools.partest.instrumented.Instrumentation._
import instrumented._

object Test {
def main(args: Array[String]) {
// force predef initialization before profiling
Predef
MyPredef
startProfiling()
val a = new Foo(2)
val b = new Bar(true)
stopProfiling()
printStatistics()
}
}

0 comments on commit 4caa766

Please sign in to comment.