Skip to content

Commit 803d451

Browse files
committed
Merge pull request #2605 from JamesIry/weak_hashset_uniques
SI-7149 Use a WeakHashSet for type uniqueness
2 parents 4a8887c + b7e0fd2 commit 803d451

File tree

4 files changed

+588
-50
lines changed

4 files changed

+588
-50
lines changed

src/reflect/scala/reflect/internal/SymbolTable.scala

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,27 +307,20 @@ abstract class SymbolTable extends macros.Universe
307307
}
308308

309309
object perRunCaches {
310-
import java.lang.ref.WeakReference
311310
import scala.collection.generic.Clearable
312311

313312
// Weak references so the garbage collector will take care of
314313
// letting us know when a cache is really out of commission.
315-
private val caches = mutable.HashSet[WeakReference[Clearable]]()
314+
private val caches = WeakHashSet[Clearable]()
316315

317316
def recordCache[T <: Clearable](cache: T): T = {
318-
caches += new WeakReference(cache)
317+
caches += cache
319318
cache
320319
}
321320

322321
def clearAll() = {
323322
debuglog("Clearing " + caches.size + " caches.")
324-
caches foreach { ref =>
325-
val cache = ref.get()
326-
if (cache == null)
327-
caches -= ref
328-
else
329-
cache.clear()
330-
}
323+
caches foreach (_.clear)
331324
}
332325

333326
def newWeakMap[K, V]() = recordCache(mutable.WeakHashMap[K, V]())

src/reflect/scala/reflect/internal/Types.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,13 +3675,13 @@ trait Types
36753675
// Hash consing --------------------------------------------------------------
36763676

36773677
private val initialUniquesCapacity = 4096
3678-
private var uniques: util.HashSet[Type] = _
3678+
private var uniques: util.WeakHashSet[Type] = _
36793679
private var uniqueRunId = NoRunId
36803680

36813681
protected def unique[T <: Type](tp: T): T = {
36823682
if (Statistics.canEnable) Statistics.incCounter(rawTypeCount)
36833683
if (uniqueRunId != currentRunId) {
3684-
uniques = util.HashSet[Type]("uniques", initialUniquesCapacity)
3684+
uniques = util.WeakHashSet[Type](initialUniquesCapacity)
36853685
perRunCaches.recordCache(uniques)
36863686
uniqueRunId = currentRunId
36873687
}

0 commit comments

Comments
 (0)