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
Do not needlessly create tuples when pattern matching #8790
Comments
Imported From: https://issues.scala-lang.org/browse/SI-8790?orig=1
|
@retronym said: |
Andrew Bate (andyd89) said: |
@paulp said: |
Andrew Bate (andyd89) said: |
@paulp said:
These statements aren't in opposition. |
@paulp said:
I can't write three lines of scala where that doesn't happen. |
Andrew Bate (andyd89) said: |
@paulp said: |
Andrew Bate (andyd89) said: |
@retronym said: |
Andrew Bate (andyd89) said: |
@adriaanm said: |
Andrew Bate (andyd89) said: (As an aside, it is a shame that Scala doesn't some kind of "bug fix rewards system" whereby issues would be upvoted and monetary rewards offered.) |
@som-snytt said: |
@adriaanm said: (Rumors to the contrary, we can't run Typesafe on whiskey and beer.) |
@adriaanm said: |
Andrew Bate (andyd89) said (edited on Mar 7, 2015 6:28:44 PM UTC): |
@adriaanm said: |
@som-snytt said: The issue says, If I create a tuple just to deconstruct it, don't bother constructing it. This would add, if I deconstruct a tuple just to construct it, just reuse the instance, even though it's a different tuple type. In other words, avoid reboxing the int and the tuple for (x,i): scala> :pa
// Entering paste mode (ctrl-D to finish)
sealed trait Fruit
case class Apple(weight: Int = 200) extends Fruit
case object Orange extends Fruit
val fruits = Seq[Fruit](Apple(120), Orange, Orange, Apple())
// Exiting paste mode, now interpreting.
defined trait Fruit
defined class Apple
defined object Orange
fruits: Seq[Fruit] = List(Apple(120), Orange, Orange, Apple(200))
scala> fruits.zipWithIndex map { case (x: Apple, i: Int) => (x,i) case _ => (null, -1) }
res0: Seq[(Apple, Int)] = List((Apple(120),0), (null,-1), (null,-1), (Apple(200),3)) Use case from StackOverflow. |
@retronym said: |
@lrytz said: public slow(II)Ljava/lang/String;
BIPUSH 7
ILOAD 1
IF_ICMPNE L1
BIPUSH 8
ILOAD 2
IF_ICMPNE L1
LDC "You got it!"
ASTORE 3
GOTO L2
LDC "No such luck."
ASTORE 3
L2
ALOAD 3
ARETURN |
@lrytz said: |
@retronym said: |
In the current version of Scala, pattern matching on tuples results in extra objects being created at runtime.
Consider the following MWE:
where the method
faster
in the above is some attempt to hand-optimise the implementation of theslow
method. (I know that I will now have a duplicated case in the generated code, but you get the idea: the pair(x,y)
has been eliminated.)Compiling with
scalac -print
we see that the pattern matcher generates a Tuple2 when generating the bytecode forslow
:Reading the generated code, we can see that the Tuple generation is unnecessary.
I pattern match on pairs quite often, and the extra strain of short-lived objects such that these Tuples is quite burdensome on the garbage collector, particularly in multi-threaded applications.
The text was updated successfully, but these errors were encountered: