Skip to content

Commit 6e8a489

Browse files
Rework benchmark files for report
1 parent 5c82ee0 commit 6e8a489

File tree

7 files changed

+68
-58
lines changed

7 files changed

+68
-58
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dotty.tools.benchmarks.inlinetraits
2+
3+
import org.openjdk.jmh.annotations._
4+
import java.util.concurrent.TimeUnit.SECONDS
5+
import scala.util.Random
6+
7+
@Fork(10)
8+
@Threads(3)
9+
@Warmup(iterations = 3, time = 5, timeUnit = SECONDS)
10+
@Measurement(iterations = 5, time = 10, timeUnit = SECONDS)
11+
@State(Scope.Benchmark)
12+
class MatrixBenchmark {
13+
val n: Int = 100
14+
15+
def intMatrixElems: List[List[Int]] =
16+
List.tabulate(n, n)((_, _) => Random.nextInt())
17+
18+
@Param(Array("standard", "specialized", "inlinetrait"))
19+
var libType: String = _
20+
21+
var m1: BenchmarkMatrix = _
22+
var m2: BenchmarkMatrix = _
23+
24+
@Setup(Level.Trial)
25+
def setup = {
26+
Random.setSeed(n)
27+
28+
val matrixFactory = BenchmarkMatrix.ofType(libType)
29+
m1 = matrixFactory(intMatrixElems)
30+
m2 = matrixFactory(intMatrixElems)
31+
}
32+
33+
@Benchmark
34+
def matrixBenchmark = (m1 + m2) * m1 // O(n^3) loops
35+
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ object BenchmarkMatrix:
1717
}
1818

1919
private class StdBenchmarkMatrix(val m: StdIntMatrix) extends BenchmarkMatrix:
20+
import standard.IntMatrixLib.{+, `*`}
2021
override def +(n: BenchmarkMatrix): StdBenchmarkMatrix = n match {
2122
case stdN: StdBenchmarkMatrix => StdBenchmarkMatrix(this.m + stdN.m)
2223
}
@@ -25,16 +26,16 @@ private class StdBenchmarkMatrix(val m: StdIntMatrix) extends BenchmarkMatrix:
2526
}
2627

2728
private class SpeBenchmarkMatrix(val m: SpeIntMatrix) extends BenchmarkMatrix:
28-
import specialized.IntMatrixLib
29-
29+
import specialized.IntMatrixLib.{+ => plus, `*` => times}
3030
override def +(n: BenchmarkMatrix): SpeBenchmarkMatrix = n match {
31-
case speN: SpeBenchmarkMatrix => SpeBenchmarkMatrix(IntMatrixLib.+(this.m)(speN.m))
31+
case speN: SpeBenchmarkMatrix => SpeBenchmarkMatrix(plus(this.m)(speN.m))
3232
}
3333
override def *(n: BenchmarkMatrix): SpeBenchmarkMatrix = n match {
34-
case speN: SpeBenchmarkMatrix => SpeBenchmarkMatrix(IntMatrixLib.*(this.m)(speN.m))
34+
case speN: SpeBenchmarkMatrix => SpeBenchmarkMatrix(times(this.m)(speN.m))
3535
}
3636

3737
private class InlBenchmarkMatrix(val m: InlIntMatrix) extends BenchmarkMatrix:
38+
import inlinetrait.IntMatrixLib.{+, `*`}
3839
override def +(n: BenchmarkMatrix): InlBenchmarkMatrix = n match {
3940
case inlN: InlBenchmarkMatrix => InlBenchmarkMatrix(this.m + inlN.m)
4041
}

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

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
package dotty.tools.benchmarks.inlinetraits
22

33
import org.openjdk.jmh.annotations._
4-
import java.util.concurrent.TimeUnit.{SECONDS, MILLISECONDS}
4+
import java.util.concurrent.TimeUnit.SECONDS
55
import scala.util.Random
66

