Skip to content

Commit

Permalink
Add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed May 23, 2019
1 parent f677b4c commit 54b0584
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class Compiler {
new AugmentScala2Traits, // Augments Scala2 traits with additional members needed for mixin composition.
new ResolveSuper, // Implement super accessors
new FunctionXXLForwarders, // Add forwarders for FunctionXXL apply method
new GenericTuples, // TODO
new TuplesOptimizations, // Optimize generic operations on tuples
new ArrayConstructors) :: // Intercept creation of (non-generic) arrays and intrinsify.
List(new Erasure) :: // Rewrite types to JVM model, erasing all type parameters, abstract types and refinements.
List(new ElimErasedValueType, // Expand erased value types to their underlying implmementation types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import dotty.tools.dotc.ast.tpd

import scala.annotation.tailrec

/** TODO
*/
class GenericTuples extends MiniPhase with IdentityDenotTransformer {
/** Optimize generic operations on tuples */
class TuplesOptimizations extends MiniPhase with IdentityDenotTransformer {
import tpd._

def phaseName: String = "genericTuples"
Expand All @@ -39,6 +38,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
val tail :: head :: Nil = tree.args
tupleTypes(tree.tpe) match {
case Some(tpes) =>
// Generate a the tuple directly with TupleN+1.apply
val size = tpes.size
if (size <= 5) {
// val t = tail
Expand All @@ -49,13 +49,14 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
}
} else {
// val it = Iterator.single(head) ++ tail.asInstanceOf[Product].productIterator
// TupleN(it.next(), ..., it.next())
// TupleN+1(it.next(), ..., it.next())
val fullIterator = ref(defn.DynamicTuple_consIterator).appliedToArgs(head :: tail :: Nil)
evalOnce(fullIterator) { it =>
knownTupleFromIterator(tpes.length, it).asInstance(tree.tpe)
}
}
case _ =>
// No optimization, keep:
// DynamicTuple.dynamicCons:(tail, head)
tree
}
Expand All @@ -65,6 +66,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
val Apply(TypeApply(_, tpt :: Nil), tup :: Nil) = tree
tupleTypes(tpt.tpe) match { // TODO tupleBoundedTypes
case Some(tpes) =>
// Generate a the tuple directly with TupleN-1.apply
val size = tpes.size
assert(size > 0)
if (size == 1) {
Expand All @@ -81,7 +83,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
} else {
// val it = this.asInstanceOf[Product].productIterator
// it.next()
// TupleN(it.next(), ..., it.next())
// TupleN-1(it.next(), ..., it.next())
evalOnce(tup.asInstance(defn.ProductType).select(nme.productIterator)) { it =>
Block(
it.select(nme.next).ensureApplied :: Nil,
Expand All @@ -90,6 +92,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
}
}
case None =>
// No optimization, keep:
// DynamicTuple.dynamicTail(tup)
tree
}
Expand All @@ -107,6 +110,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {

(tupleTypes(selfTp.tpe), tupleTypes(that.tpe.widenTermRefExpr)) match {
case (Some(tpes1), Some(tpes2)) =>
// Generate a the tuple directly with TupleN+M.apply
val n = tpes1.size
val m = tpes2.size
if (n == 0) that
Expand All @@ -127,13 +131,14 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
}
} else {
// val it = self.asInstanceOf[Product].productIterator ++ that.asInstanceOf[Product].productIterator
// TupleN(it.next(), ..., it.next())
// TupleN+M(it.next(), ..., it.next())
val fullIterator = ref(defn.DynamicTuple_concatIterator).appliedToArgs(tree.args)
evalOnce(fullIterator) { it =>
knownTupleFromIterator(n + m, it).asInstance(tree.tpe)
}
}
case _ =>
// No optimization, keep:
// DynamicTuple.dynamicCons[This, that.type](self, that)
tree
}
Expand All @@ -143,6 +148,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
val Apply(TypeApply(_, tpt :: nTpt :: Nil), tup :: nTree :: Nil) = tree
(tupleTypes(tpt.tpe), nTpt.tpe) match {
case (Some(tpes), nTpe: ConstantType) =>
// Get the element directly with TupleM._n+1 or TupleXXL.productElement(n)
val size = tpes.size
val n = nTpe.value.intValue
if (n < 0 || n >= size) {
Expand All @@ -159,6 +165,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
ctx.error("index out of bounds: " + nTpe.value.intValue, nTree.sourcePos)
tree
case _ =>
// No optimization, keep:
// DynamicTuple.dynamicApply(tup, n)
tree
}
Expand All @@ -180,6 +187,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
tup.asInstance(defn.TupleXXLType).select(nme.elems)
}
case None =>
// No optimization, keep:
// DynamicTuple.dynamicToArray(tup)
tree
}
Expand Down Expand Up @@ -208,6 +216,7 @@ class GenericTuples extends MiniPhase with IdentityDenotTransformer {
val elements = (0 until size).map(_ => it.select(nme.next)).toList
knownTupleFromElements(tpes, elements)
} else {
// No optimization, keep:
// TupleXXL.fromIterator(it)
ref(defn.TupleXXL_fromIterator).appliedTo(it)
}
Expand Down

0 comments on commit 54b0584

Please sign in to comment.