Skip to content

Commit e6c099d

Browse files
authored
Merge pull request #9657 from dotty-staging/optimize-namedPartsWith
Change defaults for NamedPartsAccumulator
2 parents b0c4f33 + dd5eb6f commit e6c099d

File tree

3 files changed

+22
-33
lines changed

3 files changed

+22
-33
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ object TypeOps:
404404

405405
def apply(tp: Type): Type = tp match {
406406
case tp: TermRef
407-
if toAvoid(tp.symbol) || partsToAvoid(mutable.Set.empty, tp.info).nonEmpty =>
407+
if toAvoid(tp.symbol) || partsToAvoid(Nil, tp.info).nonEmpty =>
408408
tp.info.widenExpr.dealias match {
409409
case info: SingletonType => apply(info)
410410
case info => range(defn.NothingType, apply(info))
@@ -422,7 +422,7 @@ object TypeOps:
422422
}
423423
case tp: ThisType if toAvoid(tp.cls) =>
424424
range(defn.NothingType, apply(classBound(tp.cls.classInfo)))
425-
case tp: SkolemType if partsToAvoid(mutable.Set.empty, tp.info).nonEmpty =>
425+
case tp: SkolemType if partsToAvoid(Nil, tp.info).nonEmpty =>
426426
range(defn.NothingType, apply(tp.info))
427427
case tp: TypeVar if mapCtx.typerState.constraint.contains(tp) =>
428428
val lo = TypeComparer.instanceType(

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

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -394,20 +394,13 @@ object Types {
394394
final def foreachPart(p: Type => Unit, stopAtStatic: Boolean = false)(using Context): Unit =
395395
new ForeachAccumulator(p, stopAtStatic).apply((), this)
396396

397-
/** The parts of this type which are type or term refs */
398-
final def namedParts(using Context): collection.Set[NamedType] =
399-
namedPartsWith(alwaysTrue)
400-
401397
/** The parts of this type which are type or term refs and which
402398
* satisfy predicate `p`.
403399
*
404400
* @param p The predicate to satisfy
405-
* @param excludeLowerBounds If set to true, the lower bounds of abstract
406-
* types will be ignored.
407401
*/
408-
def namedPartsWith(p: NamedType => Boolean, excludeLowerBounds: Boolean = false)
409-
(using Context): collection.Set[NamedType] =
410-
new NamedPartsAccumulator(p, excludeLowerBounds).apply(mutable.LinkedHashSet(), this)
402+
def namedPartsWith(p: NamedType => Boolean)(using Context): List[NamedType] =
403+
new NamedPartsAccumulator(p).apply(Nil, this)
411404

412405
/** Map function `f` over elements of an AndType, rebuilding with function `g` */
413406
def mapReduceAnd[T](f: Type => T)(g: (T, T) => T)(using Context): T = stripTypeVar match {
@@ -5538,38 +5531,34 @@ object Types {
55385531
override def hash(x: Type): Int = System.identityHashCode(x)
55395532
override def isEqual(x: Type, y: Type) = x.eq(y)
55405533

5541-
class NamedPartsAccumulator(p: NamedType => Boolean, excludeLowerBounds: Boolean = false)
5542-
(using Context) extends TypeAccumulator[mutable.Set[NamedType]] {
5543-
override def stopAtStatic: Boolean = false
5544-
def maybeAdd(x: mutable.Set[NamedType], tp: NamedType): mutable.Set[NamedType] = if (p(tp)) x += tp else x
5534+
class NamedPartsAccumulator(p: NamedType => Boolean)(using Context)
5535+
extends TypeAccumulator[List[NamedType]]:
5536+
def maybeAdd(xs: List[NamedType], tp: NamedType): List[NamedType] = if p(tp) then tp :: xs else xs
55455537
val seen = TypeHashSet()
5546-
def apply(x: mutable.Set[NamedType], tp: Type): mutable.Set[NamedType] =
5547-
if (seen contains tp) x
5548-
else {
5538+
def apply(xs: List[NamedType], tp: Type): List[NamedType] =
5539+
if seen contains tp then xs
5540+
else
55495541
seen.addEntry(tp)
5550-
tp match {
5542+
tp match
55515543
case tp: TypeRef =>
5552-
foldOver(maybeAdd(x, tp), tp)
5544+
foldOver(maybeAdd(xs, tp), tp)
55535545
case tp: ThisType =>
5554-
apply(x, tp.tref)
5546+
apply(xs, tp.tref)
55555547
case NoPrefix =>
5556-
foldOver(x, tp)
5548+
foldOver(xs, tp)
55575549
case tp: TermRef =>
5558-
apply(foldOver(maybeAdd(x, tp), tp), tp.underlying)
5550+
apply(foldOver(maybeAdd(xs, tp), tp), tp.underlying)
55595551
case tp: AppliedType =>
5560-
foldOver(x, tp)
5552+
foldOver(xs, tp)
55615553
case TypeBounds(lo, hi) =>
5562-
if (!excludeLowerBounds) apply(x, lo)
5563-
apply(x, hi)
5554+
apply(apply(xs, lo), hi)
55645555
case tp: ParamRef =>
5565-
apply(x, tp.underlying)
5556+
apply(xs, tp.underlying)
55665557
case tp: ConstantType =>
5567-
apply(x, tp.underlying)
5558+
apply(xs, tp.underlying)
55685559
case _ =>
5569-
foldOver(x, tp)
5570-
}
5571-
}
5572-
}
5560+
foldOver(xs, tp)
5561+
end NamedPartsAccumulator
55735562

55745563
class isGroundAccumulator(using Context) extends TypeAccumulator[Boolean] {
55755564
def apply(x: Boolean, tp: Type): Boolean = x && {

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ class Typer extends Namer
907907
pt, localSyms(stats1))
908908
}
909909

910-
def escapingRefs(block: Tree, localSyms: => List[Symbol])(using Context): collection.Set[NamedType] = {
910+
def escapingRefs(block: Tree, localSyms: => List[Symbol])(using Context): List[NamedType] = {
911911
lazy val locals = localSyms.toSet
912912
block.tpe.namedPartsWith(tp => locals.contains(tp.symbol) && !tp.isErroneous)
913913
}

0 commit comments

Comments
 (0)