Skip to content

Commit 17593a1

Browse files
Manual specialization, remove unused toTuple
1 parent 6c3f4da commit 17593a1

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

bench-micro/src/main/scala/dotty/tools/benchmarks/inlinetraits/InlineTraitBenchmark.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class InlineTraitBenchmark {
4040
m1 = matrixFactory(intMatrixElems)
4141
m2 = matrixFactory(intMatrixElems)
4242

43-
val pairFactory = BenchmarkPair.ofType(libType)
44-
pairs = pairElems.map((_1, _2) => pairFactory(_1, _2))
43+
val pairFactory = (l: List[(First, Second)]) => l.map((_1, _2) => BenchmarkPair.ofType(libType)(_1, _2))
44+
pairs = pairFactory(pairElems)
4545
}
4646

4747
@Benchmark

bench-micro/src/main/scala/dotty/tools/benchmarks/inlinetraits/PairOps.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ object BenchmarkPair:
2020
def ofType(tpe: String): (First, Second) => BenchmarkPair =
2121
(_1: First, _2: Second) => tpe.toLowerCase() match {
2222
case "standard" => BenchmarkPair(StdPair(_1, _2))
23-
case "specialized" => BenchmarkPair(SpePair(_1, _2))
23+
case "specialized" =>
24+
val concretePair: SpePair[First, Second] = (_1, _2) match {
25+
case (_1: Int, _2: Double) => SpePair(_1, _2)
26+
case (_1: Char, _2: Short) => SpePair(_1, _2)
27+
case _ => ???
28+
}
29+
BenchmarkPair(concretePair)
2430
case "inlinetrait" =>
2531
val concretePair: InlPair[First, Second] = (_1, _2) match {
2632
case (_1: Int, _2: Double) => IDPair(_1, _2)

bench-micro/src/main/scala/dotty/tools/benchmarks/inlinetraits/inlinetrait/Pair.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package inlinetrait
44
inline trait Pair[+T1, +T2]:
55
val _1: T1
66
val _2: T2
7-
final def toTuple: (T1, T2) = (_1, _2)
87

9-
case class IntDoublePair(_1: Int, _2: Double) extends Pair[Int, Double]
10-
case class CharShortPair(_1: Char, _2: Short) extends Pair[Char, Short]
8+
class IntDoublePair(val _1: Int, val _2: Double) extends Pair[Int, Double]
9+
class CharShortPair(val _1: Char, val _2: Short) extends Pair[Char, Short]
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
package dotty.tools.benchmarks.inlinetraits
22
package specialized
33

4-
import scala.specialized
4+
/*
5+
* This implementation relies on the @specialized tag to specialize Pair.
6+
* However, @specialized does nothing in Scala 3. Therefore, an equivalent version is
7+
* recreated by hand further down, and the actual Scala 2 code is provided below:
8+
*
9+
* import scala.specialized
10+
*
11+
* class Pair[@specialized(Int, Char) +T1, @specialized(Double, Short) +T2](_1: T1, _2: T2) {}
12+
*/
513

6-
case class Pair[@specialized(Int, Char) +T1, @specialized(Double, Short) +T2](_1: T1, _2: T2) {
7-
final def toTuple: (T1, T2) = (_1, _2)
14+
class Pair[+T1, +T2] protected (val _1: T1, val _2: T2)
15+
16+
object Pair {
17+
def apply(_1: Int, _2: Double): Pair[Int, Double] = new Pair$mcID$sp(_1, _2)
18+
def apply(_1: Int, _2: Short): Pair[Int, Short] = new Pair$mcIS$sp(_1, _2)
19+
def apply(_1: Char, _2: Double): Pair[Char, Double] = new Pair$mcCD$sp(_1, _2)
20+
def apply(_1: Char, _2: Short): Pair[Char, Short] = new Pair$mcCS$sp(_1, _2)
821
}
22+
23+
private class Pair$mcID$sp(protected[this] val _1$mcI$sp: Int, protected[this] val _2$mcD$sp: Double) extends Pair[Int, Double](_1$mcI$sp, _2$mcD$sp)
24+
private class Pair$mcIS$sp(protected[this] val _1$mcI$sp: Int, protected[this] val _2$mcS$sp: Short) extends Pair[Int, Short](_1$mcI$sp, _2$mcS$sp)
25+
private class Pair$mcCD$sp(protected[this] val _1$mcC$sp: Char, protected[this] val _2$mcD$sp: Double) extends Pair[Char, Double](_1$mcC$sp, _2$mcD$sp)
26+
private class Pair$mcCS$sp(protected[this] val _1$mcC$sp: Char, protected[this] val _2$mcS$sp: Short) extends Pair[Char, Short](_1$mcC$sp, _2$mcS$sp)
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package dotty.tools.benchmarks.inlinetraits
22
package standard
33

4-
case class Pair[+T1, +T2](_1: T1, _2: T2):
5-
final def toTuple: (T1, T2) = (_1, _2)
4+
class Pair[+T1, +T2](val _1: T1, val _2: T2)

0 commit comments

Comments
 (0)