Skip to content

Commit 71f9efb

Browse files
authored
Merge pull request #3214 from dotty-staging/faster-Stats
Various performance optimizations
2 parents f59fdd8 + c47f0c2 commit 71f9efb

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ object Config {
147147
/** Initial size of superId table */
148148
final val InitialSuperIdsSize = 4096
149149

150-
/** Initial capacity of uniques HashMap */
151-
final val initialUniquesCapacity = 40000
150+
/** Initial capacity of uniques HashMap.
151+
* Note: This MUST BE a power of two to work with util.HashSet
152+
*/
153+
final val initialUniquesCapacity = 65536
152154

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
131131

132132
/** a faster version of cs1 intersect cs2 */
133133
def intersect(cs1: List[ClassSymbol], cs2: List[ClassSymbol]): List[ClassSymbol] = {
134-
val cs2AsSet = new util.HashSet[ClassSymbol](100)
134+
val cs2AsSet = new util.HashSet[ClassSymbol](128)
135135
cs2.foreach(cs2AsSet.addEntry)
136136
cs1.filter(cs2AsSet.contains)
137137
}

compiler/src/dotty/tools/dotc/transform/TreeTransform.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,16 @@ object TreeTransforms {
210210
*/
211211
class NXTransformations {
212212

213-
private def hasRedefinedMethod(cls: Class[_], name: String): Boolean =
214-
if (cls.getDeclaredMethods.exists(_.getName == name)) cls != classOf[TreeTransform]
215-
else hasRedefinedMethod(cls.getSuperclass, name)
213+
private def hasRedefinedMethod(cls: Class[_], name: String): Boolean = {
214+
val clsMethods = cls.getDeclaredMethods
215+
var i = clsMethods.length - 1
216+
while (i >= 0) {
217+
if (clsMethods(i).getName == name)
218+
return cls != classOf[TreeTransform]
219+
i -= 1
220+
}
221+
hasRedefinedMethod(cls.getSuperclass, name)
222+
}
216223

217224
/** Create an index array `next` of size one larger than the size of `transforms` such that
218225
* for each index i, `next(i)` is the smallest index j such that

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools.dotc.util
22

33
/** A hash set that allows some privileged protected access to its internals
44
*/
5-
class HashSet[T >: Null <: AnyRef](initialCapacity: Int, loadFactor: Float = 0.25f) extends Set[T] {
5+
class HashSet[T >: Null <: AnyRef](powerOfTwoInitialCapacity: Int, loadFactor: Float = 0.25f) extends Set[T] {
66
private var used: Int = _
77
private var limit: Int = _
88
private var table: Array[AnyRef] = _
@@ -20,11 +20,11 @@ class HashSet[T >: Null <: AnyRef](initialCapacity: Int, loadFactor: Float = 0.2
2020
/** Remove all elements from this set and set back to initial configuration */
2121
def clear(): Unit = {
2222
used = 0
23-
allocate(initialCapacity)
23+
allocate(powerOfTwoInitialCapacity)
2424
}
2525

26-
/** Turn hashode `x` into a table index */
27-
private def index(x: Int): Int = math.abs(x % table.length)
26+
/** Turn hashcode `x` into a table index */
27+
private def index(x: Int): Int = x & (table.length - 1)
2828

2929
/** Hashcode, can be overridden */
3030
def hash(x: T): Int = x.hashCode

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import collection.mutable
2121
}
2222

2323
@inline
24-
def record(fn: String, n: Int = 1) =
24+
def record(fn: => String, n: => Int = 1) =
2525
if (enabled) doRecord(fn, n)
2626

2727
private def doRecord(fn: String, n: Int) =

0 commit comments

Comments
 (0)