You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Close#23224:
This PR optimizes simple tuple extraction by avoiding unnecessary tuple
allocations and refines the typing of bind patterns for named tuples.
* Optimise `makePatDef` to reduce tuple creation when a pattern uses
only simple variables or wildcards.
* If the selector of a match has bottom type, use the type from pattern
for the bind variable.
For example:
```scala
def f1: (Int, Int, Int) = (1, 2, 3)
def test1 =
val (a, b, c) = f1
a + b + c
```
Before this PR:
```scala
val $1$: (Int, Int, Int) =
this.f1:(Int, Int, Int) @unchecked match
{
case Tuple3.unapply[Int, Int, Int](a @ _, b @ _, c @ _) =>
Tuple3.apply[Int, Int, Int](a, b, c)
}
val a: Int = $1$._1
val b: Int = $1$._2
val c: Int = $1$._3
a + b + c
```
After this PR:
```scala
val $2$: (Int, Int, Int) =
this.f1:(Int, Int, Int) @unchecked match
{
case $1$ @ Tuple3.unapply[Int, Int, Int](_, _, _) =>
$1$:(Int, Int, Int)
}
val a: Int = $2$._1
val b: Int = $2$._2
val c: Int = $2$._3
a + b + c
```
Also in genBCode now:
```scala
val $2$: Tuple3 =
matchResult1[Tuple3]:
{
case val x1: Tuple3 = this.f1():Tuple3
if x1 ne null then
{
case val $1$: Tuple3 = x1
return[matchResult1] $1$:Tuple3
}
else ()
throw new MatchError(x1)
}
val a: Int = Int.unbox($2$._1())
val b: Int = Int.unbox($2$._2())
val c: Int = Int.unbox($2$._3())
a + b + c
```
I use the regular expression
(`val\s*\(\s*[a-zA-Z_]\w*(\s*,\s*[a-zA-Z_]\w*)*\s*\)\s*=`) to search in
the compiler, and found 400+ places which are simple tuple extraction
like this.
0 commit comments