Skip to content

Commit 9aca3a3

Browse files
committed
Cleanup code in DynamicTuple
* Remove dead code * Remove uses of scala.Tuple.* match types * Remove `dynamic` prefix from methods in `DynamicTuple`
1 parent b4e037c commit 9aca3a3

File tree

16 files changed

+176
-224
lines changed

16 files changed

+176
-224
lines changed

bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Apply.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Apply {
2222

2323
@Benchmark
2424
def tupleApply(): Any = {
25-
DynamicTuple.dynamicApply(tuple, index)
25+
DynamicTuple.apply(tuple, index)
2626
}
2727

2828
@Benchmark

bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Concat.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ class Concat {
2828

2929
@Benchmark
3030
def tupleConcat(): Tuple = {
31-
DynamicTuple.dynamicConcat(tuple1, tuple2)
31+
DynamicTuple.concat(tuple1, tuple2)
3232
}
3333
}

bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Cons.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Cons {
2424

2525
@Benchmark
2626
def tupleCons(): Tuple = {
27-
DynamicTuple.dynamicCons("elem", tuple)
27+
DynamicTuple.cons("elem", tuple)
2828
}
2929

3030
@Benchmark

bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Conversions.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ class Conversions {
2424

2525
@Benchmark
2626
def tupleToArray(): Array[Object] = {
27-
DynamicTuple.dynamicToArray(tuple)
27+
DynamicTuple.toArray(tuple)
2828
}
2929

3030
@Benchmark
3131
def tupleToIArray(): IArray[Object] = {
32-
DynamicTuple.dynamicToIArray(tuple)
32+
DynamicTuple.toIArray(tuple)
3333
}
3434

3535
@Benchmark
3636
def tupleFromArray(): Tuple = {
37-
DynamicTuple.dynamicFromArray(array)
37+
DynamicTuple.fromArray(array)
3838
}
3939

4040
@Benchmark
4141
def tupleFromIArray(): Tuple = {
42-
DynamicTuple.dynamicFromIArray(iarray)
42+
DynamicTuple.fromIArray(iarray)
4343
}
4444

4545
@Benchmark

bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Map.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Map {
3030

3131
@Benchmark
3232
def tupleMap(): Tuple = {
33-
DynamicTuple.dynamicMap[Tuple, Id](tuple, [T] => (x:T) => x.asInstanceOf[String].updated(0, 'a').asInstanceOf[T])
33+
DynamicTuple.map[Id](tuple, [T] => (x:T) => x.asInstanceOf[String].updated(0, 'a').asInstanceOf[T])
3434
}
3535

3636
@Benchmark

bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Tail.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Tail {
2222

2323
@Benchmark
2424
def tupleTail(): Tuple = {
25-
DynamicTuple.dynamicTail(tuple)
25+
DynamicTuple.tail(tuple)
2626
}
2727

2828
@Benchmark

bench-run/src/main/scala/dotty/tools/benchmarks/tuples/Zip.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Zip {
2828

2929
@Benchmark
3030
def tupleZip(): Tuple = {
31-
DynamicTuple.dynamicZip(tuple1, tuple2)
31+
DynamicTuple.zip(tuple1, tuple2)
3232
}
3333

3434
@Benchmark

bench-run/src/main/scala/tuples/Drop.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Drop {
2424

2525
@Benchmark
2626
def tupleDrop(): Tuple = {
27-
DynamicTuple.dynamicDrop(tuple, half)
27+
DynamicTuple.drop(tuple, half)
2828
}
2929

3030
@Benchmark

bench-run/src/main/scala/tuples/Split.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Split {
2424

2525
@Benchmark
2626
def tupleSplit(): (Tuple, Tuple) = {
27-
DynamicTuple.dynamicSplitAt(tuple, half)
27+
DynamicTuple.splitAt(tuple, half)
2828
}
2929

3030
@Benchmark

bench-run/src/main/scala/tuples/Take.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Take {
2424

2525
@Benchmark
2626
def tupleTake(): Tuple = {
27-
DynamicTuple.dynamicTake(tuple, half)
27+
DynamicTuple.take(tuple, half)
2828
}
2929

3030
@Benchmark

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,12 @@ class Definitions {
758758
@tu lazy val DynamicTupleModuleClass: Symbol = DynamicTupleModule.moduleClass
759759
lazy val DynamicTuple_consIterator: Symbol = DynamicTupleModule.requiredMethod("consIterator")
760760
lazy val DynamicTuple_concatIterator: Symbol = DynamicTupleModule.requiredMethod("concatIterator")
761-
lazy val DynamicTuple_dynamicApply: Symbol = DynamicTupleModule.requiredMethod("dynamicApply")
762-
lazy val DynamicTuple_dynamicCons: Symbol = DynamicTupleModule.requiredMethod("dynamicCons")
763-
lazy val DynamicTuple_dynamicSize: Symbol = DynamicTupleModule.requiredMethod("dynamicSize")
764-
lazy val DynamicTuple_dynamicTail: Symbol = DynamicTupleModule.requiredMethod("dynamicTail")
765-
lazy val DynamicTuple_dynamicConcat: Symbol = DynamicTupleModule.requiredMethod("dynamicConcat")
766-
lazy val DynamicTuple_dynamicToArray: Symbol = DynamicTupleModule.requiredMethod("dynamicToArray")
761+
lazy val DynamicTuple_apply: Symbol = DynamicTupleModule.requiredMethod("apply")
762+
lazy val DynamicTuple_cons: Symbol = DynamicTupleModule.requiredMethod("cons")
763+
lazy val DynamicTuple_size: Symbol = DynamicTupleModule.requiredMethod("size")
764+
lazy val DynamicTuple_tail: Symbol = DynamicTupleModule.requiredMethod("tail")
765+
lazy val DynamicTuple_concat: Symbol = DynamicTupleModule.requiredMethod("concat")
766+
lazy val DynamicTuple_toArray: Symbol = DynamicTupleModule.requiredMethod("toArray")
767767
lazy val DynamicTuple_productToArray: Symbol = DynamicTupleModule.requiredMethod("productToArray")
768768

769769
@tu lazy val TupledFunctionTypeRef: TypeRef = ctx.requiredClassRef("scala.TupledFunction")

compiler/src/dotty/tools/dotc/transform/TupleOptimizations.scala

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer {
2525

2626
override def transformApply(tree: tpd.Apply)(implicit ctx: Context): tpd.Tree =
2727
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)
28+
else if (tree.symbol == defn.DynamicTuple_cons) transformTupleCons(tree)
29+
else if (tree.symbol == defn.DynamicTuple_tail) transformTupleTail(tree)
30+
else if (tree.symbol == defn.DynamicTuple_size) transformTupleSize(tree)
31+
else if (tree.symbol == defn.DynamicTuple_concat) transformTupleConcat(tree)
32+
else if (tree.symbol == defn.DynamicTuple_apply) transformTupleApply(tree)
33+
else if (tree.symbol == defn.DynamicTuple_toArray) transformTupleToArray(tree)
3434
else tree
3535

3636
private def transformTupleCons(tree: tpd.Apply)(implicit ctx: Context): Tree = {
3737
val head :: tail :: Nil = tree.args
38-
defn.tupleTypes(tree.tpe) match {
38+
defn.tupleTypes(tree.tpe.widenTermRefExpr.dealias) match {
3939
case Some(tpes) =>
4040
// Generate a the tuple directly with TupleN+1.apply
4141
val size = tpes.size
@@ -56,14 +56,14 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer {
5656
}
5757
case _ =>
5858
// No optimization, keep:
59-
// DynamicTuple.dynamicCons:(tail, head)
59+
// DynamicTuple.cons(tail, head)
6060
tree
6161
}
6262
}
6363

6464
private def transformTupleTail(tree: tpd.Apply)(implicit ctx: Context): Tree = {
65-
val Apply(TypeApply(_, tpt :: Nil), tup :: Nil) = tree
66-
defn.tupleTypes(tpt.tpe, MaxTupleArity + 1) match {
65+
val Apply(_, tup :: Nil) = tree
66+
defn.tupleTypes(tup.tpe.widenTermRefExpr.dealias, MaxTupleArity + 1) match {
6767
case Some(tpes) =>
6868
// Generate a the tuple directly with TupleN-1.apply
6969
val size = tpes.size
@@ -93,7 +93,7 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer {
9393
tup.asInstance(defn.TupleXXLClass.typeRef).select("tailXXL".toTermName)
9494
case None =>
9595
// No optimization, keep:
96-
// DynamicTuple.dynamicTail(tup)
96+
// DynamicTuple.tail(tup)
9797
tree
9898
}
9999
}
@@ -105,9 +105,8 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer {
105105
}
106106

107107
private def transformTupleConcat(tree: tpd.Apply)(implicit ctx: Context): Tree = {
108-
val Apply(TypeApply(_, selfTp :: thatTp :: Nil), self :: that :: Nil) = tree
109-
110-
(defn.tupleTypes(selfTp.tpe), defn.tupleTypes(that.tpe.widenTermRefExpr)) match {
108+
val Apply(_, self :: that :: Nil) = tree
109+
(defn.tupleTypes(self.tpe.widenTermRefExpr.dealias), defn.tupleTypes(that.tpe.widenTermRefExpr.dealias)) match {
111110
case (Some(tpes1), Some(tpes2)) =>
112111
// Generate a the tuple directly with TupleN+M.apply
113112
val n = tpes1.size
@@ -135,14 +134,14 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer {
135134
}
136135
case _ =>
137136
// No optimization, keep:
138-
// DynamicTuple.dynamicCons[This, that.type](self, that)
137+
// DynamicTuple.cons(self, that)
139138
tree
140139
}
141140
}
142141

143142
private def transformTupleApply(tree: tpd.Apply)(implicit ctx: Context): Tree = {
144-
val Apply(TypeApply(_, tpt :: nTpt :: Nil), tup :: nTree :: Nil) = tree
145-
(defn.tupleTypes(tpt.tpe), nTpt.tpe) match {
143+
val Apply(_, tup :: nTree :: Nil) = tree
144+
(defn.tupleTypes(tup.tpe.widenTermRefExpr.dealias), nTree.tpe) match {
146145
case (Some(tpes), nTpe: ConstantType) =>
147146
// Get the element directly with TupleM._n+1 or TupleXXL.productElement(n)
148147
val size = tpes.size
@@ -162,7 +161,7 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer {
162161
tree
163162
case _ =>
164163
// No optimization, keep:
165-
// DynamicTuple.dynamicApply(tup, n)
164+
// DynamicTuple.apply(tup, n)
166165
tree
167166
}
168167
}
@@ -183,7 +182,7 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer {
183182
tup.asInstance(defn.TupleXXLClass.typeRef).select(nme.toArray)
184183
case None =>
185184
// No optimization, keep:
186-
// DynamicTuple.dynamicToArray(tup)
185+
// DynamicTuple.toArray(tup)
187186
tree
188187
}
189188
}
@@ -205,7 +204,7 @@ class TupleOptimizations extends MiniPhase with IdentityDenotTransformer {
205204

206205
// TODO outline this code for the 22 alternatives (or less, may not need the smallest ones)?
207206
// This would yield smaller bytecode at the cost of an extra (easily JIT inlinable) call.
208-
// def dynamicTupleN(it: Iterator[Any]): TupleN[Any, ..., Any] = Tuple(it.next(), ..., it.next())
207+
// def tupleN(it: Iterator[Any]): TupleN[Any, ..., Any] = Tuple(it.next(), ..., it.next())
209208
val tpes = List.fill(size)(defn.AnyType)
210209
val elements = (0 until size).map(_ => it.select(nme.next)).toList
211210
knownTupleFromElements(tpes, elements)

library/src/scala/Tuple.scala

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@ sealed trait Tuple extends Any {
1111

1212
/** Create a copy this tuple as an Array */
1313
inline def toArray: Array[Object] =
14-
DynamicTuple.dynamicToArray(this)
14+
DynamicTuple.toArray(this)
1515

1616
/** Create a copy this tuple as an IArray */
1717
inline def toIArray: IArray[Object] =
18-
DynamicTuple.dynamicToIArray(this)
18+
DynamicTuple.toIArray(this)
1919

2020
/** Return a new tuple by prepending the element to `this` tuple.
2121
* This opteration is O(this.size)
2222
*/
2323
inline def *: [H, This >: this.type <: Tuple] (x: H): H *: This =
24-
DynamicTuple.dynamicCons[H, This](x, this)
24+
DynamicTuple.cons(x, this).asInstanceOf[H *: This]
2525

2626
/** Return a new tuple by concatenating `this` tuple with `that` tuple.
2727
* This opteration is O(this.size + that.size)
2828
*/
2929
inline def ++ [This >: this.type <: Tuple](that: Tuple): Concat[This, that.type] =
30-
DynamicTuple.dynamicConcat[This, that.type](this, that)
30+
DynamicTuple.concat(this, that).asInstanceOf[Concat[This, that.type]]
3131

3232
/** Return the size (or arity) of the tuple */
3333
inline def size[This >: this.type <: Tuple]: Size[This] =
34-
DynamicTuple.dynamicSize(this)
34+
DynamicTuple.size(this).asInstanceOf[Size[This]]
3535

3636
/** Given two tuples, `(a1, ..., an)` and `(a1, ..., an)`, returns a tuple
3737
* `((a1, b1), ..., (an, bn))`. If the two tuples have different sizes,
@@ -41,35 +41,35 @@ sealed trait Tuple extends Any {
4141
* `(A1, B1) *: ... *: (Ai, Bi) *: Tuple`
4242
*/
4343
inline def zip[This >: this.type <: Tuple, T2 <: Tuple](t2: T2): Zip[This, T2] =
44-
DynamicTuple.dynamicZip(this, t2)
44+
DynamicTuple.zip(this, t2).asInstanceOf[Zip[This, T2]]
4545

4646
/** Called on a tuple `(a1, ..., an)`, returns a new tuple `(f(a1), ..., f(an))`.
4747
* The result is typed as `(F[A1], ..., F[An])` if the tuple type is fully known.
4848
* If the tuple is of the form `a1 *: ... *: Tuple` (that is, the tail is not known
4949
* to be the cons type.
5050
*/
5151
inline def map[F[_]](f: [t] => t => F[t]): Map[this.type, F] =
52-
DynamicTuple.dynamicMap(this, f)
52+
DynamicTuple.map(this, f).asInstanceOf[Map[this.type, F]]
5353

5454
/** Given a tuple `(a1, ..., am)`, returns the tuple `(a1, ..., an)` consisting
5555
* of its first n elements.
5656
*/
5757
inline def take[This >: this.type <: Tuple](n: Int): Take[This, n.type] =
58-
DynamicTuple.dynamicTake[This, n.type](this, n)
58+
DynamicTuple.take(this, n).asInstanceOf[Take[This, n.type]]
5959

6060

6161
/** Given a tuple `(a1, ..., am)`, returns the tuple `(an+1, ..., am)` consisting
6262
* all its elements except the first n ones.
6363
*/
6464
inline def drop[This >: this.type <: Tuple](n: Int): Drop[This, n.type] =
65-
DynamicTuple.dynamicDrop[This, n.type](this, n)
65+
DynamicTuple.drop(this, n).asInstanceOf[Drop[This, n.type]]
6666

6767
/** Given a tuple `(a1, ..., am)`, returns a pair of the tuple `(a1, ..., an)`
6868
* consisting of the first n elements, and the tuple `(an+1, ..., am)` consisting
6969
* of the remaining elements.
7070
*/
7171
inline def splitAt[This >: this.type <: Tuple](n: Int): Split[This, n.type] =
72-
DynamicTuple.dynamicSplitAt[This, n.type](this, n)
72+
DynamicTuple.splitAt(this, n).asInstanceOf[Split[This, n.type]]
7373
}
7474

7575
object Tuple {
@@ -165,7 +165,7 @@ object Tuple {
165165
case xs: Array[Object] => xs
166166
case xs => xs.map(_.asInstanceOf[Object])
167167
}
168-
DynamicTuple.dynamicFromArray[Tuple](xs2)
168+
DynamicTuple.fromArray(xs2).asInstanceOf[Tuple]
169169
}
170170

171171
/** Convert an immutable array into a tuple of unknown arity and types */
@@ -176,12 +176,12 @@ object Tuple {
176176
// TODO suport IArray.map
177177
xs.asInstanceOf[Array[T]].map(_.asInstanceOf[Object]).asInstanceOf[IArray[Object]]
178178
}
179-
DynamicTuple.dynamicFromIArray[Tuple](xs2)
179+
DynamicTuple.fromIArray(xs2).asInstanceOf[Tuple]
180180
}
181181

182182
/** Convert a Product into a tuple of unknown arity and types */
183183
def fromProduct(product: Product): Tuple =
184-
runtime.DynamicTuple.dynamicFromProduct[Tuple](product)
184+
runtime.DynamicTuple.fromProduct(product)
185185

186186
def fromProductTyped[P <: Product](p: P)(using m: scala.deriving.Mirror.ProductOf[P]): m.MirroredElemTypes =
187187
Tuple.fromArray(p.productIterator.toArray).asInstanceOf[m.MirroredElemTypes] // TODO use toIArray of Object to avoid double/triple array copy
@@ -195,17 +195,17 @@ sealed trait NonEmptyTuple extends Tuple {
195195
* Equivalent to productElement but with a precise return type.
196196
*/
197197
inline def apply[This >: this.type <: NonEmptyTuple](n: Int): Elem[This, n.type] =
198-
DynamicTuple.dynamicApply[This, n.type](this, n)
198+
DynamicTuple.apply(this, n).asInstanceOf[Elem[This, n.type]]
199199

200200
/** Get the head of this tuple */
201201
inline def head[This >: this.type <: NonEmptyTuple]: Head[This] =
202-
DynamicTuple.dynamicApply[This, 0](this, 0)
202+
DynamicTuple.apply(this, 0).asInstanceOf[Head[This]]
203203

204204
/** Get the tail of this tuple.
205205
* This opteration is O(this.size)
206206
*/
207207
inline def tail[This >: this.type <: NonEmptyTuple]: Tail[This] =
208-
DynamicTuple.dynamicTail[This](this)
208+
DynamicTuple.tail(this).asInstanceOf[Tail[This]]
209209

210210
}
211211

0 commit comments

Comments
 (0)