Skip to content

Commit aeb7ff6

Browse files
committed
Cache empty type bounds
Cache empty type bounds and wildcard types over empty type bounds in separate variables. This removes some of the load on uniques hashing and avoid creation of temporary objects.
1 parent 757e431 commit aeb7ff6

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,9 @@ object Contexts {
841841
def uniquesSizes: Map[String, (Int, Int, Int)] =
842842
uniqueSets.transform((_, s) => (s.size, s.accesses, s.misses))
843843

844+
var emptyTypeBounds: TypeBounds = null
845+
var emptyWildcardBounds: WildcardType = null
846+
844847
/** Number of findMember calls on stack */
845848
private[core] var findMemberCount: Int = 0
846849

@@ -894,6 +897,8 @@ object Contexts {
894897

895898
def reset(): Unit = {
896899
for ((_, set) <- uniqueSets) set.clear()
900+
emptyTypeBounds = null
901+
emptyWildcardBounds = null
897902
errorTypeMsg.clear()
898903
sources.clear()
899904
sourceNamed.clear()

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,15 @@ object Types {
263263

264264
/** Is this type exactly Nothing (no vars, aliases, refinements etc allowed)? */
265265
def isBottomType(using Context): Boolean = this match {
266-
case tp: TypeRef => tp.symbol eq defn.NothingClass
266+
case tp: TypeRef =>
267+
tp.name == tpnme.Nothing && (tp.symbol eq defn.NothingClass)
267268
case _ => false
268269
}
269270

270271
/** Is this type exactly Any (no vars, aliases, refinements etc allowed)? */
271272
def isTopType(using Context): Boolean = this match {
272-
case tp: TypeRef => tp.symbol eq defn.AnyClass
273+
case tp: TypeRef =>
274+
tp.name == tpnme.Any && (tp.symbol eq defn.AnyClass)
273275
case _ => false
274276
}
275277

@@ -4647,7 +4649,13 @@ object Types {
46474649
object TypeBounds {
46484650
def apply(lo: Type, hi: Type)(using Context): TypeBounds =
46494651
unique(new RealTypeBounds(lo, hi))
4650-
def empty(using Context): TypeBounds = apply(defn.NothingType, defn.AnyType)
4652+
def empty(using Context): TypeBounds =
4653+
val result = ctx.base.emptyTypeBounds
4654+
if result == null then
4655+
ctx.base.emptyTypeBounds = apply(defn.NothingType, defn.AnyType)
4656+
empty
4657+
else
4658+
result
46514659
def emptyPolyKind(using Context): TypeBounds = apply(defn.NothingType, defn.AnyKindType)
46524660
def upper(hi: Type)(using Context): TypeBounds = apply(defn.NothingType, hi)
46534661
def lower(lo: Type)(using Context): TypeBounds = apply(lo, defn.AnyType)
@@ -4790,7 +4798,15 @@ object Types {
47904798
final class CachedWildcardType(optBounds: Type) extends WildcardType(optBounds)
47914799

47924800
@sharable object WildcardType extends WildcardType(NoType) {
4793-
def apply(bounds: TypeBounds)(using Context): WildcardType = unique(new CachedWildcardType(bounds))
4801+
def apply(bounds: TypeBounds)(using Context): WildcardType =
4802+
if bounds eq TypeBounds.empty then
4803+
val result = ctx.base.emptyWildcardBounds
4804+
if result == null then
4805+
ctx.base.emptyWildcardBounds = unique(CachedWildcardType(bounds))
4806+
apply(bounds)
4807+
else
4808+
result
4809+
else unique(CachedWildcardType(bounds))
47944810
}
47954811

47964812
/** An extractor for single abstract method types.

0 commit comments

Comments
 (0)