Skip to content

Commit e9388bc

Browse files
committed
Better stats for unique tables
Now shows also total umber of accesses and hash collisions for each unique types set.
1 parent 19a14c5 commit e9388bc

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ object Contexts {
618618
"uniqueNamedTypes" -> uniqueNamedTypes)
619619

620620
/** A map that associates label and size of all uniques sets */
621-
def uniquesSizes: Map[String, Int] = uniqueSets.mapValues(_.size)
621+
def uniquesSizes: Map[String, (Int, Int, Int)] =
622+
uniqueSets.mapValues(s => (s.size, s.accesses, s.misses))
622623

623624
/** Number of findMember calls on stack */
624625
private[core] var findMemberCount: Int = 0

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class HashSet[T >: Null <: AnyRef](powerOfTwoInitialCapacity: Int, loadFactor: F
99

1010
protected def isEqual(x: T, y: T): Boolean = x.equals(y)
1111

12+
// Counters for Stats
13+
var accesses = 0
14+
var misses = 0
15+
1216
clear()
1317

1418
/** The number of elements in the set */
@@ -37,10 +41,12 @@ class HashSet[T >: Null <: AnyRef](powerOfTwoInitialCapacity: Int, loadFactor: F
3741
* If not, enter `x` in set and return `x`.
3842
*/
3943
def findEntryOrUpdate(x: T): T = {
44+
if (Stats.enabled) accesses += 1
4045
var h = index(hash(x))
4146
var entry = entryAt(h)
4247
while (entry ne null) {
4348
if (isEqual(x, entry)) return entry
49+
if (Stats.enabled) misses += 1
4450
h = index(h + 1)
4551
entry = entryAt(h)
4652
}
@@ -57,9 +63,11 @@ class HashSet[T >: Null <: AnyRef](powerOfTwoInitialCapacity: Int, loadFactor: F
5763

5864
/** The entry in the set such that `isEqual(x, entry)`, or else `null`. */
5965
def findEntry(x: T): T = {
66+
if (Stats.enabled) accesses += 1
6067
var h = index(hash(x))
6168
var entry = entryAt(h)
6269
while ((entry ne null) && !isEqual(x, entry)) {
70+
if (Stats.enabled) misses += 1
6371
h = index(h + 1)
6472
entry = entryAt(h)
6573
}
@@ -70,10 +78,12 @@ class HashSet[T >: Null <: AnyRef](powerOfTwoInitialCapacity: Int, loadFactor: F
7078

7179
/** Add entry `x` to set */
7280
def addEntry(x: T): Unit = {
81+
if (Stats.enabled) accesses += 1
7382
var h = index(hash(x))
7483
var entry = entryAt(h)
7584
while (entry ne null) {
7685
if (isEqual(x, entry)) return
86+
if (Stats.enabled) misses += 1
7787
h = index(h + 1)
7888
entry = entryAt(h)
7989
}
@@ -109,10 +119,12 @@ class HashSet[T >: Null <: AnyRef](powerOfTwoInitialCapacity: Int, loadFactor: F
109119
* follow a `findEntryByhash` or `nextEntryByHash` operation.
110120
*/
111121
protected def nextEntryByHash(hashCode: Int): T = {
122+
if (Stats.enabled) accesses += 1
112123
var entry = table(rover)
113124
while (entry ne null) {
114125
rover = index(rover + 1)
115126
if (hash(entry.asInstanceOf[T]) == hashCode) return entry.asInstanceOf[T]
127+
if (Stats.enabled) misses += 1
116128
entry = table(rover)
117129
}
118130
null

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import collection.mutable
8383
hb.continue = false
8484
println()
8585
println(hits.toList.sortBy(_._2).map{ case (x, y) => s"$x -> $y" } mkString "\n")
86-
println(s"sizes: ${ctx.base.uniquesSizes}")
86+
println(s"uniqieInfo (size, accesses, collisions): ${ctx.base.uniquesSizes}")
8787
}
8888
} else op
8989
}

tests/pos/i3965.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
trait Iterable[+A] extends IterableOps[A, Iterable, Iterable[A]]
2+
trait IterableOps[+A, +CCop[_], +C]
3+
4+
trait SortedSet[A] extends Iterable[A] with SortedSetOps[A, SortedSet, SortedSet[A]]
5+
6+
trait SortedSetOps[A, +CCss[X] <: SortedSet[X], +C <: SortedSetOps[A, CCss, C]]
7+
8+
class TreeSet[A]
9+
extends SortedSet[A]
10+
with SortedSetOps[A, TreeSet, TreeSet[A]]
11+
12+
class Test {
13+
def optionSequence1[CCos[X] <: IterableOps[X, CCos, _], A](xs: CCos[Option[A]]): Option[CCos[A]] = ???
14+
def optionSequence1[CC[X] <: SortedSet[X] with SortedSetOps[X, CC, CC[X]], A : Ordering](xs: CC[Option[A]]): Option[CC[A]] = ???
15+
16+
def test(xs2: TreeSet[Option[String]]) = {
17+
optionSequence1(xs2)
18+
}
19+
}

0 commit comments

Comments
 (0)