Skip to content

Commit 632922e

Browse files
committed
Optimize TermRefSet
The overwhelming majority of TermRefSets in implicit search has a single prefix per symbol (in the case of typer.scala: 100%). Optimize for this case.
1 parent a5ae258 commit 632922e

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,28 +1657,29 @@ final class SearchRoot extends SearchHistory {
16571657

16581658
/** A set of term references where equality is =:= */
16591659
sealed class TermRefSet(using Context):
1660-
private val elems = new java.util.LinkedHashMap[TermSymbol, List[Type]]
1660+
private val elems = new java.util.LinkedHashMap[TermSymbol, Type | List[Type]]
16611661

16621662
def isEmpty = elems.size == 0
16631663

1664-
def += (ref: TermRef): Unit = {
1664+
def += (ref: TermRef): Unit =
16651665
val pre = ref.prefix
16661666
val sym = ref.symbol.asTerm
1667-
elems.get(sym) match {
1667+
elems.get(sym) match
16681668
case null =>
1669-
elems.put(sym, pre :: Nil)
1670-
case prefixes =>
1671-
if (!prefixes.exists(_ =:= pre))
1672-
elems.put(sym, pre :: prefixes)
1673-
}
1674-
}
1669+
elems.put(sym, pre)
1670+
case prefix: Type =>
1671+
if !(prefix =:= pre) then elems.put(sym, pre :: prefix :: Nil)
1672+
case prefixes: List[Type] =>
1673+
if !prefixes.exists(_ =:= pre) then elems.put(sym, pre :: prefixes)
16751674

16761675
def ++= (that: TermRefSet): Unit =
16771676
that.foreach(+=)
16781677

16791678
def foreach[U](f: TermRef => U): Unit =
1680-
elems.forEach((sym: TermSymbol, prefixes: List[Type]) =>
1681-
prefixes.foreach(pre => f(TermRef(pre, sym))))
1679+
elems.forEach((sym: TermSymbol, prefixes: Type | List[Type]) =>
1680+
prefixes match
1681+
case prefix: Type => f(TermRef(prefix, sym))
1682+
case prefixes: List[Type] => prefixes.foreach(pre => f(TermRef(pre, sym))))
16821683

16831684
// used only for debugging
16841685
def toList: List[TermRef] = {

0 commit comments

Comments
 (0)