Skip to content

Commit 82d25a0

Browse files
committed
Cache identifier search in trees
1 parent e407c29 commit 82d25a0

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 */
@@ -402,6 +402,9 @@ object Symbols {
402402

403403
implicit def eqSymbol: Eq[Symbol, Symbol] = Eq
404404

405+
/** Tree attachment containing the identifiers in a tree as a sorted array */
406+
val Ids = new Property.Key[Array[String]]
407+
405408
/** A Symbol represents a Scala definition/declaration or a package.
406409
* @param coord The coordinates of the symbol (a position or an index)
407410
* @param id A unique identifier of the symbol (unique per ContextBase)
@@ -654,7 +657,7 @@ object Symbols {
654657
}
655658
else tpd.EmptyTree
656659
case tree: Tree @ unchecked =>
657-
tree
660+
if (id.isEmpty || mightContain(tree, id)) tree else tpd.EmptyTree
658661
}
659662
}
660663

@@ -663,6 +666,22 @@ object Symbols {
663666
private[dotc] def treeOrProvider_=(t: TreeOrProvider)(implicit ctx: Context): Unit =
664667
myTree = t
665668

669+
private def mightContain(tree: Tree, id: String)(implicit ctx: Context): Boolean = {
670+
val ids = tree.getAttachment(Ids) match {
671+
case Some(ids) => ids
672+
case None =>
673+
val idSet = mutable.SortedSet[String]()
674+
tree.foreachSubTree {
675+
case tree: tpd.RefTree => idSet += tree.name.toString
676+
case _ =>
677+
}
678+
val ids = idSet.toArray
679+
tree.putAttachment(Ids, ids)
680+
ids
681+
}
682+
ids.binarySearch(id) >= 0
683+
}
684+
666685
/** The source or class file from which this class was generated, null if not applicable. */
667686
override def associatedFile(implicit ctx: Context): AbstractFile =
668687
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)