Skip to content

Commit 90d99c3

Browse files
committed
Add AnnotationsMappingBenchmark
1 parent 85b1c0e commit 90d99c3

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package dotty.tools.benchmarks
2+
3+
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Level, Measurement, Mode as JMHMode, Param, Scope, Setup, State, Warmup}
4+
import java.util.concurrent.TimeUnit.SECONDS
5+
6+
import dotty.tools.dotc.{Driver, Run, Compiler}
7+
import dotty.tools.dotc.ast.{tpd, TreeTypeMap}, tpd.{Apply, Block, Tree, TreeAccumulator, TypeApply}
8+
import dotty.tools.dotc.core.Annotations.{Annotation, ConcreteAnnotation, EmptyAnnotation}
9+
import dotty.tools.dotc.core.Contexts.{ContextBase, Context, ctx, withMode}
10+
import dotty.tools.dotc.core.Mode
11+
import dotty.tools.dotc.core.Phases.Phase
12+
import dotty.tools.dotc.core.Symbols.{defn, mapSymbols, Symbol}
13+
import dotty.tools.dotc.core.Types.{AnnotatedType, NoType, SkolemType, TermRef, Type, TypeMap}
14+
import dotty.tools.dotc.parsing.Parser
15+
import dotty.tools.dotc.typer.TyperPhase
16+
17+
/** Measures the performance of mapping over annotated types.
18+
*
19+
* Run with: scala3-bench-micro / Jmh / run AnnotationsMappingBenchmark
20+
*/
21+
@Fork(value = 4)
22+
@Warmup(iterations = 4, time = 1, timeUnit = SECONDS)
23+
@Measurement(iterations = 4, time = 1, timeUnit = SECONDS)
24+
@BenchmarkMode(Array(JMHMode.Throughput))
25+
@State(Scope.Thread)
26+
class AnnotationsMappingBenchmark:
27+
var tp: Type = null
28+
var specialIntTp: Type = null
29+
var context: Context = null
30+
var typeFunction: Context ?=> Type => Type = null
31+
var typeMap: TypeMap = null
32+
33+
@Param(Array("v1", "v2", "v3", "v4"))
34+
var valName: String = null
35+
36+
@Param(Array("id", "mapInts"))
37+
var typeFunctionName: String = null
38+
39+
@Setup(Level.Iteration)
40+
def setup(): Unit =
41+
val testPhase =
42+
new Phase:
43+
final override def phaseName = "testPhase"
44+
final override def run(using ctx: Context): Unit =
45+
val pkg = ctx.compilationUnit.tpdTree.symbol
46+
tp = pkg.requiredClass("Test").requiredValueRef(valName).underlying
47+
specialIntTp = pkg.requiredClass("Test").requiredType("SpecialInt").typeRef
48+
context = ctx
49+
50+
val compiler =
51+
new Compiler:
52+
private final val baseCompiler = new Compiler()
53+
final override def phases = List(List(Parser()), List(TyperPhase()), List(testPhase))
54+
55+
val driver =
56+
new Driver:
57+
final override def newCompiler(using Context): Compiler = compiler
58+
59+
driver.process(Array("-classpath", System.getProperty("BENCH_CLASS_PATH"), "tests/someAnnotatedTypes.scala"))
60+
61+
typeFunction =
62+
typeFunctionName match
63+
case "id" => tp => tp
64+
case "mapInts" => tp => (if tp frozen_=:= defn.IntType then specialIntTp else tp)
65+
case _ => throw new IllegalArgumentException(s"Unknown type function: $typeFunctionName")
66+
67+
typeMap =
68+
new TypeMap(using context):
69+
final override def apply(tp: Type): Type = typeFunction(mapOver(tp))
70+
71+
@Benchmark def applyTypeMap() = typeMap.apply(tp)
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Test:
2+
class FlagAnnot extends annotation.StaticAnnotation
3+
class StringAnnot(val s: String) extends annotation.StaticAnnotation
4+
class LambdaAnnot(val f: Int => Boolean) extends annotation.StaticAnnotation
5+
6+
type SpecialInt <: Int
7+
8+
val v1: Int @FlagAnnot = 42
9+
10+
val v2: Int @StringAnnot("hello") = 42
11+
12+
val v3: Int @LambdaAnnot(it => it == 42) = 42
13+
14+
val v4: Int @LambdaAnnot(it => {
15+
def g(x: Int, y: Int) = x - y + 5
16+
g(it, 7) * 2 == 80
17+
}) = 42
18+
19+
/*val v5: Int @LambdaAnnot(it => {
20+
class Foo(x: Int):
21+
def xPlus10 = x + 10
22+
def xPlus20 = x + 20
23+
def xPlus(y: Int) = x + y
24+
val foo = Foo(it)
25+
foo.xPlus10 - foo.xPlus20 + foo.xPlus(30) == 62
26+
}) = 42*/
27+
28+
def main(args: Array[String]): Unit = ???

0 commit comments

Comments
 (0)