Skip to content

Commit d6ba9b2

Browse files
committed
Drop old footprint calculation
# Conflicts: # compiler/src/dotty/tools/dotc/core/Types.scala
1 parent 5036105 commit d6ba9b2

File tree

2 files changed

+30
-57
lines changed

2 files changed

+30
-57
lines changed

Diff for: compiler/src/dotty/tools/dotc/core/TypeComparer.scala

+13-35
Original file line numberDiff line numberDiff line change
@@ -3054,7 +3054,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
30543054
end provablyDisjointTypeArgs
30553055

30563056
protected def explainingTypeComparer(short: Boolean) = ExplainingTypeComparer(comparerContext, short)
3057-
protected def trackingTypeComparer = TrackingTypeComparer(comparerContext)
3057+
protected def matchReducer = MatchReducer(comparerContext)
30583058

30593059
private def inSubComparer[T, Cmp <: TypeComparer](comparer: Cmp)(op: Cmp => T): T =
30603060
val saved = myInstance
@@ -3068,8 +3068,8 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
30683068
inSubComparer(cmp)(op)
30693069
cmp.lastTrace(header)
30703070

3071-
def tracked[T](op: TrackingTypeComparer => T)(using Context): T =
3072-
inSubComparer(trackingTypeComparer)(op)
3071+
def reduceMatchWith[T](op: MatchReducer => T)(using Context): T =
3072+
inSubComparer(matchReducer)(op)
30733073
}
30743074

