Skip to content

Commit

Permalink
Simple performance improvement for Denotations (#21584)
Browse files Browse the repository at this point in the history
A simple performance improvement extracted from #21278. This PR reduces `denot` calls in `widen`s.

This is a simpler approach which can reduce the compilation time of
#20217 from 60s to about 40s.
  • Loading branch information
noti0na1 authored Sep 18, 2024
2 parents 65a53b5 + d9e13e5 commit be10596
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,8 @@ object Types extends TypeUtils {
final def widen(using Context): Type = this match
case _: TypeRef | _: MethodOrPoly => this // fast path for most frequent cases
case tp: TermRef => // fast path for next most frequent case
if tp.isOverloaded then tp else tp.underlying.widen
val denot = tp.denot
if denot.isOverloaded then tp else denot.info.widen
case tp: SingletonType => tp.underlying.widen
case tp: ExprType => tp.resultType.widen
case tp =>
Expand All @@ -1325,15 +1326,20 @@ object Types extends TypeUtils {
* base type by applying one or more `underlying` dereferences.
*/
final def widenSingleton(using Context): Type = stripped match {
case tp: SingletonType if !tp.isOverloaded => tp.underlying.widenSingleton
case tp: TermRef =>
val denot = tp.denot
if denot.isOverloaded then this else denot.info.widenSingleton
case tp: SingletonType => tp.underlying.widenSingleton
case _ => this
}

/** Widen from TermRef to its underlying non-termref
* base type, while also skipping Expr types.
*/
final def widenTermRefExpr(using Context): Type = stripTypeVar match {
case tp: TermRef if !tp.isOverloaded => tp.underlying.widenExpr.widenTermRefExpr
case tp: TermRef =>
val denot = tp.denot
if denot.isOverloaded then this else denot.info.widenExpr.widenTermRefExpr
case _ => this
}

Expand Down

0 comments on commit be10596

Please sign in to comment.