Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do abstract classes in Scala really perform better than traits?

An excerpt from the stairway book:

If efficiency is very important, lean towards using a class. Most Java runtimes make a virtual method invocation of a class member a faster operation than an interface method invocation. Traits get compiled to interfaces and therefore may pay a slight performance overhead. However, you should make this choice only if you know that the trait in question constitutes a performance bottleneck and have evidence that using a class instead actually solves the problem.

I wrote some simple code to see what really happens behind the scenes. And I did notice invokevirtual being used in case of an abstract class and invokeinterface in case of an interface. But no matter what kind of code I wrote they always roughly performed the same. I use HotSpot 1.6.0_18 in server mode.

Is it JIT doing such a great job optimizing? Does anybody have a sample code which proves the claim from the book about invokevirutal being the faster operation?

like image 290
Nermin Serifovic Avatar asked Feb 10 '11 20:02

Nermin Serifovic


People also ask

What is the difference between abstract class and trait in Scala?

The first difference was already mentioned: classes are limited to inherit from a single abstract class but can inherit from multiple traits. Another important difference is that abstract classes allow specifying constructor parameters. Traits do not allow us to do the same.

Can a trait extend an abstract class Scala?

Scala traits don't allow constructor parameters However, be aware that a class can extend only one abstract class.

Is trait same as abstract class?

Trait supports multiple inheritance. Abstract Class supports single inheritance only. Trait can be added to an object instance. Abstract class cannot be added to an object instance.

Are abstract classes better than interfaces?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.


2 Answers

If HotSpot notices that all instances at the call site are of the same type, it is able to use a monomorphic method call and both virtual and interface methods are optimized the same way. The documents PerformanceTechniques and VirtualCalls make no distinction between virtual and interface methods.

But in the general non-monomorphic case there might be some difference. The InterfaceCalls document says:

There is no simple prefixing scheme in which an interface's methods are displayed at fixed offsets within every class that implements that interface. Instead, in the general (non-monomorphic) case, an assembly-coded stub routine must fetch a list of implemented interfaces from the receiver's klassOop, and walk that list seeking the current target interface.

It also confirms that the monomorphic case is the same for both:

Nearly the same optimizations apply to interface calls as to virtual calls. As with virtual calls, most interface calls are monomorphic, and can therefore be rendered as direct calls with a cheap check.

Other JVMs might have different optimizations.

You could try a micro benchmark (if you know how) which calls methods on multiple classes which implement the same interface, and on multiple classes which extend the same abstract class. That way it should be possible to force the JVM to use non-monomorphic method calls. (Though in the real life any difference there might not matter, since most calls sites are anyways monomorphic.)

like image 60
Esko Luontola Avatar answered Oct 20 '22 01:10

Esko Luontola


The bottom line is that you're going to have to measure it yourself for your own application to see if it's important. You can get rather counterintuitive results with the current JVM. Try this out.

File TraitAbstractPackage.scala

package traitvsabstract

trait T1 { def x: Int; def inc: Unit }
trait T2 extends T1 { def x_=(x0: Int): Unit }
trait T3 extends T2 { def inc { x = x + 1 } }

abstract class C1 { def x: Int; def inc: Unit }
abstract class C2 extends C1 { def x_=(x0: Int): Unit }
abstract class C3 extends C2 { def inc { x = x + 1 } }

File TraitVsAbstract.scala

object TraitVsAbstract {
  import traitvsabstract._

  class Ta extends T3 { var x: Int = 0}
  class Tb extends T3 {
    private[this] var y: Long = 0
    def x = y.toInt
    def x_=(x0: Int) { y = x0 } 
  }
  class Tc extends T3 {
    private[this] var xHidden: Int = 0
    def x = xHidden
    def x_=(x0: Int) { if (x0 > xHidden) xHidden = x0 }
  }

  class Ca extends C3 { var x: Int = 0 }
  class Cb extends C3 {
    private[this] var y: Long = 0
    def x = y.toInt
    def x_=(x0: Int) { y = x0 } 
  }
  class Cc extends C3 {
    private[this] var xHidden: Int = 0
    def x = xHidden
    def x_=(x0: Int) { if (x0 > xHidden) xHidden = x0 }
  }

  def Tbillion3(t: T3) = {
    var i=0; while (i<1000000000) { t.inc; i+=1 }; t.x
  }

  def Tbillion1(t: T1) = {
    var i=0; while (i<1000000000) { t.inc; i+=1 }; t.x
  }

  def Cbillion3(c: C3) = {
    var i=0; while (i<1000000000) { c.inc; i+=1 }; c.x
  }

  def Cbillion1(c: C1) = {
    var i=0; while (i<1000000000) { c.inc; i+=1 }; c.x
  }

  def ptime(f: => Int) {
    val t0 = System.nanoTime
    val ans = f.toString
    val t1 = System.nanoTime
    printf("Answer: %s; elapsed: %.2f seconds\n",ans,(t1-t0)*1e-9)
  }

  def main(args: Array[String]) {
    for (i <- 1 to 3) {
      println("Iteration "+i)
      val t1s,t3s = List(new Ta, new Tb, new Tc)
      val c1s,c3s = List(new Ca, new Cb, new Cc)
      t1s.foreach(x => ptime(Tbillion1(x)))
      t3s.foreach(x => ptime(Tbillion3(x)))
      c1s.foreach(x => ptime(Cbillion1(x)))
      c3s.foreach(x => ptime(Cbillion3(x)))
      println
    }
  }
}

Every one should print out 1000000000 as the answer, and the time taken should be zero (if the JVM is really clever) or about as long as it takes to add a billion numbers. But at least on my system, the Sun JVM optimizes backwards--repeated runs get slower--and abstract classes are slower than traits. (You might want to run with java -XX:+PrintCompilation to try to figure out what goes wrong; I suspect zombies.)

Also, it's worth noting that scalac -optimise does nothing to improve matters--it's all up to the JVM.

The JRockit JVM in contrast turns in a consistent middling performance, but again, traits beat classes. Since the timings are consistent I'll report them: 3.35s for the classes (3.62s for the one with an if statement) vs. 2.51 seconds for all the traits, if-statement or no.

(I find this trend to be generally true: Hotspot produces blazing fast performance in some cases, and in others (like this case) gets confused and is woefully slow; JRockit is never super-fast--don't bother trying to get C-like performance even out of primitives--but it rarely blunders.)

like image 45
Rex Kerr Avatar answered Oct 20 '22 01:10

Rex Kerr