Skip to content

Commit 7fe7954

Browse files
committed
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.
1 parent 874a203 commit 7fe7954

File tree

2 files changed

+75
-57
lines changed

2 files changed

+75
-57
lines changed

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -410,57 +410,60 @@ trait Inferencing { this: Typer =>
410410
// anymore if they've been garbage-collected, so we can't use
411411
// `state.ownedVars.size > locked.size` as an early check to avoid computing
412412
// `qualifying`.
413-
val qualifying = state.ownedVars -- locked
414-
415-
if (!qualifying.isEmpty) {
416-
typr.println(i"interpolate $tree: ${tree.tpe.widen} in $state, owned vars = ${state.ownedVars.toList}%, %, previous = ${locked.toList}%, % / ${state.constraint}")
417-
val resultAlreadyConstrained =
418-
tree.isInstanceOf[Apply] || tree.tpe.isInstanceOf[MethodOrPoly]
419-
if (!resultAlreadyConstrained)
420-
constrainResult(tree.symbol, tree.tpe, pt)
421-
// This is needed because it could establish singleton type upper bounds. See i2998.scala.
422-
423-
val tp = tree.tpe.widen
424-
val vs = variances(tp)
425-
426-
// Avoid interpolating variables occurring in tree's type if typerstate has unreported errors.
427-
// Reason: The errors might reflect unsatisfiable constraints. In that
428-
// case interpolating without taking account the constraints risks producing
429-
// nonsensical types that then in turn produce incomprehensible errors.
430-
// An example is in neg/i1240.scala. Without the condition in the next code line
431-
// we get for
432-
//
433-
// val y: List[List[String]] = List(List(1))
434-
//
435-
// i1430.scala:5: error: type mismatch:
436-
// found : Int(1)
437-
// required: Nothing
438-
// val y: List[List[String]] = List(List(1))
439-
// ^
440-
// With the condition, we get the much more sensical:
441-
//
442-
// i1430.scala:5: error: type mismatch:
443-
// found : Int(1)
444-
// required: String
445-
// val y: List[List[String]] = List(List(1))
446-
val hasUnreportedErrors = state.reporter.hasUnreportedErrors
447-
def constraint = state.constraint
448-
for (tvar <- qualifying)
449-
if (!tvar.isInstantiated && state.constraint.contains(tvar)) {
450-
// Needs to be checked again, since previous interpolations could already have
451-
// instantiated `tvar` through unification.
452-
val v = vs(tvar)
453-
if (v == null) {
454-
typr.println(i"interpolate non-occurring $tvar in $state in $tree: $tp, fromBelow = ${tvar.hasLowerBound}, $constraint")
455-
tvar.instantiate(fromBelow = tvar.hasLowerBound)
456-
}
457-
else if (!hasUnreportedErrors)
458-
if (v.intValue != 0) {
459-
typr.println(i"interpolate $tvar in $state in $tree: $tp, fromBelow = ${v.intValue == 1}, $constraint")
460-
tvar.instantiate(fromBelow = v.intValue == 1)
413+
414+
val ownedVars = state.ownedVars
415+
if (ownedVars.size > 0) {
416+
val qualifying = ownedVars -- locked
417+
if (!qualifying.isEmpty) {
418+
typr.println(i"interpolate $tree: ${tree.tpe.widen} in $state, owned vars = ${state.ownedVars.toList}%, %, previous = ${locked.toList}%, % / ${state.constraint}")
419+
val resultAlreadyConstrained =
420+
tree.isInstanceOf[Apply] || tree.tpe.isInstanceOf[MethodOrPoly]
421+
if (!resultAlreadyConstrained)
422+
constrainResult(tree.symbol, tree.tpe, pt)
423+
// This is needed because it could establish singleton type upper bounds. See i2998.scala.
424+
425+
val tp = tree.tpe.widen
426+
val vs = variances(tp)
427+
428+
// Avoid interpolating variables occurring in tree's type if typerstate has unreported errors.
429+
// Reason: The errors might reflect unsatisfiable constraints. In that
430+
// case interpolating without taking account the constraints risks producing
431+
// nonsensical types that then in turn produce incomprehensible errors.
432+
// An example is in neg/i1240.scala. Without the condition in the next code line
433+
// we get for
434+
//
435+
// val y: List[List[String]] = List(List(1))
436+
//
437+
// i1430.scala:5: error: type mismatch:
438+
// found : Int(1)
439+
// required: Nothing
440+
// val y: List[List[String]] = List(List(1))
441+
// ^
442+
// With the condition, we get the much more sensical:
443+
//
444+
// i1430.scala:5: error: type mismatch:
445+
// found : Int(1)
446+
// required: String
447+
// val y: List[List[String]] = List(List(1))
448+
val hasUnreportedErrors = state.reporter.hasUnreportedErrors
449+
def constraint = state.constraint
450+
for (tvar <- qualifying)
451+
if (!tvar.isInstantiated && state.constraint.contains(tvar)) {
452+
// Needs to be checked again, since previous interpolations could already have
453+
// instantiated `tvar` through unification.
454+
val v = vs(tvar)
455+
if (v == null) {
456+
typr.println(i"interpolate non-occurring $tvar in $state in $tree: $tp, fromBelow = ${tvar.hasLowerBound}, $constraint")
457+
tvar.instantiate(fromBelow = tvar.hasLowerBound)
461458
}
462-
else typr.println(i"no interpolation for nonvariant $tvar in $state")
463-
}
459+
else if (!hasUnreportedErrors)
460+
if (v.intValue != 0) {
461+
typr.println(i"interpolate $tvar in $state in $tree: $tp, fromBelow = ${v.intValue == 1}, $constraint")
462+
tvar.instantiate(fromBelow = v.intValue == 1)
463+
}
464+
else typr.println(i"no interpolation for nonvariant $tvar in $state")
465+
}
466+
}
464467
}
465468
tree
466469
}

compiler/src/dotty/tools/dotc/util/SimpleIdentitySet.scala

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ abstract class SimpleIdentitySet[+Elem <: AnyRef] {
1212
def - [E >: Elem <: AnyRef](x: E): SimpleIdentitySet[Elem]
1313
def contains[E >: Elem <: AnyRef](x: E): Boolean
1414
def foreach(f: Elem => Unit): Unit
15-
def toList: List[Elem] = {
16-
val buf = new ListBuffer[Elem]
17-
foreach(buf += _)
18-
buf.toList
19-
}
15+
def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A
16+
def toList: List[Elem]
2017
def ++ [E >: Elem <: AnyRef](that: SimpleIdentitySet[E]): SimpleIdentitySet[E] =
21-
((this: SimpleIdentitySet[E]) /: that.toList)(_ + _)
18+
((this: SimpleIdentitySet[E]) /: that)(_ + _)
2219
def -- [E >: Elem <: AnyRef](that: SimpleIdentitySet[E]): SimpleIdentitySet[Elem] =
23-
(this /: that.toList)(_ - _)
20+
(this /: that)(_ - _)
2421
override def toString: String = toList.mkString("(", ", ", ")")
2522
}
2623

@@ -33,6 +30,8 @@ object SimpleIdentitySet {
3330
this
3431
def contains[E <: AnyRef](x: E): Boolean = false
3532
def foreach(f: Nothing => Unit): Unit = ()
33+
def /: [A, E <: AnyRef](z: A)(f: (A, E) => A): A = z
34+
def toList = Nil
3635
}
3736

3837
private class Set1[+Elem <: AnyRef](x0: AnyRef) extends SimpleIdentitySet[Elem] {
@@ -43,6 +42,9 @@ object SimpleIdentitySet {
4342
if (x `eq` x0) empty else this
4443
def contains[E >: Elem <: AnyRef](x: E): Boolean = x `eq` x0
4544
def foreach(f: Elem => Unit): Unit = f(x0.asInstanceOf[Elem])
45+
def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A =
46+
f(z, x0.asInstanceOf[E])
47+
def toList = x0.asInstanceOf[Elem] :: Nil
4648
}
4749

4850
private class Set2[+Elem <: AnyRef](x0: AnyRef, x1: AnyRef) extends SimpleIdentitySet[Elem] {
@@ -55,6 +57,9 @@ object SimpleIdentitySet {
5557
else this
5658
def contains[E >: Elem <: AnyRef](x: E): Boolean = (x `eq` x0) || (x `eq` x1)
5759
def foreach(f: Elem => Unit): Unit = { f(x0.asInstanceOf[Elem]); f(x1.asInstanceOf[Elem]) }
60+
def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A =
61+
f(f(z, x0.asInstanceOf[E]), x1.asInstanceOf[E])
62+
def toList = x0.asInstanceOf[Elem] :: x1.asInstanceOf[Elem] :: Nil
5863
}
5964

6065
private class Set3[+Elem <: AnyRef](x0: AnyRef, x1: AnyRef, x2: AnyRef) extends SimpleIdentitySet[Elem] {
@@ -78,6 +83,9 @@ object SimpleIdentitySet {
7883
def foreach(f: Elem => Unit): Unit = {
7984
f(x0.asInstanceOf[Elem]); f(x1.asInstanceOf[Elem]); f(x2.asInstanceOf[Elem])
8085
}
86+
def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A =
87+
f(f(f(z, x0.asInstanceOf[E]), x1.asInstanceOf[E]), x2.asInstanceOf[E])
88+
def toList = x0.asInstanceOf[Elem] :: x1.asInstanceOf[Elem] :: x2.asInstanceOf[Elem] :: Nil
8189
}
8290

8391
private class SetN[+Elem <: AnyRef](xs: Array[AnyRef]) extends SimpleIdentitySet[Elem] {
@@ -115,5 +123,12 @@ object SimpleIdentitySet {
115123
var i = 0
116124
while (i < size) { f(xs(i).asInstanceOf[Elem]); i += 1 }
117125
}
126+
def /: [A, E >: Elem <: AnyRef](z: A)(f: (A, E) => A): A =
127+
(z /: xs.asInstanceOf[Array[E]])(f)
128+
def toList: List[Elem] = {
129+
val buf = new ListBuffer[Elem]
130+
foreach(buf += _)
131+
buf.toList
132+
}
118133
}
119134
}

0 commit comments

Comments
 (0)