在对这个问题的公认最佳回答中,有一个明确的解释为什么会发生拳击。
但是,如果我反编译代码(使用 java 反编译器),我看不到 scala.runtime.BoxesRunTime 的使用。此外,如果我分析代码(使用 JProfiler),我看不到任何 BoxesRunTime 实例。
那么,我如何真正看到装箱/拆箱的证据?
在这段代码中:
class Foo[T] {
def bar(i: T) = i
}
object Main {
def main(args: Array[String]) {
val f = new Foo[Int]
f.bar(5)
}
}
的调用bar
应该首先将整数装箱。使用 Scala 2.8.1 编译并使用:
javap -c -l -private -verbose -classpath <dir> Main$
查看为类main
方法生成的字节码Main
:
public void main(java.lang.String[]);
...
9: iconst_5
10: invokestatic #24; //Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
13: invokevirtual #28; //Method Foo.bar:(Ljava/lang/Object;)Ljava/lang/Object;
16: pop
17: return
...
您可以在调用 toBoxesRunTime
之前看到调用 to bar
。
BoxesRunTime
是一个包含原始类型装箱方法的对象,因此总共应该只有一个实例。这里的诀窍是库中的这个特定文件是用 Java 编写的,并且转换是静态方法。出于这个原因,在运行时没有它的任何实例,尽管在 Scala 代码中使用它感觉就像它是一个对象。
您可能应该使用 JProfile 寻找装箱的原语(例如 java.lang.Integer),尽管我不确定 JVM 是如何工作的,以及它是否真的可以在运行时重写代码并对其进行优化以避免装箱。据我所知,它不应该应用专业化(但我相信 CLR 可以)。一些有和没有装箱情况的微基准是另一种确定运行时发生的情况的方法。
编辑:
以上假设类型参数未使用注释进行@specialized
注释。在这种情况下,可以避免装箱/拆箱。标准库中的某些类是专门的。看到这个 sid。
给定以下 Test.scala 程序:
object Test {
def main(args:Array[String]) {
val list = List(1,5,15)
val res = list.map(e => e*2).filter(e => e>10)
}
}
如果我用 编译scalac -Xprint:jvm Test.scala
,我会得到这个片段,暗示发生了专业化(对不起,宽粘贴):
package <empty> {
final class Test extends java.lang.Object with ScalaObject {
def main(args: Array[java.lang.String]): Unit = {
val list: List = immutable.this.List.apply(scala.this.Predef.wrapIntArray(Array[Int]{1, 5, 15}));
val res: List = list.map({
(new Test$$anonfun$1(): Function1)
}, immutable.this.List.canBuildFrom()).$asInstanceOf[scala.collection.TraversableLike]().filter({
(new Test$$anonfun$2(): Function1)
}).$asInstanceOf[List]();
()
};
def this(): object Test = {
Test.super.this();
()
}
};
@SerialVersionUID(0) @serializable final <synthetic> class Test$$anonfun$1 extends scala.runtime.AbstractFunction1$mcII$sp {
final def apply(e: Int): Int = Test$$anonfun$1.this.apply$mcII$sp(e);
<specialized> def apply$mcII$sp(v1: Int): Int = v1.*(2);
final <bridge> def apply(v1: java.lang.Object): java.lang.Object = scala.Int.box(Test$$anonfun$1.this.apply(scala.Int.unbox(v1)));
def this(): Test$$anonfun$1 = {
Test$$anonfun$1.super.this();
()
}
};
@SerialVersionUID(0) @serializable final <synthetic> class Test$$anonfun$2 extends scala.runtime.AbstractFunction1$mcZI$sp {
final def apply(e: Int): Boolean = Test$$anonfun$2.this.apply$mcZI$sp(e);
<specialized> def apply$mcZI$sp(v1: Int): Boolean = v1.>(10);
final <bridge> def apply(v1: java.lang.Object): java.lang.Object = scala.Boolean.box(Test$$anonfun$2.this.apply(scala.Int.unbox(v1)));
def this(): Test$$anonfun$2 = {
Test$$anonfun$2.super.this();
()
}
}
}
可能是为什么您在字节码中看不到任何装箱的证据...