7-
@BenchmarkMode(Array(Mode.AverageTime))
8-
@Fork(2)
7+
@Fork(10)
98
@Threads(3)
10-
@Warmup(iterations = 3, time = 3, timeUnit = SECONDS)
11-
@Measurement(iterations = 5, time = 5, timeUnit = SECONDS)
12-
@OutputTimeUnit(MILLISECONDS)
9+
@Warmup(iterations = 3, time = 5, timeUnit = SECONDS)
10+
@Measurement(iterations = 5, time = 10, timeUnit = SECONDS)
1311
@State(Scope.Benchmark)
14-
class InlineTraitBenchmark {
15-
var matrixSize: Int = 300
16-
17-
var numPairs: Int = 3_000_000
18-
19-
def intMatrixElems: List[List[Int]] =
20-
List.tabulate(matrixSize, matrixSize)((_, _) => Random.nextInt())
12+
class PairsBenchmark {
13+
val numPairs: Int = 3_000_000
2114

2215
def pairElems: List[(First, Second)] = List.tabulate(numPairs)(_ % 2 match {
2316
case 0 => (Random.nextInt(), Random.nextDouble())
@@ -27,26 +20,16 @@ class InlineTraitBenchmark {
2720
@Param(Array("standard", "specialized", "inlinetrait"))
2821
var libType: String = _
2922

30-
var m1: BenchmarkMatrix = _
31-
var m2: BenchmarkMatrix = _
32-
3323
var pairs: List[BenchmarkPair] = _
3424

3525
@Setup(Level.Trial)
3626
def setup = {
37-
Random.setSeed(matrixSize)
38-
39-
val matrixFactory = BenchmarkMatrix.ofType(libType)
40-
m1 = matrixFactory(intMatrixElems)
41-
m2 = matrixFactory(intMatrixElems)
27+
Random.setSeed(numPairs)
4228

4329
val pairFactory = (l: List[(First, Second)]) => l.map((_1, _2) => BenchmarkPair.ofType(libType)(_1, _2))
4430
pairs = pairFactory(pairElems)
4531
}
4632

47-
@Benchmark
48-
def matrixBenchmark = (m1 + m2) * m1 // O(n^3) loops
49-
5033
@Benchmark
5134
def pairsBenchmark = pairs.foldLeft(0){ case (sum, pair) => pair match {
5235
case BenchmarkPair(i: Int, d: Double) => 7 * i + 3 * d.toInt + sum

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

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,28 @@ package inlinetrait
33

44
import scala.reflect.ClassTag
55

6-
// FIXME uncomment the following code when inline traits work
7-
// inline trait MatrixLib[T: ClassTag]:
8-
// opaque type Matrix = Array[Array[T]]
9-
10-
// object Matrix:
11-
// def apply(rows: Seq[T]*): Matrix =
12-
// rows.map(_.toArray).toArray
13-
14-
// extension (m: Matrix)
15-
// def apply(x: Int)(y: Int): T = m(x)(y)
16-
// def rows: Int = m.length
17-
// def cols: Int = m(0).length
18-
19-
object IntMatrixLib /*extends MatrixLib[Int]*/:
20-
// FIXME remove manually "generated" code below and replace `IntMatrix` with `Matrix` when inline traits work properly
6+
inline trait MatrixLib[T: ClassTag]:
7+
// FIXME make Matrix opaque once inlinedTypeRef works properly
8+
/*opaque*/ type Matrix = Array[Array[T]]
9+
10+
// FIXME uncomment object and remove following code when inner objects work
11+
/*
12+
* object Matrix:
13+
* def apply(rows: Seq[T]*): Matrix =
14+
* rows.map(_.toArray).toArray
15+
*/
2116
// ----------------------------------
22-
opaque type Matrix = Array[Array[Int]]
23-
24-
object Matrix:
25-
def apply(rows: Seq[Int]*): Matrix =
26-
rows.map(_.toArray).toArray
27-
28-
extension (m: Matrix)
29-
/*override*/ def apply(x: Int)(y: Int): Int = m(x)(y)
30-
/*override*/ def rows: Int = m.length
31-
/*override*/ def cols: Int = m(0).length
17+
def Matrix(rows: Seq[T]*): Matrix =
18+
rows.map(_.toArray).toArray
3219
// ----------------------------------
3320
// end of code to remove
3421

22+
extension (m: Matrix)
23+
def apply(x: Int)(y: Int): T = m(x)(y)
24+
def rows: Int = m.length
25+
def cols: Int = m(0).length
26+
27+
object IntMatrixLib extends MatrixLib[Int]:
3528
extension (m: Matrix)
3629
def +(n: Matrix): Matrix =
3730
val sum =
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package dotty.tools.benchmarks.inlinetraits
22
package inlinetrait
33

4-
inline trait Pair[+T1, +T2]:
5-
val _1: T1
6-
val _2: T2
4+
inline trait Pair[+T1, +T2](val _1: T1, val _2: T2)
75

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]
6+
class IntDoublePair(override val _1: Int, override val _2: Double) extends Pair[Int, Double](_1, _2)
7+
class CharShortPair(override val _1: Char, override val _2: Short) extends Pair[Char, Short](_1, _2)

0 commit comments

Comments
 (0)