Skip to content

Commit 0dd8688

Browse files
authored
Merge pull request #3216 from dotty-staging/more-opts
Some more small optimizations
2 parents 96b0ae4 + 128afcc commit 0dd8688

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,8 @@ import Decorators._
101101
cpy.installAfter(thisTransform)
102102
}
103103

104-
lazy val field = sym.field.orElse(newField).asTerm
105-
106-
def adaptToField(tree: Tree): Tree =
107-
if (tree.isEmpty) tree else tree.ensureConforms(field.info.widen)
108-
109104
val NoFieldNeeded = Lazy | Deferred | JavaDefined | (if (ctx.settings.YnoInline.value) EmptyFlags else Inline)
110105

111-
def isErasableBottomField(cls: Symbol): Boolean = {
112-
// TODO: For Scala.js, return false if this field is in a js.Object unless it is an ErasedPhantomClass.
113-
!field.isVolatile &&
114-
((cls eq defn.NothingClass) || (cls eq defn.NullClass) || (cls eq defn.BoxedUnitClass) || (cls eq defn.ErasedPhantomClass))
115-
}
116-
117106
def erasedBottomTree(sym: Symbol) = {
118107
if (sym eq defn.NothingClass) Throw(Literal(Constant(null)))
119108
else if (sym eq defn.NullClass) Literal(Constant(null))
@@ -125,7 +114,18 @@ import Decorators._
125114
}
126115
}
127116

128-
if (sym.is(Accessor, butNot = NoFieldNeeded))
117+
if (sym.is(Accessor, butNot = NoFieldNeeded)) {
118+
val field = sym.field.orElse(newField).asTerm
119+
120+
def adaptToField(tree: Tree): Tree =
121+
if (tree.isEmpty) tree else tree.ensureConforms(field.info.widen)
122+
123+
def isErasableBottomField(cls: Symbol): Boolean = {
124+
// TODO: For Scala.js, return false if this field is in a js.Object unless it is an ErasedPhantomClass.
125+
!field.isVolatile &&
126+
((cls eq defn.NothingClass) || (cls eq defn.NullClass) || (cls eq defn.BoxedUnitClass) || (cls eq defn.ErasedPhantomClass))
127+
}
128+
129129
if (sym.isGetter) {
130130
var rhs = tree.rhs.changeOwnerAfter(sym, field, thisTransform)
131131
if (isWildcardArg(rhs)) rhs = EmptyTree
@@ -151,6 +151,7 @@ import Decorators._
151151
}
152152
else tree // curiously, some accessors from Scala2 have ' ' suffixes. They count as
153153
// neither getters nor setters
154+
}
154155
else tree
155156
}
156157
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,12 @@ object TreeTransforms {
196196
}
197197
}
198198

199-
@sharable val NoTransform = new TreeTransform {
199+
private class NoTreeTransform extends TreeTransform {
200200
def phase = unsupported("phase")
201201
}
202202

203+
@sharable val NoTransform: TreeTransform = new NoTreeTransform
204+
203205
type Mutator[T] = (TreeTransform, T, Context) => TreeTransform
204206

205207
class TransformerInfo(val transformers: Array[TreeTransform], val nx: NXTransformations, val group: TreeTransformer)
@@ -209,15 +211,29 @@ object TreeTransforms {
209211
* @see NXTransformations.index for format of plan
210212
*/
211213
class NXTransformations {
214+
private val clsMethodsCache = new java.util.IdentityHashMap[Class[_], Array[java.lang.reflect.Method]]
212215

216+
// TODO: We spend too much time here. See if we can call it less or make it faster,
217+
// e.g. by checking `cls.getMethod(name, ...).getDeclaringClass != classOf[TreeTransform]` instead.
213218
private def hasRedefinedMethod(cls: Class[_], name: String): Boolean = {
214-
val clsMethods = cls.getDeclaredMethods
219+
if (cls.eq(classOf[TreeTransform]) || cls.eq(classOf[NoTreeTransform]) ||
220+
cls.eq(classOf[MiniPhaseTransform]))
221+
return false
222+
223+
// Class#getDeclaredMethods is slow, so we cache its output
224+
var clsMethods = clsMethodsCache.get(cls)
225+
if (clsMethods eq null) {
226+
clsMethods = cls.getDeclaredMethods
227+
clsMethodsCache.put(cls, clsMethods)
228+
}
229+
215230
var i = clsMethods.length - 1
216231
while (i >= 0) {
217232
if (clsMethods(i).getName == name)
218-
return cls != classOf[TreeTransform]
233+
return true
219234
i -= 1
220235
}
236+
221237
hasRedefinedMethod(cls.getSuperclass, name)
222238
}
223239

0 commit comments

Comments
 (0)