Skip to content

Commit

Permalink
optimize the cachability check
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyxus authored and little-inferno committed Jan 25, 2023
1 parent e87c8c3 commit eacb5f9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/cc/CapturingType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ object CapturingType:
EventuallyCapturingType.unapply(tp)
else None

/** Check whether a type is uncachable when computing `baseType`.
* - Avoid caching all the types during the setup phase, since at that point
* the capture set variables are not fully installed yet.
* - Avoid caching capturing types when IgnoreCaptures mode is set, since the
* capture sets may be thrown away in the computed base type.
*/
def isUncachable(tp: Type)(using Context): Boolean =
ctx.phase == Phases.checkCapturesPhase &&
(Setup.isDuringSetup || ctx.mode.is(Mode.IgnoreCaptures) && tp.isEventuallyCapturingType)

end CapturingType

/** An extractor for types that will be capturing types at phase CheckCaptures. Also
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2140,15 +2140,14 @@ object SymDenotations {
final def baseTypeOf(tp: Type)(using Context): Type = {
val btrCache = baseTypeCache
def inCache(tp: Type) = tp match
case EventuallyCapturingType(_, _) => false
case tp: CachedType => btrCache.contains(tp)
case _ => false
def record(tp: CachedType, baseTp: Type) = {
if (Stats.monitored) {
Stats.record("basetype cache entries")
if (!baseTp.exists) Stats.record("basetype cache NoTypes")
}
if (!tp.isProvisional && !tp.isEventuallyCapturingType && !Setup.isDuringSetup)
if (!tp.isProvisional && !CapturingType.isUncachable(tp))
btrCache(tp) = baseTp
else
btrCache.remove(tp) // Remove any potential sentinel value
Expand Down

0 comments on commit eacb5f9

Please sign in to comment.