Commit 6f35f4a
authored
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.
File tree
8 files changed
+163
-32
lines changed- compiler
- src/dotty/tools/dotc
- ast
- typer
- test/dotty/tools/backend/jvm
- tests
- neg
- pos
8 files changed
+163
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1450 | 1450 | | |
1451 | 1451 | | |
1452 | 1452 | | |
| 1453 | + | |
| 1454 | + | |
| 1455 | + | |
| 1456 | + | |
| 1457 | + | |
| 1458 | + | |
| 1459 | + | |
| 1460 | + | |
| 1461 | + | |
| 1462 | + | |
| 1463 | + | |
| 1464 | + | |
| 1465 | + | |
| 1466 | + | |
| 1467 | + | |
| 1468 | + | |
| 1469 | + | |
| 1470 | + | |
| 1471 | + | |
| 1472 | + | |
| 1473 | + | |
1453 | 1474 | | |
1454 | 1475 | | |
1455 | 1476 | | |
| |||
1483 | 1504 | | |
1484 | 1505 | | |
1485 | 1506 | | |
1486 | | - | |
1487 | | - | |
1488 | | - | |
1489 | | - | |
1490 | | - | |
1491 | | - | |
1492 | | - | |
1493 | | - | |
1494 | | - | |
1495 | | - | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
1496 | 1532 | | |
1497 | | - | |
1498 | | - | |
1499 | | - | |
1500 | | - | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
1501 | 1539 | | |
1502 | 1540 | | |
1503 | 1541 | | |
1504 | 1542 | | |
1505 | 1543 | | |
1506 | | - | |
1507 | | - | |
| 1544 | + | |
| 1545 | + | |
1508 | 1546 | | |
1509 | | - | |
| 1547 | + | |
1510 | 1548 | | |
1511 | 1549 | | |
1512 | 1550 | | |
| |||
1517 | 1555 | | |
1518 | 1556 | | |
1519 | 1557 | | |
1520 | | - | |
| 1558 | + | |
| 1559 | + | |
1521 | 1560 | | |
1522 | | - | |
| 1561 | + | |
1523 | 1562 | | |
1524 | | - | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
1525 | 1577 | | |
| 1578 | + | |
1526 | 1579 | | |
1527 | 1580 | | |
1528 | 1581 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
350 | 350 | | |
351 | 351 | | |
352 | 352 | | |
353 | | - | |
| 353 | + | |
354 | 354 | | |
355 | | - | |
| 355 | + | |
356 | 356 | | |
357 | | - | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
358 | 361 | | |
359 | 362 | | |
360 | | - | |
361 | 363 | | |
362 | 364 | | |
363 | 365 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1721 | 1721 | | |
1722 | 1722 | | |
1723 | 1723 | | |
1724 | | - | |
| 1724 | + | |
| 1725 | + | |
1725 | 1726 | | |
1726 | 1727 | | |
1727 | 1728 | | |
| |||
1743 | 1744 | | |
1744 | 1745 | | |
1745 | 1746 | | |
1746 | | - | |
| 1747 | + | |
1747 | 1748 | | |
1748 | 1749 | | |
1749 | 1750 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2827 | 2827 | | |
2828 | 2828 | | |
2829 | 2829 | | |
| 2830 | + | |
2830 | 2831 | | |
2831 | 2832 | | |
2832 | 2833 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
136 | 145 | | |
137 | 146 | | |
138 | 147 | | |
| |||
Lines changed: 22 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1606 | 1606 | | |
1607 | 1607 | | |
1608 | 1608 | | |
| 1609 | + | |
| 1610 | + | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
| 1615 | + | |
| 1616 | + | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
| 1623 | + | |
| 1624 | + | |
| 1625 | + | |
| 1626 | + | |
| 1627 | + | |
| 1628 | + | |
| 1629 | + | |
| 1630 | + | |
1609 | 1631 | | |
1610 | 1632 | | |
1611 | 1633 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
0 commit comments