Skip to content

Commit b05a56c

Browse files
committed
Allow to reuse table of a util.{MutableHashSet,MutableHashMap}
On demand, fill the array with zeroes instead of creating a fresh one. This can save some array allocations.
1 parent b48554c commit b05a56c

File tree

7 files changed

+19
-11
lines changed

7 files changed

+19
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ class OrderingConstraint(private val boundsMap: ParamBounds,
360360
def adjustReferenced(bound: Type, isLower: Boolean, add: Boolean) =
361361
adjuster.variance = if isLower then 1 else -1
362362
adjuster.add = add
363-
adjuster.seen.clear()
363+
adjuster.seen.clear(resetToInitial = false)
364364
adjuster.traverse(bound)
365365

366366
/** Use an optimized strategy to adjust dependencies to account for the delta

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ trait ImplicitRunInfo:
630630

631631
def apply(tp: Type): collection.Set[Type] =
632632
parts = mutable.LinkedHashSet()
633-
partSeen.clear()
633+
partSeen.clear(resetToInitial = false)
634634
traverse(tp)
635635
parts
636636
end collectParts

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ abstract class GenericHashMap[Key, Value]
4242
else 1 << (32 - Integer.numberOfLeadingZeros(n))
4343

4444
/** Remove all elements from this table and set back to initial configuration */
45-
def clear(): Unit =
45+
def clear(resetToInitial: Boolean): Unit =
4646
used = 0
47-
allocate(roundToPower(initialCapacity))
47+
if resetToInitial then allocate(roundToPower(initialCapacity))
48+
else java.util.Arrays.fill(table, null)
4849

4950
/** The number of elements in the set */
5051
def size: Int = used

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ class HashSet[T](initialCapacity: Int = 8, capacityMultiple: Int = 2) extends Mu
4444
else if Integer.bitCount(n) == 1 then n
4545
else 1 << (32 - Integer.numberOfLeadingZeros(n))
4646

47-
/** Remove all elements from this set and set back to initial configuration */
48-
def clear(): Unit = {
47+
def clear(resetToInitial: Boolean): Unit =
4948
used = 0
50-
allocate(roundToPower(initialCapacity))
51-
}
49+
if resetToInitial then allocate(roundToPower(initialCapacity))
50+
else java.util.Arrays.fill(table, null)
5251

5352
/** The number of elements in the set */
5453
def size: Int = used

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ abstract class MutableMap[Key, Value] extends ReadOnlyMap[Key, Value]:
1313
remove(k)
1414
this
1515

16-
def clear(): Unit
16+
/** Remove all bindings from this map.
17+
* @param resetToInitial If true, set back to initial configuration, which includes
18+
* reallocating tables.
19+
*/
20+
def clear(resetToInitial: Boolean = true): Unit
1721

1822
def getOrElseUpdate(key: Key, value: => Value): Value

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ abstract class MutableSet[T] extends ReadOnlySet[T]:
1515
/** Remove element `x` from the set */
1616
def -=(x: T): Unit
1717

18-
def clear(): Unit
18+
/** Remove all elements from this set.
19+
* @param resetToInitial If true, set back to initial configuration, which includes
20+
* reallocating tables.
21+
*/
22+
def clear(resetToInitial: Boolean = true): Unit
1923

2024
def ++= (xs: IterableOnce[T]): Unit =
2125
xs.iterator.foreach(this += _)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ abstract class WeakHashSet[A <: AnyRef](initialCapacity: Int = 8, loadFactor: Do
204204
linkedListLoop(null, table(bucket))
205205
}
206206

207-
def clear(): Unit = {
207+
def clear(resetToInitial: Boolean): Unit = {
208208
table = new Array[Entry[A] | Null](table.size)
209209
threshold = computeThreshold
210210
count = 0

0 commit comments

Comments
 (0)