Skip to content

Commit cdfa433

Browse files
committed
Optimise resolveDependencySource
1 parent 452356b commit cdfa433

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

compiler/src/dotty/tools/dotc/sbt/ExtractDependencies.scala

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,32 @@ private class ExtractDependenciesCollector extends tpd.TreeTraverser { thisTreeT
240240
_responsibleForImports
241241
}
242242

243+
private[this] var lastOwner: Symbol = _
244+
private[this] var lastDepSource: Symbol = _
245+
243246
/**
244247
* Resolves dependency source (that is, the closest non-local enclosing
245-
* class from a given `currentOwner` set by the `Traverser`).
246-
*
247-
* TODO: cache and/or optimise?
248+
* class from a given `ctx.owner`
248249
*/
249250
private def resolveDependencySource(implicit ctx: Context): Symbol = {
250-
def isNonLocalClass(sym: Symbol) = sym.isClass && !isLocal(sym)
251-
val source = ctx.owner.ownersIterator.find(isNonLocalClass).get
252-
if (source.is(PackageClass)) responsibleForImports else source
251+
def resolveDepSource: Symbol = {
252+
val owners = ctx.owner.ownersIterator
253+
while (owners.hasNext) {
254+
val source = owners.next()
255+
def isLocal = !owners.exists(_.isTerm) // side-effectful: consume iterator elements
256+
if (source.isClass && isLocal) return source
257+
else if (source.is(PackageClass)) return responsibleForImports
258+
}
259+
assert(false, "unreachable")
260+
NoSymbol
261+
}
262+
263+
if (lastOwner != ctx.owner) {
264+
lastOwner = ctx.owner
265+
lastDepSource = resolveDepSource
266+
}
267+
268+
lastDepSource
253269
}
254270

255271
private def addUsedName(fromClass: Symbol, name: Name, scope: UseScope): Unit = {

0 commit comments

Comments
 (0)