This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Another clarification on boxing

3 replies
edmondo1984
Joined: 2011-09-14,
User offline. Last seen 28 weeks 4 days ago.
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
roland.kuhn
Joined: 2011-02-21,
User offline. Last seen 35 weeks 3 days ago.
Re: Another clarification on boxing
Dear Edmondo,
On Oct 13, 2011, at 09:35 , Edmondo Porcu 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?

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
Typesafe – Enterprise-Grade Scala from the Experts
twitter: @rolandkuhn

SPOILER ALERT!
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
}
Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
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
Sebastien Bocq
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland