Skip to content

Commit

Permalink
Drop old footprint calculation
Browse files Browse the repository at this point in the history
# Conflicts:
#	compiler/src/dotty/tools/dotc/core/Types.scala

[Cherry-picked d6ba9b2][modified]
  • Loading branch information
WojciechMazur committed Jul 1, 2024
1 parent 698160d commit 633bfe2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 46 deletions.
48 changes: 13 additions & 35 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2879,7 +2879,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
}

protected def explainingTypeComparer(short: Boolean) = ExplainingTypeComparer(comparerContext, short)
protected def trackingTypeComparer = TrackingTypeComparer(comparerContext)
protected def matchReducer = MatchReducer(comparerContext)

private def inSubComparer[T, Cmp <: TypeComparer](comparer: Cmp)(op: Cmp => T): T =
val saved = myInstance
Expand All @@ -2893,8 +2893,8 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
inSubComparer(cmp)(op)
cmp.lastTrace(header)

def tracked[T](op: TrackingTypeComparer => T)(using Context): T =
inSubComparer(trackingTypeComparer)(op)
def reduceMatchWith[T](op: MatchReducer => T)(using Context): T =
inSubComparer(matchReducer)(op)
}

object TypeComparer {
Expand Down Expand Up @@ -3051,11 +3051,11 @@ object TypeComparer {
def explained[T](op: ExplainingTypeComparer => T, header: String = "Subtype trace:", short: Boolean = false)(using Context): String =
comparing(_.explained(op, header, short))

def tracked[T](op: TrackingTypeComparer => T)(using Context): T =
comparing(_.tracked(op))
def reduceMatchWith[T](op: MatchReducer => T)(using Context): T =
comparing(_.reduceMatchWith(op))
}

object TrackingTypeComparer:
object MatchReducer:
import printing.*, Texts.*
enum MatchResult extends Showable:
case Reduced(tp: Type)
Expand All @@ -3069,38 +3069,16 @@ object TrackingTypeComparer:
case Stuck => "Stuck"
case NoInstance(fails) => "NoInstance(" ~ Text(fails.map(p.toText(_) ~ p.toText(_)), ", ") ~ ")"

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

init(initctx)

override def trackingTypeComparer = this

val footprint: mutable.Set[Type] = mutable.Set[Type]()

override def bounds(param: TypeParamRef)(using Context): TypeBounds = {
if (param.binder `ne` caseLambda) footprint += param
super.bounds(param)
}

override def addOneBound(param: TypeParamRef, bound: Type, isUpper: Boolean)(using Context): Boolean = {
if (param.binder `ne` caseLambda) footprint += param
super.addOneBound(param, bound, isUpper)
}

override def gadtBounds(sym: Symbol)(using Context): TypeBounds | Null = {
if (sym.exists) footprint += sym.typeRef
super.gadtBounds(sym)
}

override def gadtAddBound(sym: Symbol, b: Type, isUpper: Boolean): Boolean =
if (sym.exists) footprint += sym.typeRef
super.gadtAddBound(sym, b, isUpper)

override def typeVarInstance(tvar: TypeVar)(using Context): Type = {
footprint += tvar
super.typeVarInstance(tvar)
}
override def matchReducer = this

def matchCases(scrut: Type, cases: List[Type])(using Context): Type = {

Expand Down
16 changes: 5 additions & 11 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4952,17 +4952,11 @@ object Types extends TypeUtils {
tp.underlying
}

def updateReductionContext(footprint: collection.Set[Type]): Unit =
reductionContext = util.HashMap()
for (tp <- footprint)
reductionContext(tp) = contextInfo(tp)
matchTypes.println(i"footprint for $this $hashCode: ${footprint.toList.map(x => (x, contextInfo(x)))}%, %")

def isUpToDate: Boolean =
reductionContext.keysIterator.forall: tp =>
reductionContext(tp) `eq` contextInfo(tp)

def computeFootprint(): Unit =
def setReductionContext(): Unit =
new TypeTraverser:
var footprint: Set[Type] = Set()
var deep: Boolean = true
Expand Down Expand Up @@ -4994,6 +4988,7 @@ object Types extends TypeUtils {
for tp <- footprint do
reductionContext(tp) = contextInfo(tp)
matchTypes.println(i"footprint for $thisMatchType $hashCode: ${footprint.toList.map(x => (x, contextInfo(x)))}%, %")
end setReductionContext

record("MatchType.reduce called")
if !Config.cacheMatchReduced
Expand All @@ -5005,8 +5000,8 @@ object Types extends TypeUtils {
if (myReduced != null) record("MatchType.reduce cache miss")
myReduced =
trace(i"reduce match type $this $hashCode", matchTypes, show = true):
computeFootprint()
def matchCases(cmp: TrackingTypeComparer): Type =
setReductionContext()
def matchCases(cmp: MatchReducer): Type =
val saved = ctx.typerState.snapshot()
try cmp.matchCases(scrutinee.normalized, cases)
catch case ex: Throwable =>
Expand All @@ -5016,8 +5011,7 @@ object Types extends TypeUtils {
ctx.typerState.resetTo(saved)
// this drops caseLambdas in constraint and undoes any typevar
// instantiations during matchtype reduction

TypeComparer.tracked(matchCases)
TypeComparer.reduceMatchWith(matchCases)
//else println(i"no change for $this $hashCode / $myReduced")
myReduced.nn
}
Expand Down

0 comments on commit 633bfe2

Please sign in to comment.