Skip to content

Fix inductive implicits performance regression #6329

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 4 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 46 additions & 23 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4966,34 +4966,57 @@ object Types {
}

class TypeSizeAccumulator(implicit ctx: Context) extends TypeAccumulator[Int] {
def apply(n: Int, tp: Type): Int = tp match {
case tp: AppliedType =>
foldOver(n + 1, tp)
case tp: RefinedType =>
foldOver(n + 1, tp)
case tp: TypeRef if tp.info.isTypeAlias =>
apply(n, tp.superType)
case _ =>
foldOver(n, tp)
val seen: util.HashSet[Type] = new util.HashSet[Type](64) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably faster to use a java.util.IdentityHashMap[Type, Type], We already use that in lots of other places in the compiler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even:

import java.util.{Collections, IdentityHashMap}
val seen = Collections.newSetFromMap[Type](new IdentityHashMap)

override def hash(x: Type): Int = System.identityHashCode(x)
override def isEqual(x: Type, y: Type) = x.eq(y)
}
def apply(n: Int, tp: Type): Int =
if (seen contains tp) n
else {
seen.addEntry(tp)
tp match {
case tp: AppliedType =>
foldOver(n + 1, tp)
case tp: RefinedType =>
foldOver(n + 1, tp)
case tp: TypeRef if tp.info.isTypeAlias =>
apply(n, tp.superType)
case tp: TypeParamRef if !seen(tp) =>
apply(n, ctx.typeComparer.bounds(tp))
case _ =>
foldOver(n, tp)
}
}
}

class CoveringSetAccumulator(implicit ctx: Context) extends TypeAccumulator[Set[Symbol]] {
val seen: util.HashSet[Type] = new util.HashSet[Type](64) {
override def hash(x: Type): Int = System.identityHashCode(x)
override def isEqual(x: Type, y: Type) = x.eq(y)
}
def apply(cs: Set[Symbol], tp: Type): Set[Symbol] = {
val sym = tp.typeSymbol
tp match {
case tp if tp.isTopType || tp.isBottomType =>
cs
case tp: AppliedType =>
foldOver(cs + sym, tp)
case tp: RefinedType =>
foldOver(cs + sym, tp)
case tp: TypeRef if tp.info.isTypeAlias =>
apply(cs, tp.superType)
case tp: TypeBounds =>
foldOver(cs, tp)
case other =>
foldOver(cs + sym, tp)
if (seen contains tp) cs
else {
seen.addEntry(tp)
tp match {
case tp if tp.isTopType || tp.isBottomType =>
cs
case tp: AppliedType =>
foldOver(cs + tp.typeSymbol, tp)
case tp: RefinedType =>
foldOver(cs + tp.typeSymbol, tp)
case tp: TypeRef if tp.info.isTypeAlias =>
apply(cs, tp.superType)
case tp: TypeRef if tp.typeSymbol.isClass =>
foldOver(cs + tp.typeSymbol, tp)
case tp: TermRef =>
val tsym = if (tp.termSymbol.is(Param)) tp.underlying.typeSymbol else tp.termSymbol
foldOver(cs + tsym, tp)
case tp: TypeParamRef =>
apply(cs, ctx.typeComparer.bounds(tp))
case other =>
foldOver(cs, tp)
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1413,9 +1413,10 @@ abstract class SearchHistory { outer =>
if (cand1.ref == cand.ref) {
val wideTp = tp.widenExpr
lazy val wildTp = wildApprox(wideTp)
lazy val tpSize = wideTp.typeSize
if (belowByname && (wildTp <:< wildPt)) false
else if ((wideTp.typeSize < ptSize && wideTp.coveringSet == ptCoveringSet) || (wildTp =:= wildPt)) true
else loop(tl, isByname(tp) || belowByname)
else if (tpSize > ptSize || wideTp.coveringSet != ptCoveringSet) loop(tl, isByname(tp) || belowByname)
else tpSize < ptSize || wildTp =:= wildPt || loop(tl, isByname(tp) || belowByname)
}
else loop(tl, isByname(tp) || belowByname)
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ class CompilationTests extends ParallelTesting {
compileList("duplicate source", List(
"tests/neg-custom-args/toplevel-samesource/S.scala",
"tests/neg-custom-args/toplevel-samesource/nested/S.scala"),
defaultOptions)
defaultOptions) +
compileFile("tests/neg-custom-args/i6300.scala", allowDeepSubtypes)
}.checkExpectedErrors()

@Test def fuzzyAll: Unit = {
Expand Down
8 changes: 8 additions & 0 deletions tests/neg-custom-args/i6300.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Test {
class Foo[T <: Foo[T]]
class Bar extends Foo[Bar]

implicit def i[T <: Foo[T]](implicit t: Foo[Foo[T]]): Foo[T] = ???

implicitly[Foo[Bar]] // error
}