From 7fe7954174f54cae10bba28026042d33c6706565 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 17 Apr 2019 19:02:33 +0200 Subject: [PATCH 1/3] Optimize interpolateTypeVars There seemed to have been a large performance regression due to its recent changes. Let's find out whether that's really the case. --- .../dotty/tools/dotc/typer/Inferencing.scala | 103 +++++++++--------- .../tools/dotc/util/SimpleIdentitySet.scala | 29 +++-- 2 files changed, 75 insertions(+), 57 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Inferencing.scala b/compiler/src/dotty/tools/dotc/typer/Inferencing.scala index eea23ca62c33..2c25272349ec 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inferencing.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inferencing.scala @@ -410,57 +410,60 @@ trait Inferencing { this: Typer => // anymore if they've been garbage-collected, so we can't use // `state.ownedVars.size > locked.size` as an early check to avoid computing // `qualifying`. - val qualifying = state.ownedVars -- locked - - if (!qualifying.isEmpty) { - typr.println(i"interpolate $tree: ${tree.tpe.widen} in $state, owned vars = ${state.ownedVars.toList}%, %, previous = ${locked.toList}%, % / ${state.constraint}") - val resultAlreadyConstrained = - tree.isInstanceOf[Apply] || tree.tpe.isInstanceOf[MethodOrPoly] - if (!resultAlreadyConstrained) - constrainResult(tree.symbol, tree.tpe, pt) - // This is needed because it could establish singleton type upper bounds. See i2998.scala. - - val tp = tree.tpe.widen - val vs = variances(tp) - - // Avoid interpolating variables occurring in tree's type if typerstate has unreported errors. - // Reason: The errors might reflect unsatisfiable constraints. In that - // case interpolating without taking account the constraints risks producing - // nonsensical types that then in turn produce incomprehensible errors. - // An example is in neg/i1240.scala. Without the condition in the next code line - // we get for - // - // val y: List[List[String]] = List(List(1)) - // - // i1430.scala:5: error: type mismatch: - // found : Int(1) - // required: Nothing - // val y: List[List[String]] = List(List(1)) - // ^ - // With the condition, we get the much more sensical: - // - // i1430.scala:5: error: type mismatch: - // found : Int(1) - // required: String - // val y: List[List[String]] = List(List(1)) - val hasUnreportedErrors = state.reporter.hasUnreportedErrors - def constraint = state.constraint - for (tvar <- qualifying) - if (!tvar.isInstantiated && state.constraint.contains(tvar)) { - // Needs to be checked again, since previous interpolations could already have - // instantiated `tvar` through unification. - val v = vs(tvar) - if (v == null) { - typr.println(i"interpolate non-occurring $tvar in $state in $tree: $tp, fromBelow = ${tvar.hasLowerBound}, $constraint") - tvar.instantiate(fromBelow = tvar.hasLowerBound) - } - else if (!hasUnreportedErrors) - if (v.intValue != 0) { - typr.println(i"interpolate $tvar in $state in $tree: $tp, fromBelow = ${v.intValue == 1}, $constraint") - tvar.instantiate(fromBelow = v.intValue == 1) + + val ownedVars = state.ownedVars + if (ownedVars.size > 0) { + val qualifying = ownedVars -- locked + if (!qualifying.isEmpty) { + typr.println(i"interpolate $tree: ${tree.tpe.widen} in $state, owned vars = ${state.ownedVars.toList}%, %, previous = ${locked.toList}%, % / ${state.constraint}") + val resultAlreadyConstrained = + tree.isInstanceOf[Apply] || tree.tpe.isInstanceOf[MethodOrPoly] + if (!resultAlreadyConstrained) + constrainResult(tree.symbol, tree.tpe, pt) + // This is needed because it could establish singleton type upper bounds. See i2998.scala. + + val tp = tree.tpe.widen + val vs = variances(tp) + + // Avoid interpolating variables occurring in tree's type if typerstate has unreported errors. + // Reason: The errors might reflect unsatisfiable constraints. In that + // case interpolating without taking account the constraints risks producing + // nonsensical types that then in turn produce incomprehensible errors. + // An example is in neg/i1240.scala. Without the condition in the next code line + // we get for + // + // val y: List[List[String]] = List(List(1)) + // + // i1430.scala:5: error: type mismatch: + // found : Int(1) + // required: Nothing + // val y: List[List[String]] = List(List(1)) + // ^ + // With the condition, we get the much more sensical: + // + // i1430.scala:5: error: type mismatch: + // found : Int(1) + // required: String + // val y: List[List[String]] = List(List(1)) + val hasUnreportedErrors = state.reporter.hasUnreportedErrors + def constraint = state.constraint + for (tvar <- qualifying) + if (!tvar.isInstantiated && state.constraint.contains(tvar)) { + // Needs to be checked again, since previous interpolations could already have + // instantiated `tvar` through unification. + val v = vs(tvar) + if (v == null) { + typr.println(i"interpolate non-occurring $tvar in $state in $tree: $tp, fromBelow = ${tvar.hasLowerBound}, $constraint") + tvar.instantiate(fromBelow = tvar.hasLowerBound) } - else typr.println(i"no interpolation for nonvariant $tvar in $state") - } + else if (!hasUnreportedErrors) + if (v.intValue != 0) { + typr.println(i"interpolate $tvar in $state in $tree: $tp, fromBelow = ${v.intValue == 1}, $constraint") + tvar.instantiate(fromBelow = v.intValue == 1) + } + else typr.println(i"no interpolation for nonvariant $tvar in $state") + } + } } tree } diff --git a/compiler/src/dotty/tools/dotc/util/SimpleIdentitySet.scala b/compiler/src/dotty/tools/dotc/util/SimpleIdentitySet.scala index 2161d597e150..60a772450ea3 100644 --- a/compiler/src/dotty/tools/dotc/util/SimpleIdentitySet.scala +++ b/compiler/src/dotty/tools/dotc/util/SimpleIdentitySet.scala @@ -12,15 +12,12 @@ abstract class SimpleIdentitySet[+Elem <: AnyRef] { def - [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[Elem] def contains[E >: Elem <: AnyRef](x: E): Boolean def foreach(f: Elem => Unit): Unit - def toList: List[Elem] = { - val buf = new ListBuffer[Elem] - foreach(buf += _) - buf.toList - } + def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A + def toList: List[Elem] def ++ [E >: Elem <: AnyRef](that: SimpleIdentitySet[E]): SimpleIdentitySet[E] = - ((this: SimpleIdentitySet[E]) /: that.toList)(_ + _) + ((this: SimpleIdentitySet[E]) /: that)(_ + _) def -- [E >: Elem <: AnyRef](that: SimpleIdentitySet[E]): SimpleIdentitySet[Elem] = - (this /: that.toList)(_ - _) + (this /: that)(_ - _) override def toString: String = toList.mkString("(", ", ", ")") } @@ -33,6 +30,8 @@ object SimpleIdentitySet { this def contains[E <: AnyRef](x: E): Boolean = false def foreach(f: Nothing => Unit): Unit = () + def /: [A, E <: AnyRef](z: A)(f: (A, E) => A): A = z + def toList = Nil } private class Set1[+Elem <: AnyRef](x0: AnyRef) extends SimpleIdentitySet[Elem] { @@ -43,6 +42,9 @@ object SimpleIdentitySet { if (x `eq` x0) empty else this def contains[E >: Elem <: AnyRef](x: E): Boolean = x `eq` x0 def foreach(f: Elem => Unit): Unit = f(x0.asInstanceOf[Elem]) + def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A = + f(z, x0.asInstanceOf[E]) + def toList = x0.asInstanceOf[Elem] :: Nil } private class Set2[+Elem <: AnyRef](x0: AnyRef, x1: AnyRef) extends SimpleIdentitySet[Elem] { @@ -55,6 +57,9 @@ object SimpleIdentitySet { else this def contains[E >: Elem <: AnyRef](x: E): Boolean = (x `eq` x0) || (x `eq` x1) def foreach(f: Elem => Unit): Unit = { f(x0.asInstanceOf[Elem]); f(x1.asInstanceOf[Elem]) } + def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A = + f(f(z, x0.asInstanceOf[E]), x1.asInstanceOf[E]) + def toList = x0.asInstanceOf[Elem] :: x1.asInstanceOf[Elem] :: Nil } private class Set3[+Elem <: AnyRef](x0: AnyRef, x1: AnyRef, x2: AnyRef) extends SimpleIdentitySet[Elem] { @@ -78,6 +83,9 @@ object SimpleIdentitySet { def foreach(f: Elem => Unit): Unit = { f(x0.asInstanceOf[Elem]); f(x1.asInstanceOf[Elem]); f(x2.asInstanceOf[Elem]) } + def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A = + f(f(f(z, x0.asInstanceOf[E]), x1.asInstanceOf[E]), x2.asInstanceOf[E]) + def toList = x0.asInstanceOf[Elem] :: x1.asInstanceOf[Elem] :: x2.asInstanceOf[Elem] :: Nil } private class SetN[+Elem <: AnyRef](xs: Array[AnyRef]) extends SimpleIdentitySet[Elem] { @@ -115,5 +123,12 @@ object SimpleIdentitySet { var i = 0 while (i < size) { f(xs(i).asInstanceOf[Elem]); i += 1 } } + def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A = + (z /: xs.asInstanceOf[Array[E]])(f) + def toList: List[Elem] = { + val buf = new ListBuffer[Elem] + foreach(buf += _) + buf.toList + } } } From 35e631833752837a72d758c44a2238a3cca4d515 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 17 Apr 2019 19:05:34 +0200 Subject: [PATCH 2/3] Another optimization --- compiler/src/dotty/tools/dotc/typer/Inferencing.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Inferencing.scala b/compiler/src/dotty/tools/dotc/typer/Inferencing.scala index 2c25272349ec..8507fcd19c6d 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inferencing.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inferencing.scala @@ -413,7 +413,7 @@ trait Inferencing { this: Typer => val ownedVars = state.ownedVars if (ownedVars.size > 0) { - val qualifying = ownedVars -- locked + val qualifying = if (locked.isEmpty) ownedVars else ownedVars -- locked if (!qualifying.isEmpty) { typr.println(i"interpolate $tree: ${tree.tpe.widen} in $state, owned vars = ${state.ownedVars.toList}%, %, previous = ${locked.toList}%, % / ${state.constraint}") val resultAlreadyConstrained = From e1a9c2fc7db7e198d4ae7ce23a9ef45d8959f89c Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 17 Apr 2019 23:16:03 +0200 Subject: [PATCH 3/3] Test Only: Revert interpolateTypeVars fix. Just to see whether this indeed brings performance back to the previous levels. --- compiler/src/dotty/tools/dotc/typer/Inferencing.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Inferencing.scala b/compiler/src/dotty/tools/dotc/typer/Inferencing.scala index 8507fcd19c6d..bbf50daac8e7 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inferencing.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inferencing.scala @@ -412,7 +412,7 @@ trait Inferencing { this: Typer => // `qualifying`. val ownedVars = state.ownedVars - if (ownedVars.size > 0) { + if (ownedVars.size > locked.size) { val qualifying = if (locked.isEmpty) ownedVars else ownedVars -- locked if (!qualifying.isEmpty) { typr.println(i"interpolate $tree: ${tree.tpe.widen} in $state, owned vars = ${state.ownedVars.toList}%, %, previous = ${locked.toList}%, % / ${state.constraint}")