Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple variable initialization through tuples should not create extra objects + pattern match #1880

Open
scabug opened this issue Apr 13, 2009 · 3 comments

Comments

@scabug
Copy link

scabug commented Apr 13, 2009

The usage of pairs in variable initialization pessimizes the code abysmally:

For the simple program below:

object O
{
  def bugRep(x : Double, y : Double) : Double =
  {
      val (a, b, c) = (x + y, x - y, x / y)

      a + b + c
  }
}

Scala 2.7.3.final produces the bytecode below:

public double bugRep(double, double);
  Code:
   0:   dload_1
   1:   dload_3
   2:   dadd
   3:   dstore  6
   5:   dload_1
   6:   dload_3
   7:   dsub
   8:   dstore  8
   10:  dload_1
   11:  dload_3
   12:  ddiv
   13:  dstore  10
   15:  new     SI-23; //class scala/Tuple3
   18:  dup
   19:  dload   6
   21:  invokestatic    SI-29; //Method scala/runtime/BoxesRunTime.boxToDouble:(D)
Ljava/lang/Double;
   24:  dload   8
   26:  invokestatic    SI-29; //Method scala/runtime/BoxesRunTime.boxToDouble:(D)
Ljava/lang/Double;
   29:  dload   10
   31:  invokestatic    SI-29; //Method scala/runtime/BoxesRunTime.boxToDouble:(D)
Ljava/lang/Double;
   34:  invokespecial   SI-32; //Method scala/Tuple3."<init>":(Ljava/lang/Object;L
java/lang/Object;Ljava/lang/Object;)V
   37:  astore  5
   39:  aload   5
   41:  invokevirtual   SI-36; //Method scala/Tuple3._1:()Ljava/lang/Object;
   44:  invokestatic    SI-40; //Method scala/runtime/BoxesRunTime.unboxToDouble:(
Ljava/lang/Object;)D
   47:  dstore  12
   49:  aload   5
   51:  invokevirtual   SI-43; //Method scala/Tuple3._2:()Ljava/lang/Object;
   54:  invokestatic    SI-40; //Method scala/runtime/BoxesRunTime.unboxToDouble:(
Ljava/lang/Object;)D
   57:  dstore  14
   59:  aload   5
   61:  invokevirtual   SI-46; //Method scala/Tuple3._3:()Ljava/lang/Object;
   64:  invokestatic    SI-40; //Method scala/runtime/BoxesRunTime.unboxToDouble:(
Ljava/lang/Object;)D
   67:  dstore  16
   69:  dload   12
   71:  dload   14
   73:  dadd
   74:  dload   16
   76:  dadd
   77:  dreturn

First the tree Double's are boxed and pushed onto the stack, and then a tuple of size 3 is created and picks up the boxed doubles from the stack. After that the three Doubles are extracted from the tuple, and then unboxed so that they can be added together. I doubt hotspot is sophisticated enough to clean this whole mess up. (Does hotspot know that boxing and unboxing cancel each other out.)

One would expect to get the same code as with the following fragment:

def bugRep2(x : Double, y : Double) : Double =
{
    val a = x + y
    val b = x - y
    val c = x / y

    a + b + c
}

For which scala produces perfectly reasonable code:

public double bugRep2(double, double);
  Code:
   0:   dload_1
   1:   dload_3
   2:   dadd
   3:   dstore  5
   5:   dload_1
   6:   dload_3
   7:   dsub
   8:   dstore  7
   10:  dload_1
   11:  dload_3
   12:  ddiv
   13:  dstore  9
   15:  dload   5
   17:  dload   7
   19:  dadd
   20:  dload   9
   22:  dadd
   23:  dreturn

High level languages carry with them an abstraction cost but this is a case when we should not have to pay anything -- recognizing this idiom and producing the same code as in the second example should be an easy thing for the compiler.

@scabug
Copy link
Author

scabug commented Apr 13, 2009

Imported From: https://issues.scala-lang.org/browse/SI-1880?orig=1
Reporter: johnconnor
See #40, #29, #43, #23, #46, #32, #36

@scabug
Copy link
Author

scabug commented Jan 19, 2010

@dragos said:
Related (maybe a duplicate of) #1913

@scabug
Copy link
Author

scabug commented Apr 24, 2013

@dragos said:
I believe you were working on something similar recently.

@scabug scabug added the patmat label Apr 6, 2017
@scabug scabug added this to the Backlog milestone Apr 6, 2017
@adriaanm adriaanm removed their assignment Sep 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants