One interesting feature that was added to Scala in version 2.8 is specialization, using the @specialized
annotation. First, a little background information.
Generics in Java and the JVM, and consequently also in Scala, are implemented by using type erasure. That means that if you have an instance of a generic class, for example List[String]
, then the compiler will throw away the information about the type argument, so that at runtime it will look like List[Object]
.
In Java, primitive types are treated very differently from reference types. One of the things that you cannot do in Java is use primitive types to fill in type parameters – for example, you cannot make a List<int>
. If you want to create a list that holds integers, you’ll have to use the wrapper class Integer
. A drawback of this approach is that you need to box each of your primitive int
s into an Integer
object that takes up a lot more memory. I’ve done some tests and found that on my JVM a double
only takes up 8 bytes, but a Double
object takes up 24 bytes. Also, the boxing and unboxing takes up processor time, which can become a serious bottleneck if you’re dealing with large collections of numbers.
In Scala, the distinction between primitive types and reference types is far less great than in Java, and you can use types like Int
directly for type arguments. But a List[Int]
is still converted to a List[Object]
at runtime because of type erasure and since primitive types are not subclasses of Object
on the JVM, Scala will still box the Int
s to objects of some wrapper class – which makes a List[Int]
in Scala just as inefficient as a List<Integer>
in Java.
The @specialized
annotation and compiler support are meant to get rid of this inefficiency. Iulian Dragos explains it very clearly in this video. If you create a generic class and you use the @specialized
annotation, for example like this:
class Container[@specialized(Int) T](value: T) { def apply(): T = value }
the compiler will actually generate two versions of the class: the normal, generic one, in which the type parameter is erased, and a special subclass that uses the primitive type Int
, without the need to box or unbox the value. You can see this when you compile the code using the -print
option of the compiler:
jesper@jesper-desktop:~/Projects/boxing$ scalac -print Container.scala [[syntax trees at end of cleanup]]// Scala source: Container.scala package <empty> { class Container extends java.lang.Object with ScalaObject { <paramaccessor> protected[this] val value: java.lang.Object = _; def apply(): java.lang.Object = Container.this.value; <specialized> def apply$mcI$sp(): Int = scala.Int.unbox(Container.this.apply()); // #1 def this(value: java.lang.Object): Container = { Container.this.value = value; Container.super.this(); () } }; <specialized> class Container$mcI$sp extends Container { <paramaccessor> <specialized> protected[this] val value$mcI$sp: Int = _; override <specialized> def apply(): Int = Container$mcI$sp.this.apply$mcI$sp(); override <specialized> def apply$mcI$sp(): Int = Container$mcI$sp.this.value$mcI$sp; // #2 override <bridge> <specialized> def apply(): java.lang.Object = scala.Int.box(Container$mcI$sp.this.apply()); <specialized> def this(value$mcI$sp: Int): Container$mcI$sp = { Container$mcI$sp.this.value$mcI$sp = value$mcI$sp; Container$mcI$sp.super.this(scala.Int.box(value$mcI$sp)); () } } }
In this output you see that there is a class Container
, which is the regular generic version with erased type parameters, and a class named Container$mcI$sp
which is a subclass of the regular Container
. That class is the version that’s specialized for Int
. Notice that both the classes have a method apply$mcI$sp()
, which is called by their apply()
methods. In the regular Container
, this method (line 7 – #1) unboxes the Object
that’s in the container. But in the specialized subclass, it directly returns the Int
without unboxing (line 17 – #2).
However, life is not always so simple. Here is an example in which specialization is not as effective as you might think at first sight.
object Boxing01 { def clamp[@specialized(Double) T : Ordering](value: T, low: T, high: T): T = { import Ordered._ if (value < low) low else if (value > high) high else value } def main(args: Array[String]) { val a = clamp(25.0, 13.0, 40.0) println(a) } }
I wanted the clamp()
method to be generic, so that I could use it on anything for which there is an Ordering
available, but I wanted to avoid it having to box and unbox when using it with Double
. At first sight you’ll probably think that using @specialized
will do the job here. But let’s look at the output of compiling this code with the -print
option. Here it is (with some parts omitted):
// The normal, type-erased version def clamp(value: java.lang.Object, low: java.lang.Object, high: java.lang.Object, evidence$1: scala.math.Ordering): java.lang.Object = if (scala.package.Ordered().orderingToOrdered(value, evidence$1).<(low)) low else if (scala.package.Ordered().orderingToOrdered(value, evidence$1).>(high)) high else value; // The specialized version <specialized> def clamp$mDc$sp(value: Double, low: Double, high: Double, evidence$1: scala.math.Ordering): Double = if (scala.package.Ordered().orderingToOrdered(scala.Double.box(value), evidence$1).<(scala.Double.box(low))) low else if (scala.package.Ordered().orderingToOrdered(scala.Double.box(value), evidence$1).>(scala.Double.box(high))) high else value;
Again, the compiler generated two versions of the clamp()
method: the normal one that works on java.lang.Object
s, and a version named clamp$mDc$sp()
that’s specialized for Double
. Take a closer look at the specialized version. You’ll see that it still contains calls to scala.Double.box()
and scala.Double.unbox()
. Using the @specialized
annotation didn’t help at all here!
The reason for this is that the method orderingToOrdered()
(defined in object scala.math.Ordered
) and the trait scala.math.Ordered
are not specialized. So, to call orderingToOrdered()
and the <
and >
methods in trait Ordered
, the Double
must be boxed, and the return value must be unboxed. Instead of getting rid of the boxing and unboxing, all that we have achieved is that it happens somewhere else.
In Scala’s standard library (version 2.8.1), @specialized
is used only very sparingly. Only a handful of traits and classes are specialized. It would be especially nice if the collection classes were specialized for the primitive types. A drawback of specialization is that it causes lots of extra classes and methods to be generated – if all collection classes would be specialized for all primitive types, the Scala language library would suddenly become about ten times as large. Hopefully, some more specialization will be added to the library in places where it really matters in future versions of Scala.