|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import core._ |
| 5 | +import Constants.Constant |
| 6 | +import Contexts.Context |
| 7 | +import Decorators._ |
| 8 | +import Flags._ |
| 9 | +import ast.Trees._ |
| 10 | +import Definitions._ |
| 11 | +import DenotTransformers._ |
| 12 | +import StdNames._ |
| 13 | +import Symbols._ |
| 14 | +import MegaPhase._ |
| 15 | +import Types._ |
| 16 | +import dotty.tools.dotc.ast.tpd |
| 17 | + |
| 18 | +import scala.annotation.tailrec |
| 19 | + |
| 20 | +/** Optimize generic operations on tuples */ |
| 21 | +class TupleOptimizations extends MiniPhase with IdentityDenotTransformer { |
| 22 | + import tpd._ |
| 23 | + |
| 24 | + def phaseName: String = "genericTuples" |
| 25 | + |
| 26 | + override def transformApply(tree: tpd.Apply)(implicit ctx: Context): tpd.Tree = { |
| 27 | + if (!tree.symbol.exists || tree.symbol.owner != defn.DynamicTupleModuleClass) tree |
| 28 | + else if (tree.symbol == defn.DynamicTuple_dynamicCons) transformTupleCons(tree) |
| 29 | + else if (tree.symbol == defn.DynamicTuple_dynamicTail) transformTupleTail(tree) |
| 30 | + else if (tree.symbol == defn.DynamicTuple_dynamicSize) transformTupleSize(tree) |
| 31 | + else if (tree.symbol == defn.DynamicTuple_dynamicConcat) transformTupleConcat(tree) |
| 32 | + else if (tree.symbol == defn.DynamicTuple_dynamicApply) transformTupleApply(tree) |
| 33 | + else if (tree.symbol == defn.DynamicTuple_dynamicToArray) transformTupleToArray(tree) |
| 34 | + else tree |
| 35 | + } |
| 36 | + |
| 37 | + private def transformTupleCons(tree: tpd.Apply)(implicit ctx: Context): Tree = { |
| 38 | + val head :: tail :: Nil = tree.args |
| 39 | + tupleTypes(tree.tpe) match { |
| 40 | + case Some(tpes) => |
| 41 | + // Generate a the tuple directly with TupleN+1.apply |
| 42 | + val size = tpes.size |
| 43 | + if (size <= 5) { |
| 44 | + // val t = tail |
| 45 | + // TupleN+1(head, t._1, ..., t._n) |
| 46 | + evalOnce(Typed(tail, TypeTree(defn.tupleType(tpes.tail)))) { tup => |
| 47 | + val elements = head :: tupleSelectors(tup, size - 1) |
| 48 | + knownTupleFromElements(tpes, elements) |
| 49 | + } |
| 50 | + } else { |
| 51 | + // val it = Iterator.single(head) ++ tail.asInstanceOf[Product].productIterator |
| 52 | + // TupleN+1(it.next(), ..., it.next()) |
| 53 | + val fullIterator = ref(defn.DynamicTuple_consIterator).appliedToArgs(head :: tail :: Nil) |
| 54 | + evalOnce(fullIterator) { it => |
| 55 | + knownTupleFromIterator(tpes.length, it).asInstance(tree.tpe) |
| 56 | + } |
| 57 | + } |
| 58 | + case _ => |
| 59 | + // No optimization, keep: |
| 60 | + // DynamicTuple.dynamicCons:(tail, head) |
| 61 | + tree |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private def transformTupleTail(tree: tpd.Apply)(implicit ctx: Context): Tree = { |
| 66 | + val Apply(TypeApply(_, tpt :: Nil), tup :: Nil) = tree |
| 67 | + tupleTypes(tpt.tpe, MaxTupleArity + 1) match { |
| 68 | + case Some(tpes) => |
| 69 | + // Generate a the tuple directly with TupleN-1.apply |
| 70 | + val size = tpes.size |
| 71 | + assert(size > 0) |
| 72 | + if (size == 1) { |
| 73 | + // () |
| 74 | + Literal(Constant(())) |
| 75 | + } |
| 76 | + else if (size <= 5) { |
| 77 | + // val t = tup.asInstanceOf[TupleN[...]] |
| 78 | + // TupleN-1(t._2, ..., t._n) |
| 79 | + evalOnce(Typed(tup, TypeTree(defn.tupleType(tpes)))) { tup => |
| 80 | + val elements = tupleSelectors(tup, size).tail |
| 81 | + knownTupleFromElements(tpes.tail, elements) |
| 82 | + } |
| 83 | + } else if (size <= MaxTupleArity + 1) { |
| 84 | + // val it = this.asInstanceOf[Product].productIterator |
| 85 | + // it.next() |
| 86 | + // TupleN-1(it.next(), ..., it.next()) |
| 87 | + evalOnce(tup.asInstance(defn.ProductType).select(nme.productIterator)) { it => |
| 88 | + Block( |
| 89 | + it.select(nme.next).ensureApplied :: Nil, |
| 90 | + knownTupleFromIterator(size - 1, it).asInstance(tree.tpe) |
| 91 | + ) |
| 92 | + } |
| 93 | + } else { |
| 94 | + // tup.asInstanceOf[TupleXXL].tailXXL |
| 95 | + tup.asInstance(defn.TupleXXLType).select("tailXXL".toTermName) |
| 96 | + } |
| 97 | + case None => |
| 98 | + // No optimization, keep: |
| 99 | + // DynamicTuple.dynamicTail(tup) |
| 100 | + tree |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private def transformTupleSize(tree: tpd.Apply)(implicit ctx: Context): Tree = { |
| 105 | + tree.tpe.tryNormalize match { |
| 106 | + case tp: ConstantType => Literal(tp.value) |
| 107 | + case _ => tree |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private def transformTupleConcat(tree: tpd.Apply)(implicit ctx: Context): Tree = { |
| 112 | + val Apply(TypeApply(_, selfTp :: thatTp :: Nil), self :: that :: Nil) = tree |
| 113 | + |
| 114 | + (tupleTypes(selfTp.tpe), tupleTypes(that.tpe.widenTermRefExpr)) match { |
| 115 | + case (Some(tpes1), Some(tpes2)) => |
| 116 | + // Generate a the tuple directly with TupleN+M.apply |
| 117 | + val n = tpes1.size |
| 118 | + val m = tpes2.size |
| 119 | + if (n == 0) that |
| 120 | + else if (m == 0) self |
| 121 | + else if (n + m < 5) { |
| 122 | + // val t = self |
| 123 | + // val u = that |
| 124 | + // TupleN+M(t._1,..., t._N, u._1, ..., u._M) |
| 125 | + evalOnce(Typed(self, TypeTree(defn.tupleType(tpes1)))) { self => |
| 126 | + evalOnce(Typed(that, TypeTree(defn.tupleType(tpes2)))) { that => |
| 127 | + val types = tpes1 ::: tpes2 |
| 128 | + val elements = tupleSelectors(self, n) ::: tupleSelectors(that, m) |
| 129 | + knownTupleFromElements(types, elements) |
| 130 | + } |
| 131 | + } |
| 132 | + } else { |
| 133 | + // val it = self.asInstanceOf[Product].productIterator ++ that.asInstanceOf[Product].productIterator |
| 134 | + // TupleN+M(it.next(), ..., it.next()) |
| 135 | + val fullIterator = ref(defn.DynamicTuple_concatIterator).appliedToArgs(tree.args) |
| 136 | + evalOnce(fullIterator) { it => |
| 137 | + knownTupleFromIterator(n + m, it).asInstance(tree.tpe) |
| 138 | + } |
| 139 | + } |
| 140 | + case _ => |
| 141 | + // No optimization, keep: |
| 142 | + // DynamicTuple.dynamicCons[This, that.type](self, that) |
| 143 | + tree |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private def transformTupleApply(tree: tpd.Apply)(implicit ctx: Context): Tree = { |
| 148 | + val Apply(TypeApply(_, tpt :: nTpt :: Nil), tup :: nTree :: Nil) = tree |
| 149 | + (tupleTypes(tpt.tpe), nTpt.tpe) match { |
| 150 | + case (Some(tpes), nTpe: ConstantType) => |
| 151 | + // Get the element directly with TupleM._n+1 or TupleXXL.productElement(n) |
| 152 | + val size = tpes.size |
| 153 | + val n = nTpe.value.intValue |
| 154 | + if (n < 0 || n >= size) { |
| 155 | + ctx.error("index out of bounds: " + n, nTree.underlyingArgument.sourcePos) |
| 156 | + tree |
| 157 | + } else if (size <= MaxTupleArity) { |
| 158 | + // tup._n |
| 159 | + Typed(tup, TypeTree(defn.tupleType(tpes))).select(nme.selectorName(n)) |
| 160 | + } else { |
| 161 | + // tup.asInstanceOf[TupleXXL].productElement(n) |
| 162 | + tup.asInstance(defn.TupleXXLType).select(nme.productElement).appliedTo(Literal(nTpe.value)) |
| 163 | + } |
| 164 | + case (None, nTpe: ConstantType) if nTpe.value.intValue < 0 => |
| 165 | + ctx.error("index out of bounds: " + nTpe.value.intValue, nTree.sourcePos) |
| 166 | + tree |
| 167 | + case _ => |
| 168 | + // No optimization, keep: |
| 169 | + // DynamicTuple.dynamicApply(tup, n) |
| 170 | + tree |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private def transformTupleToArray(tree: tpd.Apply)(implicit ctx: Context): Tree = { |
| 175 | + val Apply(_, tup :: Nil) = tree |
| 176 | + tupleTypes(tup.tpe.widen, MaxTupleArity) match { |
| 177 | + case Some(tpes) => |
| 178 | + val size = tpes.size |
| 179 | + if (size == 0) { |
| 180 | + // Array.emptyObjectArray |
| 181 | + ref(defn.ArrayModule.companionModule).select("emptyObjectArray".toTermName).ensureApplied |
| 182 | + } else if (size <= MaxTupleArity) { |
| 183 | + // DynamicTuple.productToArray(tup.asInstanceOf[Product]) |
| 184 | + ref(defn.DynamicTuple_productToArray).appliedTo(tup.asInstance(defn.ProductType)) |
| 185 | + } else { |
| 186 | + // tup.asInstanceOf[TupleXXL].elems.clone() |
| 187 | + tup.asInstance(defn.TupleXXLType).select(nme.toArray) |
| 188 | + } |
| 189 | + case None => |
| 190 | + // No optimization, keep: |
| 191 | + // DynamicTuple.dynamicToArray(tup) |
| 192 | + tree |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /** Create a TupleN (1 <= N < 23) from the elements */ |
| 197 | + private def knownTupleFromElements(tpes: List[Type], elements: List[Tree])(implicit ctx: Context) = { |
| 198 | + val size = elements.size |
| 199 | + assert(0 < size && size <= MaxTupleArity) |
| 200 | + val tupleModule = defn.TupleType(size).classSymbol.companionModule |
| 201 | + ref(tupleModule).select(nme.apply).appliedToTypes(tpes).appliedToArgs(elements) |
| 202 | + } |
| 203 | + |
| 204 | + private def knownTupleFromIterator(size: Int, it: Tree)(implicit ctx: Context): Tree = { |
| 205 | + if (size == 0) { |
| 206 | + // Unit for empty tuple |
| 207 | + Literal(Constant(())) // TODO should this code be here? Or assert(size > specializedSize) |
| 208 | + } |
| 209 | + else if (size <= MaxTupleArity) { |
| 210 | + // TupleN(it.next(), ..., it.next()) |
| 211 | + |
| 212 | + // TODO outline this code for the 22 alternatives (or less, may not need the smallest ones)? |
| 213 | + // This would yield smaller bytecode at the cost of an extra (easily JIT inlinable) call. |
| 214 | + // def dynamicTupleN(it: Iterator[Any]): TupleN[Any, ..., Any] = Tuple(it.next(), ..., it.next()) |
| 215 | + val tpes = List.fill(size)(defn.AnyType) |
| 216 | + val elements = (0 until size).map(_ => it.select(nme.next)).toList |
| 217 | + knownTupleFromElements(tpes, elements) |
| 218 | + } else { |
| 219 | + // No optimization, keep: |
| 220 | + // TupleXXL.fromIterator(it) |
| 221 | + ref(defn.TupleXXL_fromIterator).appliedTo(it) |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + private def tupleTypes(tp: Type, bound: Int = Int.MaxValue)(implicit ctx: Context): Option[List[Type]] = { |
| 226 | + @tailrec def rec(tp: Type, acc: List[Type], bound: Int): Option[List[Type]] = tp match { |
| 227 | + case _ if bound < 0 => Some(acc.reverse) |
| 228 | + case tp: AppliedType if defn.PairClass == tp.classSymbol => rec(tp.args(1), tp.args.head :: acc, bound - 1) |
| 229 | + case tp: AppliedType if defn.isTupleClass(tp.tycon.classSymbol) => Some(acc.reverse ::: tp.args) |
| 230 | + case tp if tp.classSymbol == defn.UnitClass => Some(acc.reverse) |
| 231 | + case _ => None |
| 232 | + } |
| 233 | + rec(tp.stripTypeVar, Nil, bound) |
| 234 | + } |
| 235 | + |
| 236 | + private def tupleSelectors(tup: Tree, size: Int)(implicit ctx: Context): List[Tree] = |
| 237 | + (0 until size).map(i => tup.select(nme.selectorName(i))).toList |
| 238 | + |
| 239 | +} |
0 commit comments