|
| 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) |
0 commit comments