30753075
object TypeComparer {
@@ -3236,14 +3236,14 @@ object TypeComparer {
32363236
def explained[T](op: ExplainingTypeComparer => T, header: String = "Subtype trace:", short: Boolean = false)(using Context): String =
32373237
comparing(_.explained(op, header, short))
32383238

3239-
def tracked[T](op: TrackingTypeComparer => T)(using Context): T =
3240-
comparing(_.tracked(op))
3239+
def reduceMatchWith[T](op: MatchReducer => T)(using Context): T =
3240+
comparing(_.reduceMatchWith(op))
32413241

32423242
def subCaptures(refs1: CaptureSet, refs2: CaptureSet, frozen: Boolean)(using Context): CaptureSet.CompareResult =
32433243
comparing(_.subCaptures(refs1, refs2, frozen))
32443244
}
32453245

3246-
object TrackingTypeComparer:
3246+
object MatchReducer:
32473247
import printing.*, Texts.*
32483248
enum MatchResult extends Showable:
32493249
case Reduced(tp: Type)
@@ -3259,38 +3259,16 @@ object TrackingTypeComparer:
32593259
case Stuck => "Stuck"
32603260
case NoInstance(fails) => "NoInstance(" ~ Text(fails.map(p.toText(_) ~ p.toText(_)), ", ") ~ ")"
32613261

3262-
class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
3263-
import TrackingTypeComparer.*
3262+
/** A type comparer for reducing match types.
3263+
* TODO: Not sure this needs to be a type comparer. Can we make it a
3264+
* separate class?
3265+
*/
3266+
class MatchReducer(initctx: Context) extends TypeComparer(initctx) {
3267+
import MatchReducer.*
32643268

32653269
init(initctx)
32663270

3267-
override def trackingTypeComparer = this
3268-
3269-
val footprint: mutable.Set[Type] = mutable.Set[Type]()
3270-
3271-
override def bounds(param: TypeParamRef)(using Context): TypeBounds = {
3272-
if (param.binder `ne` caseLambda) footprint += param
3273-
super.bounds(param)
3274-
}
3275-
3276-
override def addOneBound(param: TypeParamRef, bound: Type, isUpper: Boolean)(using Context): Boolean = {
3277-
if (param.binder `ne` caseLambda) footprint += param
3278-
super.addOneBound(param, bound, isUpper)
3279-
}
3280-
3281-
override def gadtBounds(sym: Symbol)(using Context): TypeBounds | Null = {
3282-
if (sym.exists) footprint += sym.typeRef
3283-
super.gadtBounds(sym)
3284-
}
3285-
3286-
override def gadtAddBound(sym: Symbol, b: Type, isUpper: Boolean): Boolean =
3287-
if (sym.exists) footprint += sym.typeRef
3288-
super.gadtAddBound(sym, b, isUpper)
3289-
3290-
override def typeVarInstance(tvar: TypeVar)(using Context): Type = {
3291-
footprint += tvar
3292-
super.typeVarInstance(tvar)
3293-
}
3271+
override def matchReducer = this
32943272

32953273
def matchCases(scrut: Type, cases: List[MatchTypeCaseSpec])(using Context): Type = {
32963274
// a reference for the type parameters poisoned during matching

Diff for: compiler/src/dotty/tools/dotc/core/Types.scala

+17-22
Original file line numberDiff line numberDiff line change
@@ -5025,17 +5025,11 @@ object Types extends TypeUtils {
50255025
tp.underlying
50265026
}
50275027

5028-
def updateReductionContext(footprint: collection.Set[Type]): Unit =
5029-
reductionContext = util.HashMap()
5030-
for (tp <- footprint)
5031-
reductionContext(tp) = contextInfo(tp)
5032-
matchTypes.println(i"footprint for $this $hashCode: ${footprint.toList.map(x => (x, contextInfo(x)))}%, %")
5033-
50345028
def isUpToDate: Boolean =
50355029
reductionContext.keysIterator.forall: tp =>
50365030
reductionContext(tp) `eq` contextInfo(tp)
50375031

5038-
def computeFootprint(): Unit =
5032+
def setReductionContext(): Unit =
50395033
new TypeTraverser:
50405034
var footprint: Set[Type] = Set()
50415035
var deep: Boolean = true
@@ -5067,6 +5061,7 @@ object Types extends TypeUtils {
50675061
for tp <- footprint do
50685062
reductionContext(tp) = contextInfo(tp)
50695063
matchTypes.println(i"footprint for $thisMatchType $hashCode: ${footprint.toList.map(x => (x, contextInfo(x)))}%, %")
5064+
end setReductionContext
50705065

50715066
record("MatchType.reduce called")
50725067
if !Config.cacheMatchReduced
@@ -5077,21 +5072,21 @@ object Types extends TypeUtils {
50775072
record("MatchType.reduce computed")
50785073
if (myReduced != null) record("MatchType.reduce cache miss")
50795074
myReduced =
5080-
trace(i"reduce match type $this $hashCode", matchTypes, show = true)(withMode(Mode.Type) {
5081-
computeFootprint()
5082-
def matchCases(cmp: TrackingTypeComparer): Type =
5083-
val saved = ctx.typerState.snapshot()
5084-
try cmp.matchCases(scrutinee.normalized, cases.map(MatchTypeCaseSpec.analyze(_)))
5085-
catch case ex: Throwable =>
5086-
handleRecursive("reduce type ", i"$scrutinee match ...", ex)
5087-
finally
5088-
//updateReductionContext(cmp.footprint)
5089-
ctx.typerState.resetTo(saved)
5090-
// this drops caseLambdas in constraint and undoes any typevar
5091-
// instantiations during matchtype reduction
5092-
5093-
TypeComparer.tracked(matchCases)
5094-
})
5075+
trace(i"reduce match type $this $hashCode", matchTypes, show = true):
5076+
withMode(Mode.Type):
5077+
setReductionContext()
5078+
def matchCases(cmp: MatchReducer): Type =
5079+
val saved = ctx.typerState.snapshot()
5080+
try
5081+
cmp.matchCases(scrutinee.normalized, cases.map(MatchTypeCaseSpec.analyze(_)))
5082+
catch case ex: Throwable =>
5083+
handleRecursive("reduce type ", i"$scrutinee match ...", ex)
5084+
finally
5085+
ctx.typerState.resetTo(saved)
5086+
// this drops caseLambdas in constraint and undoes any typevar
5087+
// instantiations during matchtype reduction
5088+
TypeComparer.reduceMatchWith(matchCases)
5089+
50955090
//else println(i"no change for $this $hashCode / $myReduced")
50965091
myReduced.nn
50975092
}

0 commit comments

Comments
 (0)