Skip to content

Commit 2ce2b38

Browse files
committed
Cache identifier search in trees
1 parent 76ef18c commit 2ce2b38

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

401401
implicit def eqSymbol: Eq[Symbol, Symbol] = Eq
402402

403+
/** Tree attachment containing the identifiers in a tree as a sorted array */
404+
val Ids = new Property.Key[Array[String]]
405+
403406
/** A Symbol represents a Scala definition/declaration or a package.
404407
* @param coord The coordinates of the symbol (a position or an index)
405408
* @param id A unique identifier of the symbol (unique per ContextBase)
@@ -652,7 +655,7 @@ object Symbols {
652655
}
653656
else tpd.EmptyTree
654657
case tree: Tree @ unchecked =>
655-
tree
658+
if (id.isEmpty || mightContain(tree, id)) tree else tpd.EmptyTree
656659
}
657660
}
658661

@@ -661,6 +664,22 @@ object Symbols {
661664
private[dotc] def treeOrProvider_=(t: TreeOrProvider)(implicit ctx: Context): Unit =
662665
myTree = t
663666

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