Skip to content

Commit 61d4a9b

Browse files
committed
Increase loadFactor
Reduce capacityMultiple for HashSets from 4 to 2 and for HashMaps from 3 to 2. We observed not a large increase in misses, for half the space used, and consequently half as many re-enter operations.
1 parent 733f812 commit 61d4a9b

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

compiler/src/dotty/tools/dotc/config/Config.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ object Config {
174174
/** If set, enables tracing */
175175
final val tracingEnabled = false
176176

177-
/** Initial capacity of uniques HashMap.
178-
* Note: This MUST BE a power of two to work with util.HashSet
177+
/** Initial capacity of the uniques HashMap.
178+
* Note: This should be a power of two to work with util.HashSet
179179
*/
180-
final val initialUniquesCapacity = 65536
180+
final val initialUniquesCapacity = 0x8000
181181

182182
/** How many recursive calls to NamedType#underlying are performed before logging starts. */
183183
final val LogPendingUnderlyingThreshold = 50

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object Uniques:
3232
if tp.hash == NotCached then tp
3333
else ctx.uniques.put(tp).asInstanceOf[T]
3434

35-
final class NamedTypeUniques extends HashSet[NamedType](Config.initialUniquesCapacity) with Hashable:
35+
final class NamedTypeUniques extends HashSet[NamedType](Config.initialUniquesCapacity * 4) with Hashable:
3636
override def hash(x: NamedType): Int = x.hash
3737

3838
def enterIfNew(prefix: Type, designator: Designator, isTerm: Boolean)(using Context): NamedType =
@@ -53,7 +53,7 @@ object Uniques:
5353
addEntryAt(idx, newType)
5454
end NamedTypeUniques
5555

56-
final class AppliedUniques extends HashSet[AppliedType](Config.initialUniquesCapacity) with Hashable:
56+
final class AppliedUniques extends HashSet[AppliedType](Config.initialUniquesCapacity * 2) with Hashable:
5757
override def hash(x: AppliedType): Int = x.hash
5858

5959
def enterIfNew(tycon: Type, args: List[Type]): AppliedType =

compiler/src/dotty/tools/dotc/util/HashSet.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ object HashSet:
1919
* However, a table of size up to DenseLimit will be re-sized only
2020
* once the number of elements reaches the table's size.
2121
*/
22-
class HashSet[T >: Null <: AnyRef](initialCapacity: Int = 8, capacityMultiple: Int = 4) extends MutableSet[T] {
22+
class HashSet[T >: Null <: AnyRef](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends MutableSet[T] {
2323
import HashSet.DenseLimit
2424

2525
private var used: Int = _
@@ -57,6 +57,8 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int = 8, capacityMultiple: I
5757
/** Turn hashcode `x` into a table index */
5858
protected def index(x: Int): Int = x & (table.length - 1)
5959

60+
protected def currentTable: Array[AnyRef] = table
61+
6062
protected def firstIndex(x: T) = if isDense then 0 else index(hash(x))
6163
protected def nextIndex(idx: Int) =
6264
Stats.record(statsItem("miss"))
@@ -136,7 +138,7 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int = 8, capacityMultiple: I
136138
protected def growTable(): Unit =
137139
val oldTable = table
138140
val newLength =
139-
if oldTable.length == DenseLimit then DenseLimit * roundToPower(capacityMultiple)
141+
if oldTable.length == DenseLimit then DenseLimit * 2 * roundToPower(capacityMultiple)
140142
else table.length * 2
141143
allocate(newLength)
142144
copyFrom(oldTable)

0 commit comments

Comments
 (0)