Specialized version of a method calls unspecialised one in Scala 2.12.1


#1

I don’t know is this a known issue or a fundamental limitation. If anybody have any hints they are highly appreciated.

class Test1[@specialized(Double) N] {
def getIt(a: Array[N]):N = a(0)
}

leads to the following byte code:

public getIt$mcD$sp([D)D
// parameter final a
L0
LINENUMBER 14 L0
ALOAD 0
ALOAD 1
INVOKEVIRTUAL Test1.getIt (Ljava/lang/Object;)Ljava/lang/Object;
INVOKESTATIC scala/runtime/BoxesRunTime.unboxToDouble (Ljava/lang/Object;)D
DRETURN
L1
LOCALVARIABLE this LTest1; L0 L1 0
LOCALVARIABLE a [D L0 L1 1
MAXSTACK = 2
MAXLOCALS = 2


#2

I think it’s not a big deal.

@specialized does help very much for method parameters or return values, since HotSpot JVM is already very good at to inline and eliminate those boxing / unboxing at run-time.

@specialized is best to use for optimizing fields stored in a class, which is hard to be optimized by HotSpot JVM.


#3

Looks like https://github.com/scala/bug/issues/10277