Skip to content

Commit 9dfdeec

Browse files
committed
Specialize hash-consing of WithFixedSym types
1 parent df1c3c2 commit 9dfdeec

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,13 @@ object Contexts {
609609
/** A table for hash consing unique named types */
610610
private[core] val uniqueNamedTypes = new NamedTypeUniques
611611

612+
/** A table for hash consing unique symbolic named types */
613+
private[core] val uniqueWithFixedSyms = new WithFixedSymUniques
614+
612615
private def uniqueSets = Map(
613616
"uniques" -> uniques,
614617
"uniqueAppliedTypes" -> uniqueAppliedTypes,
618+
"uniqueWithFixedSyms" -> uniqueWithFixedSyms,
615619
"uniqueNamedTypes" -> uniqueNamedTypes)
616620

617621
/** A map that associates label and size of all uniques sets */

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,7 +2020,7 @@ object Types {
20202020
case that: WithFixedSym => this.prefix == that.prefix && (this.fixedSym eq that.fixedSym)
20212021
case _ => false
20222022
}
2023-
override def computeHash = doHash(fixedSym, prefix)
2023+
override def computeHash = unsupported("computeHash")
20242024
}
20252025

20262026
final class CachedTermRef(prefix: Type, name: TermName, hc: Int) extends TermRef(prefix, name) {
@@ -2036,8 +2036,12 @@ object Types {
20362036
}
20372037

20382038
// Those classes are non final as Linker extends them.
2039-
class TermRefWithFixedSym(prefix: Type, name: TermName, val fixedSym: TermSymbol) extends TermRef(prefix, name) with WithFixedSym
2040-
class TypeRefWithFixedSym(prefix: Type, name: TypeName, val fixedSym: TypeSymbol) extends TypeRef(prefix, name) with WithFixedSym
2039+
class TermRefWithFixedSym(prefix: Type, name: TermName, val fixedSym: TermSymbol, hc: Int) extends TermRef(prefix, name) with WithFixedSym {
2040+
myHash = hc
2041+
}
2042+
class TypeRefWithFixedSym(prefix: Type, name: TypeName, val fixedSym: TypeSymbol, hc: Int) extends TypeRef(prefix, name) with WithFixedSym {
2043+
myHash = hc
2044+
}
20412045

20422046
/** Assert current phase does not have erasure semantics */
20432047
private def assertUnerased()(implicit ctx: Context) =
@@ -2094,7 +2098,7 @@ object Types {
20942098
* with given prefix, name, and signature
20952099
*/
20962100
def withFixedSym(prefix: Type, name: TermName, sym: TermSymbol)(implicit ctx: Context): TermRef =
2097-
unique(new TermRefWithFixedSym(prefix, name, sym))
2101+
ctx.uniqueWithFixedSyms.enterIfNew(prefix, name, sym).asInstanceOf[TermRef]
20982102

20992103
/** Create a term ref referring to given symbol with given name, taking the signature
21002104
* from the symbol if it is completed, or creating a term ref without
@@ -2148,7 +2152,7 @@ object Types {
21482152
* with given prefix, name, and symbol.
21492153
*/
21502154
def withFixedSym(prefix: Type, name: TypeName, sym: TypeSymbol)(implicit ctx: Context): TypeRef =
2151-
unique(new TypeRefWithFixedSym(prefix, name, sym))
2155+
ctx.uniqueWithFixedSyms.enterIfNew(prefix, name, sym).asInstanceOf[TypeRef]
21522156

21532157
/** Create a type ref referring to given symbol with given name.
21542158
* This is very similar to TypeRef(Type, Symbol),

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dotty.tools.dotc
22
package core
33

4-
import Types._, Contexts._, util.Stats._, Hashable._, Names._
4+
import Types._, Symbols._, Contexts._, util.Stats._, Hashable._, Names._
55
import config.Config
66
import util.HashSet
77

@@ -54,7 +54,7 @@ object Uniques {
5454

5555
def enterIfNew(prefix: Type, name: Name): NamedType = {
5656
val h = doHash(name, prefix)
57-
if (monitored) recordCaching(h, classOf[CachedTermRef])
57+
if (monitored) recordCaching(h, classOf[NamedType])
5858
def newType =
5959
if (name.isTypeName) new CachedTypeRef(prefix, name.asTypeName, h)
6060
else new CachedTermRef(prefix, name.asTermName, h)
@@ -66,6 +66,32 @@ object Uniques {
6666
}
6767
}
6868

69+
final class WithFixedSymUniques extends HashSet[WithFixedSym](Config.initialUniquesCapacity) with Hashable {
70+
override def hash(x: WithFixedSym): Int = x.hash
71+
72+
private def findPrevious(h: Int, prefix: Type, sym: Symbol): NamedType = {
73+
var e = findEntryByHash(h)
74+
while (e != null) {
75+
if ((e.prefix eq prefix) && (e.fixedSym eq sym)) return e
76+
e = nextEntryByHash(h)
77+
}
78+
e
79+
}
80+
81+
def enterIfNew(prefix: Type, name: Name, sym: Symbol): NamedType = {
82+
val h = doHash(sym, prefix)
83+
if (monitored) recordCaching(h, classOf[WithFixedSym])
84+
def newType =
85+
if (name.isTypeName) new TypeRefWithFixedSym(prefix, name.asTypeName, sym.asInstanceOf[TypeSymbol], h)
86+
else new TermRefWithFixedSym(prefix, name.asTermName, sym.asInstanceOf[TermSymbol], h)
87+
if (h == NotCached) newType
88+
else {
89+
val r = findPrevious(h, prefix, sym)
90+
if (r ne null) r else addEntryAfterScan(newType)
91+
}
92+
}
93+
}
94+
6995
final class AppliedUniques extends HashSet[AppliedType](Config.initialUniquesCapacity) with Hashable {
7096
override def hash(x: AppliedType): Int = x.hash
7197

0 commit comments

Comments
 (0)