Last active
May 26, 2018 20:17
-
-
Save squid314/cea10fc38891d62c2f75696a89dd2c80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Compiled from "MatchVsVal.scala" | |
public class MatchVsVal { | |
public scala.Tuple2<java.lang.Object, java.lang.Object> source(int); | |
Code: | |
0: getstatic #17 // Field scala/Predef$.MODULE$:Lscala/Predef$; | |
3: invokevirtual #21 // Method scala/Predef$.$qmark$qmark$qmark:()Lscala/runtime/Nothing$; | |
6: athrow | |
public double matchVersion(int); | |
Code: | |
0: aload_0 | |
1: iload_1 | |
2: invokevirtual #28 // Method source:(I)Lscala/Tuple2; | |
5: astore 4 | |
7: aload 4 | |
9: ifnull 44 | |
44: goto 47 | |
47: new #40 // class scala/MatchError | |
50: dup | |
51: aload 4 | |
53: invokespecial #44 // Method scala/MatchError."<init>":(Ljava/lang/Object;)V | |
56: athrow | |
12: aload 4 | |
14: invokevirtual #34 // Method scala/Tuple2._1$mcD$sp:()D | |
17: dstore 5 | |
19: aload 4 | |
21: invokevirtual #38 // Method scala/Tuple2._2$mcJ$sp:()J | |
24: lstore 7 | |
26: dload 5 | |
28: dstore 9 | |
30: lload 7 | |
32: lstore 11 | |
34: dload 9 | |
36: lload 11 | |
38: l2d | |
39: dmul | |
40: dstore_2 | |
41: goto 57 | |
// MatchError used to be here | |
57: dload_2 | |
58: dreturn | |
public double valVersion(int); | |
Code: | |
0: aload_0 | |
1: iload_1 | |
2: invokevirtual #28 // Method source:(I)Lscala/Tuple2; | |
5: astore 4 | |
7: aload 4 | |
9: ifnull 49 | |
49: goto 52 | |
52: new #40 // class scala/MatchError | |
55: dup | |
56: aload 4 | |
58: invokespecial #44 // Method scala/MatchError."<init>":(Ljava/lang/Object;)V | |
61: athrow | |
12: aload 4 | |
14: invokevirtual #34 // Method scala/Tuple2._1$mcD$sp:()D | |
17: dstore 5 | |
19: aload 4 | |
21: invokevirtual #38 // Method scala/Tuple2._2$mcJ$sp:()J | |
24: lstore 7 | |
26: dload 5 | |
28: dstore 9 | |
30: lload 7 | |
32: lstore 11 | |
34: new #51 // class scala/Tuple2$mcDJ$sp | |
37: dup | |
38: dload 9 | |
40: lload 11 | |
42: invokespecial #54 // Method scala/Tuple2$mcDJ$sp."<init>":(DJ)V | |
45: astore_2 | |
46: goto 62 | |
// MatchError used to be here | |
62: aload_2 | |
63: astore_3 | |
64: aload_3 | |
65: invokevirtual #34 // Method scala/Tuple2._1$mcD$sp:()D | |
68: dstore 13 | |
70: aload_3 | |
71: invokevirtual #38 // Method scala/Tuple2._2$mcJ$sp:()J | |
74: lstore 15 | |
76: dload 13 | |
78: lload 15 | |
80: l2d | |
81: dmul | |
82: dreturn | |
public MatchVsVal(); | |
Code: | |
0: aload_0 | |
1: invokespecial #57 // Method java/lang/Object."<init>":()V | |
4: return | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MatchVsVal { | |
def source(a: Int): (Double, Long) = ??? | |
def matchVersion(a: Int): Double = | |
source(a) match { | |
case (d: Double, l: Long) => d * l | |
} | |
def valVersion(a: Int): Double = { | |
val (d: Double, l: Long) = source(a) | |
d * l | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Comparison of Scala bytecode generated from effectively equivalent implementations of deconstructing a function return value. The bytecode has been slightly reordered to make it easier to diff the methods side-by-side. This shows how usage of the
val
creates a hidden object (a Tuple here, bytecodes 34-74 invalVersion
) and probably imparts poorer performance because. Why would this be different?