|
| 1 | +package dotty.tools.benchmarks |
| 2 | + |
| 3 | +import java.nio.file.{Files, Paths, Path} |
| 4 | +import java.util.Random |
| 5 | + |
| 6 | +/** Generates benchmarks in `genDirName`. |
| 7 | + * |
| 8 | + * Called automatically by the benchmarks runner ([[Bench.main]]). |
| 9 | + */ |
| 10 | +def generateBenchmarks(genDirName: String) = |
| 11 | + val thisFile = Paths.get("src/main/scala/generateBenchmarks.scala") |
| 12 | + val genDir = Paths.get(genDirName) |
| 13 | + |
| 14 | + def generateBenchmark(subDirName: String, fileName: String, make: () => String) = |
| 15 | + val outputDir = genDir.resolve(Paths.get(subDirName)) |
| 16 | + Files.createDirectories(outputDir) |
| 17 | + val file = outputDir.resolve(Paths.get(fileName)) |
| 18 | + if !Files.exists(file) || |
| 19 | + Files.getLastModifiedTime(file).toMillis() < |
| 20 | + Files.getLastModifiedTime(thisFile).toMillis() then |
| 21 | + println(f"Generate benchmark $file") |
| 22 | + Files.write(file, make().getBytes()) |
| 23 | + |
| 24 | + // Big compile-time sums of constant integer types: (1.type + 2.type + …). |
| 25 | + // This should ideally have a linear complexity. |
| 26 | + generateBenchmark("compiletime-ops", "sum-constants.scala", () => |
| 27 | + val innerSum = (1 to 50) // Limited to 50 to avoid stackoverflows in the compiler. |
| 28 | + .map(i => f"$i") |
| 29 | + .mkString(" + ") |
| 30 | + val outerSum = (1 to 50) |
| 31 | + .map(_ => f"($innerSum)") |
| 32 | + .mkString(" + ") |
| 33 | + val vals = (1 to 50) |
| 34 | + .map(i => f"val v$i: $outerSum = ???") |
| 35 | + .mkString("\n\n ") |
| 36 | + |
| 37 | + f""" |
| 38 | +import scala.compiletime.ops.int.* |
| 39 | + |
| 40 | +object Test: |
| 41 | + val one: 1 = ??? |
| 42 | + val n: Int = ??? |
| 43 | + val m: Int = ??? |
| 44 | + |
| 45 | + $vals |
| 46 | + """ |
| 47 | + ) |
| 48 | + |
| 49 | + // Big compile-time sums of term reference types: (one.type + m.type + n.type |
| 50 | + // + one.type + m.type + n.type + …). This big type is normalized to (8000 + |
| 51 | + // 8000 * m.type + 8000 * n.type). |
| 52 | + generateBenchmark("compiletime-ops", "sum-termrefs.scala", () => |
| 53 | + val innerSum = (1 to 40) |
| 54 | + .map(_ => "one.type + m.type + n.type") |
| 55 | + .mkString(" + ") |
| 56 | + val outerSum = (1 to 20) |
| 57 | + .map(_ => f"($innerSum)") |
| 58 | + .mkString(" + ") |
| 59 | + val vals = (1 to 4) |
| 60 | + .map(i => f"val v$i: $outerSum = ???") |
| 61 | + .mkString("\n\n ") |
| 62 | + |
| 63 | + f""" |
| 64 | +import scala.compiletime.ops.int.* |
| 65 | + |
| 66 | +object Test: |
| 67 | + val one: 1 = ??? |
| 68 | + val n: Int = ??? |
| 69 | + val m: Int = ??? |
| 70 | + |
| 71 | + $vals |
| 72 | + """ |
| 73 | + ) |
| 74 | + |
| 75 | + // Big compile-time sums of term references: (n + m + …). The result type is |
| 76 | + // inferred. The goal of this benchmark is to measure the performance cost of |
| 77 | + // inferring precise types for arithmetic operations. |
| 78 | + generateBenchmark("compiletime-ops", "sum-termrefs-terms.scala", () => |
| 79 | + val innerSum = (1 to 40) |
| 80 | + .map(_ => "one + m + n") |
| 81 | + .mkString(" + ") |
| 82 | + val outerSum = (1 to 20) |
| 83 | + .map(_ => f"($innerSum)") |
| 84 | + .mkString(" + ") |
| 85 | + val vals = (1 to 4) |
| 86 | + .map(i => f"val v$i = $outerSum") |
| 87 | + .mkString("\n\n ") |
| 88 | + |
| 89 | + f""" |
| 90 | +import scala.compiletime.ops.int.* |
| 91 | + |
| 92 | +object Test: |
| 93 | + val one: 1 = ??? |
| 94 | + val n: Int = ??? |
| 95 | + val m: Int = ??? |
| 96 | + |
| 97 | + $vals |
| 98 | + """ |
| 99 | + ) |
| 100 | + |
| 101 | + // Big compile-time product of sums of term references: (one + n + m) * (one + |
| 102 | + // n + m) * …. The goal of this benchmark is to measure the performance impact |
| 103 | + // of distributing addition over multiplication during compile-time operations |
| 104 | + // normalization. |
| 105 | + generateBenchmark("compiletime-ops", "distribute.scala", () => |
| 106 | + val product = (1 to 18) |
| 107 | + .map(_ => "(one.type + m.type + n.type)") |
| 108 | + .mkString(" * ") |
| 109 | + val vals = (1 to 50) |
| 110 | + .map(i => f"val v$i: $product = ???") |
| 111 | + .mkString("\n\n ") |
| 112 | + |
| 113 | + f""" |
| 114 | +import scala.compiletime.ops.int.* |
| 115 | + |
| 116 | +object Test: |
| 117 | + val one: 1 = ??? |
| 118 | + val n: Int = ??? |
| 119 | + val m: Int = ??? |
| 120 | + |
| 121 | + $vals |
| 122 | + """ |
| 123 | + ) |
| 124 | + |
| 125 | + def applicationCount = 14 |
| 126 | + def applicationDepth = 10 |
| 127 | + def applicationVals = 2 |
| 128 | + |
| 129 | + // Compile-time sums of big applications: Op[Op[…], Op[…]] + Op[Op[…], Op[…]] |
| 130 | + // + …. Applications are deep balanced binary trees only differing in their |
| 131 | + // very last (top-right) leafs. These applications are compared pairwise in |
| 132 | + // order to sort the terms of the sum. |
| 133 | + generateBenchmark("compiletime-ops", "sum-applications.scala", () => |
| 134 | + def makeOp(depth: Int, last: Boolean, k: Int): String = |
| 135 | + if depth == 0 then f"Op[one.type, ${if last then k.toString else "n.type"}]" |
| 136 | + else f"Op[${makeOp(depth - 1, false, k)}, ${makeOp(depth - 1, last, k)}]" |
| 137 | + val sum = (applicationCount to 1 by -1) |
| 138 | + .map(k => makeOp(applicationDepth, true, k)) |
| 139 | + .mkString(" + ") |
| 140 | + val vals = (1 to applicationVals) |
| 141 | + .map(i => f"val v$i: $sum = ???") |
| 142 | + .mkString("\n\n ") |
| 143 | + |
| 144 | + f""" |
| 145 | +import scala.compiletime.ops.int.* |
| 146 | + |
| 147 | +object Test: |
| 148 | + val one: 1 = ??? |
| 149 | + val n: Int = ??? |
| 150 | + type SInt = Int & Singleton |
| 151 | + type Op[A <: SInt, B <: SInt] <:SInt |
| 152 | + |
| 153 | + $vals |
| 154 | + """ |
| 155 | + ) |
0 commit comments