@@ -17,9 +17,8 @@ import dotty.tools.dotc.ast.tpd
17
17
18
18
import scala .annotation .tailrec
19
19
20
- /** TODO
21
- */
22
- class GenericTuples extends MiniPhase with IdentityDenotTransformer {
20
+ /** Optimize generic operations on tuples */
21
+ class TuplesOptimizations extends MiniPhase with IdentityDenotTransformer {
23
22
import tpd ._
24
23
25
24
def phaseName : String = " genericTuples"
@@ -39,6 +38,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
39
38
val tail :: head :: Nil = tree.args
40
39
tupleTypes(tree.tpe) match {
41
40
case Some (tpes) =>
41
+ // Generate a the tuple directly with TupleN+1.apply
42
42
val size = tpes.size
43
43
if (size <= 5 ) {
44
44
// val t = tail
@@ -49,13 +49,14 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
49
49
}
50
50
} else {
51
51
// val it = Iterator.single(head) ++ tail.asInstanceOf[Product].productIterator
52
- // TupleN(it.next(), ..., it.next())
52
+ // TupleN+1 (it.next(), ..., it.next())
53
53
val fullIterator = ref(defn.DynamicTuple_consIterator ).appliedToArgs(head :: tail :: Nil )
54
54
evalOnce(fullIterator) { it =>
55
55
knownTupleFromIterator(tpes.length, it).asInstance(tree.tpe)
56
56
}
57
57
}
58
58
case _ =>
59
+ // No optimization, keep:
59
60
// DynamicTuple.dynamicCons:(tail, head)
60
61
tree
61
62
}
@@ -65,6 +66,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
65
66
val Apply (TypeApply (_, tpt :: Nil ), tup :: Nil ) = tree
66
67
tupleTypes(tpt.tpe) match { // TODO tupleBoundedTypes
67
68
case Some (tpes) =>
69
+ // Generate a the tuple directly with TupleN-1.apply
68
70
val size = tpes.size
69
71
assert(size > 0 )
70
72
if (size == 1 ) {
@@ -81,7 +83,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
81
83
} else {
82
84
// val it = this.asInstanceOf[Product].productIterator
83
85
// it.next()
84
- // TupleN(it.next(), ..., it.next())
86
+ // TupleN-1 (it.next(), ..., it.next())
85
87
evalOnce(tup.asInstance(defn.ProductType ).select(nme.productIterator)) { it =>
86
88
Block (
87
89
it.select(nme.next).ensureApplied :: Nil ,
@@ -90,6 +92,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
90
92
}
91
93
}
92
94
case None =>
95
+ // No optimization, keep:
93
96
// DynamicTuple.dynamicTail(tup)
94
97
tree
95
98
}
@@ -107,6 +110,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
107
110
108
111
(tupleTypes(selfTp.tpe), tupleTypes(that.tpe.widenTermRefExpr)) match {
109
112
case (Some (tpes1), Some (tpes2)) =>
113
+ // Generate a the tuple directly with TupleN+M.apply
110
114
val n = tpes1.size
111
115
val m = tpes2.size
112
116
if (n == 0 ) that
@@ -127,13 +131,14 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
127
131
}
128
132
} else {
129
133
// val it = self.asInstanceOf[Product].productIterator ++ that.asInstanceOf[Product].productIterator
130
- // TupleN(it.next(), ..., it.next())
134
+ // TupleN+M (it.next(), ..., it.next())
131
135
val fullIterator = ref(defn.DynamicTuple_concatIterator ).appliedToArgs(tree.args)
132
136
evalOnce(fullIterator) { it =>
133
137
knownTupleFromIterator(n + m, it).asInstance(tree.tpe)
134
138
}
135
139
}
136
140
case _ =>
141
+ // No optimization, keep:
137
142
// DynamicTuple.dynamicCons[This, that.type](self, that)
138
143
tree
139
144
}
@@ -143,6 +148,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
143
148
val Apply (TypeApply (_, tpt :: nTpt :: Nil ), tup :: nTree :: Nil ) = tree
144
149
(tupleTypes(tpt.tpe), nTpt.tpe) match {
145
150
case (Some (tpes), nTpe : ConstantType ) =>
151
+ // Get the element directly with TupleM._n+1 or TupleXXL.productElement(n)
146
152
val size = tpes.size
147
153
val n = nTpe.value.intValue
148
154
if (n < 0 || n >= size) {
@@ -159,6 +165,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
159
165
ctx.error(" index out of bounds: " + nTpe.value.intValue, nTree.sourcePos)
160
166
tree
161
167
case _ =>
168
+ // No optimization, keep:
162
169
// DynamicTuple.dynamicApply(tup, n)
163
170
tree
164
171
}
@@ -180,6 +187,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
180
187
tup.asInstance(defn.TupleXXLType ).select(nme.elems)
181
188
}
182
189
case None =>
190
+ // No optimization, keep:
183
191
// DynamicTuple.dynamicToArray(tup)
184
192
tree
185
193
}
@@ -208,6 +216,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
208
216
val elements = (0 until size).map(_ => it.select(nme.next)).toList
209
217
knownTupleFromElements(tpes, elements)
210
218
} else {
219
+ // No optimization, keep:
211
220
// TupleXXL.fromIterator(it)
212
221
ref(defn.TupleXXL_fromIterator ).appliedTo(it)
213
222
}
0 commit comments