Skip to content

Commit 6c3f4da

Browse files
Add Pair to benchmark
This way, we have a benchmark test that does not rely on inner classes
1 parent 5c45bd7 commit 6c3f4da

File tree

5 files changed

+76
-2
lines changed

5 files changed

+76
-2
lines changed

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

+20-2
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,52 @@ import java.util.concurrent.TimeUnit.{SECONDS, MILLISECONDS}
55
import scala.util.Random
66

77
@BenchmarkMode(Array(Mode.AverageTime))
8-
@Fork(3)
8+
@Fork(2)
99
@Threads(3)
1010
@Warmup(iterations = 3, time = 3, timeUnit = SECONDS)
1111
@Measurement(iterations = 5, time = 5, timeUnit = SECONDS)
1212
@OutputTimeUnit(MILLISECONDS)
1313
@State(Scope.Benchmark)
1414
class InlineTraitBenchmark {
15-
// @Param(Array("100", "200", "300"))
1615
var matrixSize: Int = 300
1716

17+
var numPairs: Int = 3_000_000
18+
1819
def intMatrixElems: List[List[Int]] =
1920
List.tabulate(matrixSize, matrixSize)((_, _) => Random.nextInt())
2021

22+
def pairElems: List[(First, Second)] = List.tabulate(numPairs)(_ % 2 match {
23+
case 0 => (Random.nextInt(), Random.nextDouble())
24+
case 1 => (Random.nextInt(Char.MaxValue).asInstanceOf[Char], Random.nextInt(Short.MaxValue).asInstanceOf[Short])
25+
})
26+
2127
@Param(Array("standard", "specialized", "inlinetrait"))
2228
var libType: String = _
2329

2430
var m1: BenchmarkMatrix = _
2531
var m2: BenchmarkMatrix = _
2632

33+
var pairs: List[BenchmarkPair] = _
34+
2735
@Setup(Level.Trial)
2836
def setup = {
2937
Random.setSeed(matrixSize)
3038

3139
val matrixFactory = BenchmarkMatrix.ofType(libType)
3240
m1 = matrixFactory(intMatrixElems)
3341
m2 = matrixFactory(intMatrixElems)
42+
43+
val pairFactory = BenchmarkPair.ofType(libType)
44+
pairs = pairElems.map((_1, _2) => pairFactory(_1, _2))
3445
}
3546

3647
@Benchmark
3748
def matrixBenchmark = (m1 + m2) * m1 // O(n^3) loops
49+
50+
@Benchmark
51+
def pairsBenchmark = pairs.foldLeft(0){ case (sum, pair) => pair match {
52+
case BenchmarkPair(i: Int, d: Double) => 7 * i + 3 * d.toInt + sum
53+
case BenchmarkPair(c: Char, s: Short) => 5 * c + 2 * s + sum
54+
}
55+
}
3856
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dotty.tools.benchmarks.inlinetraits
2+
3+
import standard.{Pair => StdPair}
4+
import specialized.{Pair => SpePair}
5+
import inlinetrait.{
6+
Pair => InlPair,
7+
IntDoublePair => IDPair,
8+
CharShortPair => CSPair,
9+
}
10+
11+
type First = Int | Char
12+
type Second = Double | Short
13+
14+
class BenchmarkPair private (val _1: First, val _2: Second):
15+
def this(p: StdPair[First, Second]) = this(p._1, p._2)
16+
def this(p: SpePair[First, Second]) = this(p._1, p._2)
17+
def this(p: InlPair[First, Second]) = this(p._1, p._2)
18+
19+
object BenchmarkPair:
20+
def ofType(tpe: String): (First, Second) => BenchmarkPair =
21+
(_1: First, _2: Second) => tpe.toLowerCase() match {
22+
case "standard" => BenchmarkPair(StdPair(_1, _2))
23+
case "specialized" => BenchmarkPair(SpePair(_1, _2))
24+
case "inlinetrait" =>
25+
val concretePair: InlPair[First, Second] = (_1, _2) match {
26+
case (_1: Int, _2: Double) => IDPair(_1, _2)
27+
case (_1: Char, _2: Short) => CSPair(_1, _2)
28+
case _ => ???
29+
}
30+
BenchmarkPair(concretePair)
31+
}
32+
33+
def unapply(p: BenchmarkPair): Option[(First, Second)] = Some(p._1, p._2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package dotty.tools.benchmarks.inlinetraits
2+
package inlinetrait
3+
4+
inline trait Pair[+T1, +T2]:
5+
val _1: T1
6+
val _2: T2
7+
final def toTuple: (T1, T2) = (_1, _2)
8+
9+
case class IntDoublePair(_1: Int, _2: Double) extends Pair[Int, Double]
10+
case class CharShortPair(_1: Char, _2: Short) extends Pair[Char, Short]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dotty.tools.benchmarks.inlinetraits
2+
package specialized
3+
4+
import scala.specialized
5+
6+
case class Pair[@specialized(Int, Char) +T1, @specialized(Double, Short) +T2](_1: T1, _2: T2) {
7+
final def toTuple: (T1, T2) = (_1, _2)
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package dotty.tools.benchmarks.inlinetraits
2+
package standard
3+
4+
case class Pair[+T1, +T2](_1: T1, _2: T2):
5+
final def toTuple: (T1, T2) = (_1, _2)

0 commit comments

Comments
 (0)