- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Another clarification on boxing
Thu, 2011-10-13, 08:35
Dear all,
what does it happen with this code, where item.size returns a scala.Int ?
lazy val sizes:Array[Int] = subcollections.map(_.size).toArray;
Is sizes allocated as an Array[java.lang.Integer] or an Array[int] ? Does boxing occur only at creation, or at every access to any element of the array?
Best Regards
what does it happen with this code, where item.size returns a scala.Int ?
lazy val sizes:Array[Int] = subcollections.map(_.size).toArray;
Is sizes allocated as an Array[java.lang.Integer] or an Array[int] ? Does boxing occur only at creation, or at every access to any element of the array?
Best Regards
Thu, 2011-10-13, 09:07
#2
Re: Another clarification on boxing
On Thu, Oct 13, 2011 at 9:35 AM, Edmondo Porcu <edmondo.porcu@gmail.com> wrote:
Dear all,
what does it happen with this code, where item.size returns a scala.Int ?
lazy val sizes:Array[Int] = subcollections.map(_.size).toArray;
Is sizes allocated as an Array[java.lang.Integer] or an Array[int] ? Does boxing occur only at creation, or at every access to any element of the array?
Check the bytecode with javap?
Best Regards
--
Viktor Klang
Akka Tech LeadTypesafe - Enterprise-Grade Scala from the Experts
Twitter: @viktorklang
Thu, 2011-10-13, 10:27
#3
Re: Another clarification on boxing
2011/10/13 Edmondo Porcu <edmondo.porcu@gmail.com>
Dear all,
what does it happen with this code, where item.size returns a scala.Int ?
lazy val sizes:Array[Int] = subcollections.map(_.size).toArray;
Is sizes allocated as an Array[java.lang.Integer] or an Array[int] ? Does boxing occur only at creation, or at every access to any element of the array?
Best Regards
If subcollections.map(_.size) returns something else than an array then the ints will be boxed.
Does something like this makes a difference?
scala> import scala.collection.breakOut
import scala.collection.breakOut
scala> val x:Array[Int] = List("1", "2", "3").map(_.toInt)(breakOut)
x: Array[Int] = Array(1, 2, 3)
--
Sébastien
On Oct 13, 2011, at 09:35 , Edmondo Porcu wrote:
It would probably have an overall better effect if you would check this for yourself: one avenue would be reasoning over the type Array[Int] and what that means, another would be to verify with javap (since you are asking these performance related question, you will have to be familiar with this tool and its output, I assume).
Regards,
Roland Kuhn
SPOILER ALERT!Typesafe – Enterprise-Grade Scala from the Experts
twitter: @rolandkuhn
scala> object A { val s = (1 to 5).toArray }defined module A
scala> :javap -c -p ACompiled from "<console>"public final class A$ extends java.lang.Object implements scala.ScalaObject{public static final A$ MODULE$;
private final int[] s;
public static {}; Code: 0: new #9; //class A$ 3: invokespecial #12; //Method "<init>":()V 6: return
public int[] s(); Code: 0: aload_0 1: getfield #18; //Field s:[I 4: areturn
public A$(); Code: 0: aload_0 1: invokespecial #21; //Method java/lang/Object."<init>":()V 4: aload_0 5: putstatic #23; //Field MODULE$:LA$; 8: aload_0 9: getstatic #28; //Field scala/Predef$.MODULE$:Lscala/Predef$; 12: iconst_1 13: invokevirtual #32; //Method scala/Predef$.intWrapper:(I)Lscala/runtime/RichInt; 16: iconst_5 17: invokevirtual #38; //Method scala/runtime/RichInt.to:(I)Lscala/collection/immutable/Range$Inclusive; 20: getstatic #43; //Field scala/reflect/Manifest$.MODULE$:Lscala/reflect/Manifest$; 23: invokevirtual #47; //Method scala/reflect/Manifest$.Int:()Lscala/reflect/AnyValManifest; 26: invokeinterface #53, 2; //InterfaceMethod scala/collection/TraversableOnce.toArray:(Lscala/reflect/ClassManifest;)Ljava/lang/Object; 31: checkcast #54; //class "[I" 34: putfield #18; //Field s:[I 37: return
}