tuples and specialization - surprise

65 views
Skip to first unread message

HamsterofDeath

unread,
Jan 18, 2012, 3:29:00 PM1/18/12
to scala...@googlegroups.com

why are there 4 members instead of 2?

HamsterofDeath

unread,
Jan 18, 2012, 3:40:00 PM1/18/12
to √iktor Ҡlang, scala...@googlegroups.com
i kinda expected tuple2specialized to be:

class Tuple2Double(val a:double, val b:double)

not

class Tuple2Double(val a:double, val b:double, val unused1:Object, val unused2:Object)

Am 18.01.2012 21:33, schrieb √iktor Ҡlang:


On Wed, Jan 18, 2012 at 9:29 PM, HamsterofDeath <h-s...@gmx.de> wrote:

why are there 4 members instead of 2?

How else do you store a primitive without boxing it?

--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang


√iktor Ҡlang

unread,
Jan 18, 2012, 3:33:11 PM1/18/12
to HamsterofDeath, scala...@googlegroups.com
On Wed, Jan 18, 2012 at 9:29 PM, HamsterofDeath <h-s...@gmx.de> wrote:

why are there 4 members instead of 2?
jgdddfbd.png

Rex Kerr

unread,
Jan 18, 2012, 6:04:19 PM1/18/12
to HamsterofDeath, √iktor Ҡlang, scala...@googlegroups.com
Tuple2 is a class from which Tuple2$mcDD$sp inherits.

Therefore, unfortunately, it inherits the now-unused fields _1 and _2 (and the extra 8 bytes of storage that those require).

A scheme where there was a common base class that has no fields (just accessors) and the object-containing fields were in the (default) subclass would avoid this problem (albeit at the cost of confusing all the class names even when the specialized version wasn't used).

  --Rex

2012/1/18 HamsterofDeath <h-s...@gmx.de>

Matthew Pocock

unread,
Jan 18, 2012, 6:53:38 PM1/18/12
to HamsterofDeath, scala...@googlegroups.com
Eugh!


On 18 January 2012 20:29, HamsterofDeath <h-s...@gmx.de> wrote:

why are there 4 members instead of 2?



--
Dr Matthew Pocock
Integrative Bioinformatics Group, School of Computing Science, Newcastle University
skype: matthew.pocock
tel: (0191) 2566550

jgdddfbd.png

Matthew Pocock

unread,
Jan 21, 2012, 11:18:41 AM1/21/12
to Rex Kerr, HamsterofDeath, √iktor Ҡlang, scala...@googlegroups.com
On 18 January 2012 23:04, Rex Kerr <ich...@gmail.com> wrote:
Tuple2 is a class from which Tuple2$mcDD$sp inherits.

Therefore, unfortunately, it inherits the now-unused fields _1 and _2 (and the extra 8 bytes of storage that those require).

A scheme where there was a common base class that has no fields (just accessors) and the object-containing fields were in the (default) subclass would avoid this problem (albeit at the cost of confusing all the class names even when the specialized version wasn't used).

Right, so if a class is specialized, perhaps this should cause specific sub-classes to be generated for the Any cases in addition to the primitive ones? Who knows, it may even clean up some of the implementation ;)

Matthew
 


  --Rex


2012/1/18 HamsterofDeath <h-s...@gmx.de>
i kinda expected tuple2specialized to be:

class Tuple2Double(val a:double, val b:double)

not

class Tuple2Double(val a:double, val b:double, val unused1:Object, val unused2:Object)

Am 18.01.2012 21:33, schrieb √iktor Ҡlang:


On Wed, Jan 18, 2012 at 9:29 PM, HamsterofDeath <h-s...@gmx.de> wrote:

why are there 4 members instead of 2?

How else do you store a primitive without boxing it?

--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang



Ben Hutchison

unread,
Jun 9, 2013, 10:41:14 PM6/9/13
to scala...@googlegroups.com
Somewhere this got fixed, it seems. Thank you to whoever fixed it! :)

In 2.10.0 I'm getting:

scala> (1d, 1d).getClass
res2: Class[_ <: (Double, Double)] = class scala.Tuple2$mcDD$sp

scala> :javap  scala.Tuple2$mcDD$sp
Compiled from "Tuple2.scala"
public class scala.Tuple2$mcDD$sp extends scala.Tuple2 implements scala.Product2$mcDD$sp{
    public final double _1$mcD$sp;
    public final double _2$mcD$sp;
    public double _1$mcD$sp();
    public double _1();
    public double _2$mcD$sp();
    public double _2();
    public scala.Tuple2 swap();
    public scala.Tuple2 swap$mcDD$sp();
    public double copy$default$1();
    public double copy$default$1$mcD$sp();
    public double copy$default$2();
    public double copy$default$2$mcD$sp();
    public boolean specInstance$();
    public java.lang.Object copy$default$2();
    public java.lang.Object copy$default$1();
    public java.lang.Object _2();
    public java.lang.Object _1();
    public scala.Tuple2$mcDD$sp(double, double);
}

Does anyone know:
1. Are there any cases where primitive tuples end up as object-tuples
2. Was there an issue to track this

-Ben

Ben Hutchison

unread,
Jun 9, 2013, 11:38:08 PM6/9/13
to scala-user
Doh! I misinterpreted javap; It's not fixed. 

Specialized tuples still inherit a pair of redundant object fields from their superclass, limiting their usefulness in memory constrained situations.

scala> :javap  scala.Tuple2
Compiled from "Tuple2.scala"
public class scala.Tuple2 extends java.lang.Object implements scala.Product2,scala.Serializable{
    public final java.lang.Object _1;
    public final java.lang.Object _2; ...

public class scala.Tuple2$mcDD$sp extends scala.Tuple2 implements scala.Product2$mcDD$sp{
    public final double _1$mcD$sp;
    public final double _2$mcD$sp;...

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages