Skip to content

Commit c9b9aea

Browse files
authored
Some fixes for AnnotatedTypes mapping (#19957)
2 parents 8e9ded0 + ac76938 commit c9b9aea

23 files changed

+242
-18
lines changed
Lines changed: 71 additions & 0 deletions
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)

bench-micro/src/main/scala/dotty/tools/benchmarks/lazyvals/ContendedInitialization.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools.benchmarks.lazyvals
22

3+
import compiletime.uninitialized
34
import org.openjdk.jmh.annotations._
45
import LazyVals.LazyHolder
56
import org.openjdk.jmh.infra.Blackhole
@@ -16,12 +17,12 @@ import java.util.concurrent.{Executors, ExecutorService}
1617
class ContendedInitialization {
1718

1819
@Param(Array("2000000", "5000000"))
19-
var size: Int = _
20+
var size: Int = uninitialized
2021

2122
@Param(Array("2", "4", "8"))
22-
var nThreads: Int = _
23+
var nThreads: Int = uninitialized
2324

24-
var executor: ExecutorService = _
25+
var executor: ExecutorService = uninitialized
2526

2627
@Setup
2728
def prepare: Unit = {

bench-micro/src/main/scala/dotty/tools/benchmarks/lazyvals/InitializedAccess.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools.benchmarks.lazyvals
22

3+
import compiletime.uninitialized
34
import org.openjdk.jmh.annotations._
45
import LazyVals.LazyHolder
56
import org.openjdk.jmh.infra.Blackhole
@@ -14,7 +15,7 @@ import java.util.concurrent.TimeUnit
1415
@State(Scope.Benchmark)
1516
class InitializedAccess {
1617

17-
var holder: LazyHolder = _
18+
var holder: LazyHolder = uninitialized
1819

1920
@Setup
2021
def prepare: Unit = {

bench-micro/src/main/scala/dotty/tools/benchmarks/lazyvals/InitializedAccessAny.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools.benchmarks.lazyvals
22

3+
import compiletime.uninitialized
34
import org.openjdk.jmh.annotations._
45
import LazyVals.LazyAnyHolder
56
import org.openjdk.jmh.infra.Blackhole
@@ -14,7 +15,7 @@ import java.util.concurrent.TimeUnit
1415
@State(Scope.Benchmark)
1516
class InitializedAccessAny {
1617

17-
var holder: LazyAnyHolder = _
18+
var holder: LazyAnyHolder = uninitialized
1819

1920
@Setup
2021
def prepare: Unit = {

bench-micro/src/main/scala/dotty/tools/benchmarks/lazyvals/InitializedAccessGeneric.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools.benchmarks.lazyvals
22

3+
import compiletime.uninitialized
34
import org.openjdk.jmh.annotations._
45
import LazyVals.LazyGenericHolder
56
import org.openjdk.jmh.infra.Blackhole
@@ -14,7 +15,7 @@ import java.util.concurrent.TimeUnit
1415
@State(Scope.Benchmark)
1516
class InitializedAccessGeneric {
1617

17-
var holder: LazyGenericHolder[String] = _
18+
var holder: LazyGenericHolder[String] = uninitialized
1819

1920
@Setup
2021
def prepare: Unit = {

bench-micro/src/main/scala/dotty/tools/benchmarks/lazyvals/InitializedAccessInt.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools.benchmarks.lazyvals
22

3+
import compiletime.uninitialized
34
import org.openjdk.jmh.annotations.*
45
import org.openjdk.jmh.infra.Blackhole
56
import LazyVals.LazyIntHolder
@@ -14,7 +15,7 @@ import java.util.concurrent.TimeUnit
1415
@State(Scope.Benchmark)
1516
class InitializedAccessInt {
1617

17-
var holder: LazyIntHolder = _
18+
var holder: LazyIntHolder = uninitialized
1819

1920
@Setup
2021
def prepare: Unit = {

bench-micro/src/main/scala/dotty/tools/benchmarks/lazyvals/InitializedAccessMultiple.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools.benchmarks.lazyvals
22

3+
import compiletime.uninitialized
34
import org.openjdk.jmh.annotations._
45
import LazyVals.LazyHolder
56
import org.openjdk.jmh.infra.Blackhole
@@ -14,7 +15,7 @@ import java.util.concurrent.TimeUnit
1415
@State(Scope.Benchmark)
1516
class InitializedAccessMultiple {
1617

17-
var holders: Array[LazyHolder] = _
18+
var holders: Array[LazyHolder] = uninitialized
1819

1920
@Setup
2021
def prepare: Unit = {

bench-micro/src/main/scala/dotty/tools/benchmarks/lazyvals/InitializedAccessString.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dotty.tools.benchmarks.lazyvals
22

3+
import compiletime.uninitialized
34
import org.openjdk.jmh.annotations._
45
import LazyVals.LazyStringHolder
56
import org.openjdk.jmh.infra.Blackhole
@@ -14,7 +15,7 @@ import java.util.concurrent.TimeUnit
1415
@State(Scope.Benchmark)
1516
class InitializedAccessString {
1617

17-
var holder: LazyStringHolder = _
18+
var holder: LazyStringHolder = uninitialized
1819

1920
@Setup
2021
def prepare: Unit = {
Lines changed: 28 additions & 0 deletions
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 = ???

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,17 @@ trait TreeInfo[T <: Untyped] { self: Trees.Instance[T] =>
141141
loop(tree, Nil)
142142

143143
/** All term arguments of an application in a single flattened list */
144+
def allTermArguments(tree: Tree): List[Tree] = unsplice(tree) match {
145+
case Apply(fn, args) => allArguments(fn) ::: args
146+
case TypeApply(fn, args) => allArguments(fn)
147+
case Block(_, expr) => allArguments(expr)
148+
case _ => Nil
149+
}
150+
151+
/** All type and term arguments of an application in a single flattened list */
144152
def allArguments(tree: Tree): List[Tree] = unsplice(tree) match {
145153
case Apply(fn, args) => allArguments(fn) ::: args
146-
case TypeApply(fn, _) => allArguments(fn)
154+
case TypeApply(fn, args) => allArguments(fn) ::: args
147155
case Block(_, expr) => allArguments(expr)
148156
case _ => Nil
149157
}

0 commit comments

Comments
 (0)