I've searched for the use of @specialized
in the source code of the standard library of Scala 2.8.1. It looks like only a handful of traits and classes use this annotation: Function0
, Function1
, Function2
, Tuple1
, Tuple2
, Product1
, Product2
, AbstractFunction0
, AbstractFunction1
, AbstractFunction2
.
None of the collection classes are @specialized
. Why not? Would this generate too many classes?
This means that using collection classes with primitive types is very inefficient, because there will be a lot of unnecessary boxing and unboxing going on.
What's the most efficient way to have an immutable list or sequence (with IndexedSeq
characteristics) of Int
s, avoiding boxing and unboxing?
List
andIndexedSeq
are specialized. Doing some:javap -c
with the new scala 2.9 REPL shows they still do boxing all the time. The constructors are optimized though it seems (List(1,2,3)
uses awrapIntArray
). – Johannejohannes