-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Some fixes for AnnotatedTypes mapping #19957
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
Merged
Changes from all commits
Commits
Show all changes
4 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
71 changes: 71 additions & 0 deletions
71
bench-micro/src/main/scala/dotty/tools/benchmarks/AnnotationsMappingBenchmark.scala
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,71 @@ | ||
package dotty.tools.benchmarks | ||
|
||
import org.openjdk.jmh.annotations.{Benchmark, BenchmarkMode, Fork, Level, Measurement, Mode as JMHMode, Param, Scope, Setup, State, Warmup} | ||
import java.util.concurrent.TimeUnit.SECONDS | ||
|
||
import dotty.tools.dotc.{Driver, Run, Compiler} | ||
import dotty.tools.dotc.ast.{tpd, TreeTypeMap}, tpd.{Apply, Block, Tree, TreeAccumulator, TypeApply} | ||
import dotty.tools.dotc.core.Annotations.{Annotation, ConcreteAnnotation, EmptyAnnotation} | ||
import dotty.tools.dotc.core.Contexts.{ContextBase, Context, ctx, withMode} | ||
import dotty.tools.dotc.core.Mode | ||
import dotty.tools.dotc.core.Phases.Phase | ||
import dotty.tools.dotc.core.Symbols.{defn, mapSymbols, Symbol} | ||
import dotty.tools.dotc.core.Types.{AnnotatedType, NoType, SkolemType, TermRef, Type, TypeMap} | ||
import dotty.tools.dotc.parsing.Parser | ||
import dotty.tools.dotc.typer.TyperPhase | ||
|
||
/** Measures the performance of mapping over annotated types. | ||
* | ||
* Run with: scala3-bench-micro / Jmh / run AnnotationsMappingBenchmark | ||
*/ | ||
@Fork(value = 4) | ||
@Warmup(iterations = 4, time = 1, timeUnit = SECONDS) | ||
@Measurement(iterations = 4, time = 1, timeUnit = SECONDS) | ||
@BenchmarkMode(Array(JMHMode.Throughput)) | ||
@State(Scope.Thread) | ||
class AnnotationsMappingBenchmark: | ||
var tp: Type = null | ||
var specialIntTp: Type = null | ||
var context: Context = null | ||
var typeFunction: Context ?=> Type => Type = null | ||
var typeMap: TypeMap = null | ||
|
||
@Param(Array("v1", "v2", "v3", "v4")) | ||
var valName: String = null | ||
|
||
@Param(Array("id", "mapInts")) | ||
var typeFunctionName: String = null | ||
|
||
@Setup(Level.Iteration) | ||
def setup(): Unit = | ||
val testPhase = | ||
new Phase: | ||
final override def phaseName = "testPhase" | ||
final override def run(using ctx: Context): Unit = | ||
val pkg = ctx.compilationUnit.tpdTree.symbol | ||
tp = pkg.requiredClass("Test").requiredValueRef(valName).underlying | ||
specialIntTp = pkg.requiredClass("Test").requiredType("SpecialInt").typeRef | ||
context = ctx | ||
|
||
val compiler = | ||
new Compiler: | ||
private final val baseCompiler = new Compiler() | ||
final override def phases = List(List(Parser()), List(TyperPhase()), List(testPhase)) | ||
|
||
val driver = | ||
new Driver: | ||
final override def newCompiler(using Context): Compiler = compiler | ||
|
||
driver.process(Array("-classpath", System.getProperty("BENCH_CLASS_PATH"), "tests/someAnnotatedTypes.scala")) | ||
|
||
typeFunction = | ||
typeFunctionName match | ||
case "id" => tp => tp | ||
case "mapInts" => tp => (if tp frozen_=:= defn.IntType then specialIntTp else tp) | ||
case _ => throw new IllegalArgumentException(s"Unknown type function: $typeFunctionName") | ||
|
||
typeMap = | ||
new TypeMap(using context): | ||
final override def apply(tp: Type): Type = typeFunction(mapOver(tp)) | ||
|
||
@Benchmark def applyTypeMap() = typeMap.apply(tp) |
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
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
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
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,28 @@ | ||
class Test: | ||
class FlagAnnot extends annotation.StaticAnnotation | ||
class StringAnnot(val s: String) extends annotation.StaticAnnotation | ||
class LambdaAnnot(val f: Int => Boolean) extends annotation.StaticAnnotation | ||
|
||
type SpecialInt <: Int | ||
|
||
val v1: Int @FlagAnnot = 42 | ||
|
||
val v2: Int @StringAnnot("hello") = 42 | ||
|
||
val v3: Int @LambdaAnnot(it => it == 42) = 42 | ||
|
||
val v4: Int @LambdaAnnot(it => { | ||
def g(x: Int, y: Int) = x - y + 5 | ||
g(it, 7) * 2 == 80 | ||
}) = 42 | ||
|
||
/*val v5: Int @LambdaAnnot(it => { | ||
class Foo(x: Int): | ||
def xPlus10 = x + 10 | ||
def xPlus20 = x + 20 | ||
def xPlus(y: Int) = x + y | ||
val foo = Foo(it) | ||
foo.xPlus10 - foo.xPlus20 + foo.xPlus(30) == 62 | ||
}) = 42*/ | ||
|
||
def main(args: Array[String]): Unit = ??? |
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
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
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
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,10 @@ | ||
import scala.annotation.Annotation | ||
class myRefined(f: ? => Boolean) extends Annotation | ||
|
||
def test(axes: Int) = true | ||
|
||
trait Tensor: | ||
def mean(axes: Int): Int @myRefined(_ => test(axes)) | ||
|
||
class TensorImpl() extends Tensor: | ||
def mean(axes: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//> using options "-Xprint:typer" | ||
|
||
class myAnnot[T]() extends annotation.Annotation | ||
|
||
trait Tensor[T]: | ||
def add: Tensor[T] @myAnnot[T]() | ||
|
||
class TensorImpl[A]() extends Tensor[A]: | ||
def add /* : Tensor[A] @myAnnot[A] */ = this |
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,10 @@ | ||
class Annot[T] extends scala.annotation.Annotation | ||
|
||
class D[T](val f: Int@Annot[T]) | ||
|
||
object A{ | ||
def main(a:Array[String]) = { | ||
val c = new D[Int](1) | ||
c.f | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.