@@ -17,9 +17,8 @@ import dotty.tools.dotc.ast.tpd
1717
1818import scala .annotation .tailrec
1919
20- /** TODO
21- */
22- class GenericTuples extends MiniPhase with IdentityDenotTransformer {
20+ /** Optimize generic operations on tuples */
21+ class TuplesOptimizations extends MiniPhase with IdentityDenotTransformer {
2322 import tpd ._
2423
2524 def phaseName : String = " genericTuples"
@@ -39,6 +38,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
3938 val tail :: head :: Nil = tree.args
4039 tupleTypes(tree.tpe) match {
4140 case Some (tpes) =>
41+ // Generate a the tuple directly with TupleN+1.apply
4242 val size = tpes.size
4343 if (size <= 5 ) {
4444 // val t = tail
@@ -49,13 +49,14 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
4949 }
5050 } else {
5151 // val it = Iterator.single(head) ++ tail.asInstanceOf[Product].productIterator
52- // TupleN(it.next(), ..., it.next())
52+ // TupleN+1 (it.next(), ..., it.next())
5353 val fullIterator = ref(defn.DynamicTuple_consIterator ).appliedToArgs(head :: tail :: Nil )
5454 evalOnce(fullIterator) { it =>
5555 knownTupleFromIterator(tpes.length, it).asInstance(tree.tpe)
5656 }
5757 }
5858 case _ =>
59+ // No optimization, keep:
5960 // DynamicTuple.dynamicCons:(tail, head)
6061 tree
6162 }
@@ -65,6 +66,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
6566 val Apply (TypeApply (_, tpt :: Nil ), tup :: Nil ) = tree
6667 tupleTypes(tpt.tpe) match { // TODO tupleBoundedTypes
6768 case Some (tpes) =>
69+ // Generate a the tuple directly with TupleN-1.apply
6870 val size = tpes.size
6971 assert(size > 0 )
7072 if (size == 1 ) {
@@ -81,7 +83,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
8183 } else {
8284 // val it = this.asInstanceOf[Product].productIterator
8385 // it.next()
84- // TupleN(it.next(), ..., it.next())
86+ // TupleN-1 (it.next(), ..., it.next())
8587 evalOnce(tup.asInstance(defn.ProductType ).select(nme.productIterator)) { it =>
8688 Block (
8789 it.select(nme.next).ensureApplied :: Nil ,
@@ -90,6 +92,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
9092 }
9193 }
9294 case None =>
95+ // No optimization, keep:
9396 // DynamicTuple.dynamicTail(tup)
9497 tree
9598 }
@@ -107,6 +110,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
107110
108111 (tupleTypes(selfTp.tpe), tupleTypes(that.tpe.widenTermRefExpr)) match {
109112 case (Some (tpes1), Some (tpes2)) =>
113+ // Generate a the tuple directly with TupleN+M.apply
110114 val n = tpes1.size
111115 val m = tpes2.size
112116 if (n == 0 ) that
@@ -127,13 +131,14 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
127131 }
128132 } else {
129133 // val it = self.asInstanceOf[Product].productIterator ++ that.asInstanceOf[Product].productIterator
130- // TupleN(it.next(), ..., it.next())
134+ // TupleN+M (it.next(), ..., it.next())
131135 val fullIterator = ref(defn.DynamicTuple_concatIterator ).appliedToArgs(tree.args)
132136 evalOnce(fullIterator) { it =>
133137 knownTupleFromIterator(n + m, it).asInstance(tree.tpe)
134138 }
135139 }
136140 case _ =>
141+ // No optimization, keep:
137142 // DynamicTuple.dynamicCons[This, that.type](self, that)
138143 tree
139144 }
@@ -143,6 +148,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
143148 val Apply (TypeApply (_, tpt :: nTpt :: Nil ), tup :: nTree :: Nil ) = tree
144149 (tupleTypes(tpt.tpe), nTpt.tpe) match {
145150 case (Some (tpes), nTpe : ConstantType ) =>
151+ // Get the element directly with TupleM._n+1 or TupleXXL.productElement(n)
146152 val size = tpes.size
147153 val n = nTpe.value.intValue
148154 if (n < 0 || n >= size) {
@@ -159,6 +165,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
159165 ctx.error(" index out of bounds: " + nTpe.value.intValue, nTree.sourcePos)
160166 tree
161167 case _ =>
168+ // No optimization, keep:
162169 // DynamicTuple.dynamicApply(tup, n)
163170 tree
164171 }
@@ -180,6 +187,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
180187 tup.asInstance(defn.TupleXXLType ).select(nme.elems)
181188 }
182189 case None =>
190+ // No optimization, keep:
183191 // DynamicTuple.dynamicToArray(tup)
184192 tree
185193 }
@@ -208,6 +216,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
208216 val elements = (0 until size).map(_ => it.select(nme.next)).toList
209217 knownTupleFromElements(tpes, elements)
210218 } else {
219+ // No optimization, keep:
211220 // TupleXXL.fromIterator(it)
212221 ref(defn.TupleXXL_fromIterator ).appliedTo(it)
213222 }
0 commit comments