Skip to content

Commit a6bebf1

Browse files
committed
Cache identifier search in trees
1 parent 9454daf commit a6bebf1

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,9 @@ object Decorators {
201201
def hl(args: Any*)(implicit ctx: Context): String =
202202
new SyntaxFormatter(sc).assemble(args).stripMargin
203203
}
204+
205+
implicit class ArrayInterpolator[T <: AnyRef](val arr: Array[T]) extends AnyVal {
206+
def binarySearch(x: T): Int = java.util.Arrays.binarySearch(arr.asInstanceOf[Array[Object]], x)
207+
}
204208
}
205209

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ import StdNames._
2121
import NameOps._
2222
import NameKinds.LazyImplicitName
2323
import ast.tpd
24-
import tpd.{Tree, TreeProvider}
24+
import tpd.{Tree, TreeProvider, TreeOps}
2525
import ast.TreeTypeMap
2626
import Constants.Constant
2727
import reporting.diagnostic.Message
2828
import Denotations.{ Denotation, SingleDenotation, MultiDenotation }
2929
import collection.mutable
3030
import io.AbstractFile
3131
import language.implicitConversions
32-
import util.{NoSource, DotClass}
32+
import util.{NoSource, DotClass, Property}
3333
import scala.collection.JavaConverters._
3434

3535
/** Creation methods for symbols */
@@ -392,6 +392,9 @@ object Symbols {
392392

393393
implicit def eqSymbol: Eq[Symbol, Symbol] = Eq
394394

395+
/** Tree attachment containing the identifiers in a tree as a sorted array */
396+
val Ids = new Property.Key[Array[String]]
397+
395398
/** A Symbol represents a Scala definition/declaration or a package.
396399
* @param coord The coordinates of the symbol (a position or an index)
397400
* @param id A unique identifier of the symbol (unique per ContextBase)
@@ -644,7 +647,7 @@ object Symbols {
644647
}
645648
else tpd.EmptyTree
646649
case tree: Tree @ unchecked =>
647-
tree
650+
if (id.isEmpty || mightContain(tree, id)) tree else tpd.EmptyTree
648651
}
649652
}
650653

@@ -653,6 +656,22 @@ object Symbols {
653656
private[dotc] def treeOrProvider_=(t: TreeOrProvider)(implicit ctx: Context): Unit =
654657
myTree = t
655658

659+
private def mightContain(tree: Tree, id: String)(implicit ctx: Context): Boolean = {
660+
val ids = tree.getAttachment(Ids) match {
661+
case Some(ids) => ids
662+
case None =>
663+
val idSet = mutable.SortedSet[String]()
664+
tree.foreachSubTree {
665+
case tree: tpd.RefTree => idSet += tree.name.toString
666+
case _ =>
667+
}
668+
val ids = idSet.toArray
669+
tree.putAttachment(Ids, ids)
670+
ids
671+
}
672+
ids.binarySearch(id) >= 0
673+
}
674+
656675
/** The source or class file from which this class was generated, null if not applicable. */
657676
override def associatedFile(implicit ctx: Context): AbstractFile =
658677
if (assocFile != null || (this.owner is PackageClass) || this.isEffectiveRoot) assocFile

compiler/src/dotty/tools/dotc/core/tasty/DottyUnpickler.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package dotc
33
package core
44
package tasty
55

6-
import Contexts._, SymDenotations._, Symbols._
6+
import Contexts._, SymDenotations._, Symbols._, Decorators._
77
import dotty.tools.dotc.ast.tpd
88
import TastyUnpickler._, TastyBuffer._
99
import util.Positions._
1010
import util.{SourceFile, NoSource}
1111
import Annotations.Annotation
12-
import core.Mode
1312
import classfile.ClassfileParser
1413

1514
object DottyUnpickler {
@@ -56,6 +55,6 @@ class DottyUnpickler(bytes: Array[Byte]) extends ClassfileParser.Embedded with t
5655

5756
override def mightContain(id: String)(implicit ctx: Context): Boolean = {
5857
if (ids == null) ids = unpickler.nameAtRef.contents.toArray.map(_.toString).sorted
59-
java.util.Arrays.binarySearch(ids.asInstanceOf[Array[Object]], id) >= 0
58+
ids.binarySearch(id) >= 0
6059
}
6160
}

0 commit comments

Comments
 (0)