Skip to content

Commit 84ace9e

Browse files
committed
Drop old footprint calculation
1 parent f67c889 commit 84ace9e

File tree

2 files changed

+29
-57
lines changed

2 files changed

+29
-57
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 13 additions & 35 deletions
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

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5019,17 +5019,11 @@ object Types extends TypeUtils {
50195019
tp.underlying
50205020
}
50215021

5022-
def updateReductionContext(footprint: collection.Set[Type]): Unit =
5023-
reductionContext = util.HashMap()
5024-
for (tp <- footprint)
5025-
reductionContext(tp) = contextInfo(tp)
5026-
matchTypes.println(i"footprint for $this $hashCode: ${footprint.toList.map(x => (x, contextInfo(x)))}%, %")
5027-
50285022
def isUpToDate: Boolean =
50295023
reductionContext.keysIterator.forall: tp =>
50305024
reductionContext(tp) `eq` contextInfo(tp)
50315025

5032-
def computeFootprint(): Unit =
5026+
def setReductionContext(): Unit =
50335027
new TypeTraverser:
50345028
var footprint: Set[Type] = Set()
50355029
var deep: Boolean = true
@@ -5061,6 +5055,7 @@ object Types extends TypeUtils {
50615055
for tp <- footprint do
50625056
reductionContext(tp) = contextInfo(tp)
50635057
matchTypes.println(i"footprint for $thisMatchType $hashCode: ${footprint.toList.map(x => (x, contextInfo(x)))}%, %")
5058+
end setReductionContext
50645059

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

0 commit comments

Comments
 (0)