Skip to content

Commit

Permalink
Merge pull request #2605 from JamesIry/weak_hashset_uniques
Browse files Browse the repository at this point in the history
SI-7149 Use a WeakHashSet for type uniqueness
  • Loading branch information
gkossakowski committed Jun 4, 2013
2 parents 4a8887c + b7e0fd2 commit 803d451
Show file tree
Hide file tree
Showing 4 changed files with 588 additions and 50 deletions.
13 changes: 3 additions & 10 deletions src/reflect/scala/reflect/internal/SymbolTable.scala
Expand Up @@ -307,27 +307,20 @@ abstract class SymbolTable extends macros.Universe
}

object perRunCaches {
import java.lang.ref.WeakReference
import scala.collection.generic.Clearable

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

def recordCache[T <: Clearable](cache: T): T = {
caches += new WeakReference(cache)
caches += cache
cache
}

def clearAll() = {
debuglog("Clearing " + caches.size + " caches.")
caches foreach { ref =>
val cache = ref.get()
if (cache == null)
caches -= ref
else
cache.clear()
}
caches foreach (_.clear)
}

def newWeakMap[K, V]() = recordCache(mutable.WeakHashMap[K, V]())
Expand Down
4 changes: 2 additions & 2 deletions src/reflect/scala/reflect/internal/Types.scala
Expand Up @@ -3675,13 +3675,13 @@ trait Types
// Hash consing --------------------------------------------------------------

private val initialUniquesCapacity = 4096
private var uniques: util.HashSet[Type] = _
private var uniques: util.WeakHashSet[Type] = _
private var uniqueRunId = NoRunId

protected def unique[T <: Type](tp: T): T = {
if (Statistics.canEnable) Statistics.incCounter(rawTypeCount)
if (uniqueRunId != currentRunId) {
uniques = util.HashSet[Type]("uniques", initialUniquesCapacity)
uniques = util.WeakHashSet[Type](initialUniquesCapacity)
perRunCaches.recordCache(uniques)
uniqueRunId = currentRunId
}
Expand Down

0 comments on commit 803d451

Please sign in to comment.