|
| 1 | +package dotty.tools.benchmarks |
| 2 | + |
| 3 | +import dotty.tools.dotc._ |
| 4 | +import core.Contexts.Context |
| 5 | +import dotty.tools.FatalError |
| 6 | +import reporting._ |
| 7 | + |
| 8 | +import org.openjdk.jmh.results.RunResult |
| 9 | +import org.openjdk.jmh.runner.Runner |
| 10 | +import org.openjdk.jmh.runner.options.OptionsBuilder |
| 11 | +import org.openjdk.jmh.annotations._ |
| 12 | +import org.openjdk.jmh.results.format._ |
| 13 | +import java.util.concurrent.TimeUnit |
| 14 | + |
| 15 | +import scala.runtime.DynamicTuple |
| 16 | + |
| 17 | +object Bench { |
| 18 | + def main(args: Array[String]): Unit = { |
| 19 | + val (intArgs, args1) = args.span(x => try { x.toInt; true } catch { case _: Throwable => false } ) |
| 20 | + |
| 21 | + val warmup = if (intArgs.length > 0) intArgs(0).toInt else 30 |
| 22 | + val iterations = if (intArgs.length > 1) intArgs(1).toInt else 20 |
| 23 | + val forks = if (intArgs.length > 2) intArgs(2).toInt else 1 |
| 24 | + |
| 25 | + val opts = new OptionsBuilder() |
| 26 | + .shouldFailOnError(true) |
| 27 | + .jvmArgs("-Xms2G", "-Xmx2G") |
| 28 | + .mode(Mode.AverageTime) |
| 29 | + .timeUnit(TimeUnit.NANOSECONDS) |
| 30 | + .warmupIterations(warmup) |
| 31 | + .measurementIterations(iterations) |
| 32 | + .forks(forks) |
| 33 | + .build |
| 34 | + |
| 35 | + val runner = new Runner(opts) // full access to all JMH features, you can also provide a custom output Format here |
| 36 | + runner.run() // actually run the benchmarks |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +@State(Scope.Thread) |
| 41 | +class ConsConcatBenchmarks { |
| 42 | + @Param(Array("10", "20")) |
| 43 | + var size: Int = _ |
| 44 | + var tuple: Tuple = _ |
| 45 | + |
| 46 | + @Setup |
| 47 | + def setup(): Unit = { |
| 48 | + tuple = () |
| 49 | + |
| 50 | + for (i <- 1 to size) |
| 51 | + tuple = "string" *: tuple |
| 52 | + } |
| 53 | + |
| 54 | + @Benchmark |
| 55 | + def baseline(): Unit = {} |
| 56 | + |
| 57 | + // Cons benchmarks |
| 58 | + @Benchmark |
| 59 | + def tupleCons(): Tuple = { |
| 60 | + "string" *: tuple |
| 61 | + } |
| 62 | + |
| 63 | + @Benchmark |
| 64 | + def dynamicTupleCons(): Tuple = { |
| 65 | + DynamicTuple.dynamicCons("string", tuple) |
| 66 | + } |
| 67 | + |
| 68 | + // Concat benchmarks |
| 69 | + @Benchmark |
| 70 | + def tupleConcat(): Tuple = { |
| 71 | + tuple ++ tuple |
| 72 | + } |
| 73 | + |
| 74 | + @Benchmark |
| 75 | + def dynamicTupleConcat(): Tuple = { |
| 76 | + DynamicTuple.dynamicConcat(tuple, tuple) |
| 77 | + } |
| 78 | + |
| 79 | + // Interesting behaviour : I expect the two following to be the same because of TupleOptimizations.scala |
| 80 | + @Benchmark |
| 81 | + def tupleConcat2(): Tuple = { |
| 82 | + ("string", "string") ++ ("string", "string") |
| 83 | + } |
| 84 | + |
| 85 | + @Benchmark |
| 86 | + def tupleNoConcat(): Tuple = { |
| 87 | + ("string", "string", "string", "string") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +@State(Scope.Thread) |
| 92 | +class ApplyBenchmarks { |
| 93 | + val size: Int = 100 |
| 94 | + |
| 95 | + val tuple: NonEmptyTuple = { |
| 96 | + var t = () |
| 97 | + for (i <- 1 to size) |
| 98 | + t = "string" *: t |
| 99 | + t.asInstanceOf[NonEmptyTuple] |
| 100 | + } |
| 101 | + |
| 102 | + @Param(Array("1", "10", "25", "50", "75", "99")) |
| 103 | + var index: Int = _ |
| 104 | + |
| 105 | + // Apply benchmarks, doesn't work for some reason |
| 106 | + // @Benchmark |
| 107 | + // def tupleApply(): Unit = { |
| 108 | + // DynamicTuple.dynamicApply(tuple, index) |
| 109 | + // } |
| 110 | +} |
0 commit comments