-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add compiletime benchmarks and fix performance regression #14273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mbovel
merged 2 commits into
scala:master
from
dotty-staging:mb/compiletime-ops-benchmarks
Jan 20, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
charts: | ||
|
||
- name: "Compile-time sums of constant integer types (generated)" | ||
url: https://github.com/lampepfl/dotty/blob/master/bench/src/main/scala/generateBenchmarks.scala | ||
lines: | ||
- key: compiletime-sum-constants | ||
label: bootstrapped | ||
|
||
- name: "Compile-time sums of term reference types (generated)" | ||
url: https://github.com/lampepfl/dotty/blob/master/bench/src/main/scala/generateBenchmarks.scala | ||
lines: | ||
- key: compiletime-sum-termrefs | ||
label: bootstrapped | ||
|
||
- name: "Sums of term references, result type inferred (generated)" | ||
url: https://github.com/lampepfl/dotty/blob/master/bench/src/main/scala/generateBenchmarks.scala | ||
lines: | ||
- key: compiletime-sum-termrefs-terms | ||
label: bootstrapped | ||
|
||
- name: "Compile-time sums of type applications (generated)" | ||
url: https://github.com/lampepfl/dotty/blob/master/bench/src/main/scala/generateBenchmarks.scala | ||
lines: | ||
- key: compiletime-sum-applications | ||
label: bootstrapped | ||
|
||
- name: "Compile-time additions inside multiplications (generated)" | ||
url: https://github.com/lampepfl/dotty/blob/master/bench/src/main/scala/generateBenchmarks.scala | ||
lines: | ||
- key: compiletime-distribute | ||
label: bootstrapped | ||
|
||
scripts: | ||
|
||
compiletime-sum-constants: | ||
- measure 6 6 7 1 $PROG_HOME/dotty/bench/tests-generated/compiletime-ops/sum-constants.scala | ||
|
||
compiletime-sum-termrefs: | ||
- measure 6 6 7 1 $PROG_HOME/dotty/bench/tests-generated/compiletime-ops/sum-termrefs.scala | ||
|
||
compiletime-sum-termrefs-terms: | ||
- measure 6 6 7 1 $PROG_HOME/dotty/bench/tests-generated/compiletime-ops/sum-termrefs-terms.scala | ||
|
||
compiletime-sum-applications: | ||
- measure 6 6 7 1 $PROG_HOME/dotty/bench/tests-generated/compiletime-ops/sum-applications.scala | ||
|
||
compiletime-distribute: | ||
- measure 6 6 7 1 $PROG_HOME/dotty/bench/tests-generated/compiletime-ops/distribute.scala | ||
|
||
config: | ||
pr_base_url: "https://github.com/lampepfl/dotty/pull/" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ includes: | |
- empty.yml | ||
- quotes.yml | ||
- tuples.yml | ||
- compiletime.yml | ||
|
||
|
||
config: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package dotty.tools.benchmarks | ||
|
||
import java.nio.file.{Files, Paths, Path} | ||
import java.util.Random | ||
|
||
/** Generates benchmarks in `genDirName`. | ||
* | ||
* Called automatically by the benchmarks runner ([[Bench.main]]). | ||
*/ | ||
def generateBenchmarks(genDirName: String) = | ||
val thisFile = Paths.get("src/main/scala/generateBenchmarks.scala") | ||
val genDir = Paths.get(genDirName) | ||
|
||
def generateBenchmark(subDirName: String, fileName: String, make: () => String) = | ||
val outputDir = genDir.resolve(Paths.get(subDirName)) | ||
Files.createDirectories(outputDir) | ||
val file = outputDir.resolve(Paths.get(fileName)) | ||
if !Files.exists(file) || | ||
Files.getLastModifiedTime(file).toMillis() < | ||
Files.getLastModifiedTime(thisFile).toMillis() then | ||
println(f"Generate benchmark $file") | ||
Files.write(file, make().getBytes()) | ||
|
||
// Big compile-time sums of constant integer types: (1.type + 2.type + …). | ||
// This should ideally have a linear complexity. | ||
generateBenchmark("compiletime-ops", "sum-constants.scala", () => | ||
val innerSum = (1 to 50) // Limited to 50 to avoid stackoverflows in the compiler. | ||
.map(i => f"$i") | ||
.mkString(" + ") | ||
val outerSum = (1 to 50) | ||
.map(_ => f"($innerSum)") | ||
.mkString(" + ") | ||
val vals = (1 to 50) | ||
.map(i => f"val v$i: $outerSum = ???") | ||
.mkString("\n\n ") | ||
|
||
f""" | ||
import scala.compiletime.ops.int.* | ||
|
||
object Test: | ||
val one: 1 = ??? | ||
val n: Int = ??? | ||
val m: Int = ??? | ||
|
||
$vals | ||
""" | ||
) | ||
|
||
// Big compile-time sums of term reference types: (one.type + m.type + n.type | ||
// + one.type + m.type + n.type + …). This big type is normalized to (8000 + | ||
// 8000 * m.type + 8000 * n.type). | ||
generateBenchmark("compiletime-ops", "sum-termrefs.scala", () => | ||
val innerSum = (1 to 40) | ||
.map(_ => "one.type + m.type + n.type") | ||
.mkString(" + ") | ||
val outerSum = (1 to 20) | ||
.map(_ => f"($innerSum)") | ||
.mkString(" + ") | ||
val vals = (1 to 4) | ||
.map(i => f"val v$i: $outerSum = ???") | ||
.mkString("\n\n ") | ||
|
||
f""" | ||
import scala.compiletime.ops.int.* | ||
|
||
object Test: | ||
val one: 1 = ??? | ||
val n: Int = ??? | ||
val m: Int = ??? | ||
|
||
$vals | ||
""" | ||
) | ||
|
||
// Big compile-time sums of term references: (n + m + …). The result type is | ||
// inferred. The goal of this benchmark is to measure the performance cost of | ||
// inferring precise types for arithmetic operations. | ||
generateBenchmark("compiletime-ops", "sum-termrefs-terms.scala", () => | ||
val innerSum = (1 to 40) | ||
.map(_ => "one + m + n") | ||
.mkString(" + ") | ||
val outerSum = (1 to 20) | ||
.map(_ => f"($innerSum)") | ||
.mkString(" + ") | ||
val vals = (1 to 4) | ||
.map(i => f"val v$i = $outerSum") | ||
.mkString("\n\n ") | ||
|
||
f""" | ||
import scala.compiletime.ops.int.* | ||
|
||
object Test: | ||
val one: 1 = ??? | ||
val n: Int = ??? | ||
val m: Int = ??? | ||
|
||
$vals | ||
""" | ||
) | ||
|
||
// Big compile-time product of sums of term references: (one + n + m) * (one + | ||
// n + m) * …. The goal of this benchmark is to measure the performance impact | ||
// of distributing addition over multiplication during compile-time operations | ||
// normalization. | ||
generateBenchmark("compiletime-ops", "distribute.scala", () => | ||
val product = (1 to 18) | ||
.map(_ => "(one.type + m.type + n.type)") | ||
.mkString(" * ") | ||
val vals = (1 to 50) | ||
.map(i => f"val v$i: $product = ???") | ||
.mkString("\n\n ") | ||
|
||
f""" | ||
import scala.compiletime.ops.int.* | ||
|
||
object Test: | ||
val one: 1 = ??? | ||
val n: Int = ??? | ||
val m: Int = ??? | ||
|
||
$vals | ||
""" | ||
) | ||
|
||
def applicationCount = 14 | ||
def applicationDepth = 10 | ||
def applicationVals = 2 | ||
|
||
// Compile-time sums of big applications: Op[Op[…], Op[…]] + Op[Op[…], Op[…]] | ||
// + …. Applications are deep balanced binary trees only differing in their | ||
// very last (top-right) leafs. These applications are compared pairwise in | ||
// order to sort the terms of the sum. | ||
generateBenchmark("compiletime-ops", "sum-applications.scala", () => | ||
def makeOp(depth: Int, last: Boolean, k: Int): String = | ||
if depth == 0 then f"Op[one.type, ${if last then k.toString else "n.type"}]" | ||
else f"Op[${makeOp(depth - 1, false, k)}, ${makeOp(depth - 1, last, k)}]" | ||
val sum = (applicationCount to 1 by -1) | ||
.map(k => makeOp(applicationDepth, true, k)) | ||
.mkString(" + ") | ||
val vals = (1 to applicationVals) | ||
.map(i => f"val v$i: $sum = ???") | ||
.mkString("\n\n ") | ||
|
||
f""" | ||
import scala.compiletime.ops.int.* | ||
|
||
object Test: | ||
val one: 1 = ??? | ||
val n: Int = ??? | ||
type SInt = Int & Singleton | ||
type Op[A <: SInt, B <: SInt] <:SInt | ||
|
||
$vals | ||
""" | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import scala.compiletime.ops.int.* | ||
|
||
object Test: | ||
val one: 1 = ??? | ||
val n: Int = ??? | ||
val m: Int = ??? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.