Skip to content

Commit c98e187

Browse files
authoredOct 9, 2020
Merge pull request #9867 from dotty-staging/optimize-frontend
Optimize core and frontend
2 parents 64315b7 + 4ed6a3f commit c98e187

File tree

12 files changed

+61
-17
lines changed

12 files changed

+61
-17
lines changed
 

‎compiler/src/dotty/tools/dotc/ast/Trees.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ object Trees {
174174
def toList: List[Tree[T]] = this :: Nil
175175

176176
/** if this tree is the empty tree, the alternative, else this tree */
177-
def orElse[U >: Untyped <: T](that: => Tree[U]): Tree[U] =
177+
inline def orElse[U >: Untyped <: T](inline that: Tree[U]): Tree[U] =
178178
if (this eq genericEmptyTree) that else this
179179

180180
/** The number of nodes in this tree */

‎compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ class ScalaSettings extends Settings.SettingGroup {
181181
val YnoDecodeStacktraces: Setting[Boolean] = BooleanSetting("-Yno-decode-stacktraces", "Show raw StackOverflow stacktraces, instead of decoding them into triggering operations.")
182182

183183
val Yinstrument: Setting[Boolean] = BooleanSetting("-Yinstrument", "Add instrumentation code that counts allocations and closure creations.")
184+
val YinstrumentDefs: Setting[Boolean] = BooleanSetting("-Yinstrument-defs", "Add instrumentation code that counts method calls; needs -Yinstrument to be set, too.")
184185

185186
/** Dottydoc specific settings */
186187
val siteRoot: Setting[String] = StringSetting(

‎compiler/src/dotty/tools/dotc/core/Contexts.scala

+5-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ object Contexts {
674674
final def retractMode(mode: Mode): c.type = c.setMode(c.mode &~ mode)
675675
}
676676

677-
private def exploreCtx(using Context): Context =
677+
private def exploreCtx(using Context): FreshContext =
678678
util.Stats.record("explore")
679679
val base = ctx.base
680680
import base._
@@ -701,6 +701,10 @@ object Contexts {
701701
val ectx = exploreCtx
702702
try op(using ectx) finally wrapUpExplore(ectx)
703703

704+
inline def exploreInFreshCtx[T](inline op: FreshContext ?=> T)(using Context): T =
705+
val ectx = exploreCtx
706+
try op(using ectx) finally wrapUpExplore(ectx)
707+
704708
private def changeOwnerCtx(owner: Symbol)(using Context): Context =
705709
val base = ctx.base
706710
import base._

‎compiler/src/dotty/tools/dotc/core/Denotations.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ object Denotations {
237237
def mapInfo(f: Type => Type)(using Context): Denotation
238238

239239
/** If this denotation does not exist, fallback to alternative */
240-
final def orElse(that: => Denotation): Denotation = if (this.exists) this else that
240+
inline def orElse(inline that: Denotation): Denotation = if (this.exists) this else that
241241

242242
/** The set of alternative single-denotations making up this denotation */
243243
final def alternatives: List[SingleDenotation] = altsWith(alwaysTrue)
@@ -596,7 +596,7 @@ object Denotations {
596596
def mapInfo(f: Type => Type)(using Context): SingleDenotation =
597597
derivedSingleDenotation(symbol, f(info))
598598

599-
def orElse(that: => SingleDenotation): SingleDenotation = if (this.exists) this else that
599+
inline def orElse(inline that: SingleDenotation): SingleDenotation = if (this.exists) this else that
600600

601601
def altsWith(p: Symbol => Boolean): List[SingleDenotation] =
602602
if (exists && p(symbol)) this :: Nil else Nil

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ object Symbols {
239239
}
240240

241241
/** This symbol, if it exists, otherwise the result of evaluating `that` */
242-
def orElse(that: => Symbol)(using Context): Symbol =
242+
inline def orElse(inline that: Symbol)(using Context): Symbol =
243243
if (this.exists) this else that
244244

245245
/** If this symbol satisfies predicate `p` this symbol, otherwise `NoSymbol` */

‎compiler/src/dotty/tools/dotc/core/Types.scala

+15-1
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,8 @@ object Types {
793793
*/
794794
final def memberNames(keepOnly: NameFilter, pre: Type = this)(using Context): Set[Name] = this match {
795795
case tp: ClassInfo =>
796-
tp.cls.classDenot.memberNames(keepOnly) filter (keepOnly(pre, _))
796+
val names = tp.cls.classDenot.memberNames(keepOnly)
797+
if keepOnly.isStable then names else names.filter(keepOnly(pre, _))
797798
case tp: RefinedType =>
798799
val ns = tp.parent.memberNames(keepOnly, pre)
799800
if (keepOnly(pre, tp.refinedName)) ns + tp.refinedName else ns
@@ -5665,6 +5666,11 @@ object Types {
56655666
*/
56665667
abstract class NameFilter {
56675668
def apply(pre: Type, name: Name)(using Context): Boolean
5669+
5670+
/** Filter does not need to be rechecked with full prefix, if it
5671+
* has been already checked for the class denotation of the prefix
5672+
*/
5673+
def isStable: Boolean
56685674
}
56695675

56705676
/** A filter for names of abstract types of a given type */
@@ -5674,6 +5680,7 @@ object Types {
56745680
val mbr = pre.nonPrivateMember(name)
56755681
mbr.symbol.is(Deferred) && mbr.info.isInstanceOf[RealTypeBounds]
56765682
}
5683+
def isStable = false
56775684
}
56785685

56795686
/** A filter for names of abstract types of a given type */
@@ -5683,12 +5690,14 @@ object Types {
56835690
val mbr = pre.member(name)
56845691
mbr.symbol.isType && !mbr.symbol.isClass
56855692
}
5693+
def isStable = false
56865694
}
56875695

56885696
/** A filter for names of deferred term definitions of a given type */
56895697
object abstractTermNameFilter extends NameFilter {
56905698
def apply(pre: Type, name: Name)(using Context): Boolean =
56915699
name.isTermName && pre.nonPrivateMember(name).hasAltWith(_.symbol.is(Deferred))
5700+
def isStable = false
56925701
}
56935702

56945703
/** A filter for names of type aliases of a given type */
@@ -5698,19 +5707,23 @@ object Types {
56985707
val mbr = pre.nonPrivateMember(name)
56995708
mbr.symbol.isAliasType
57005709
}
5710+
def isStable = false
57015711
}
57025712

57035713
object typeNameFilter extends NameFilter {
57045714
def apply(pre: Type, name: Name)(using Context): Boolean = name.isTypeName
5715+
def isStable = true
57055716
}
57065717

57075718
object fieldFilter extends NameFilter {
57085719
def apply(pre: Type, name: Name)(using Context): Boolean =
57095720
name.isTermName && (pre member name).hasAltWith(!_.symbol.is(Method))
5721+
def isStable = true
57105722
}
57115723

57125724
object takeAllFilter extends NameFilter {
57135725
def apply(pre: Type, name: Name)(using Context): Boolean = true
5726+
def isStable = true
57145727
}
57155728

57165729
object implicitFilter extends NameFilter {
@@ -5719,6 +5732,7 @@ object Types {
57195732
* no post-filtering is needed.
57205733
*/
57215734
def apply(pre: Type, name: Name)(using Context): Boolean = true
5735+
def isStable = true
57225736
}
57235737

57245738
// ----- Debug ---------------------------------------------------------

‎compiler/src/dotty/tools/dotc/interactive/Completion.scala

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ object Completion {
323323
private object completionsFilter extends NameFilter {
324324
def apply(pre: Type, name: Name)(using Context): Boolean =
325325
!name.isConstructorName && name.toTermName.info.kind == SimpleNameKind
326+
def isStable = true
326327
}
327328
}
328329

‎compiler/src/dotty/tools/dotc/parsing/Scanners.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ object Scanners {
565565
lastOffset = prev.lastOffset
566566
lineOffset = prev.lineOffset
567567
}
568-
token match {
568+
(token: @switch) match {
569569
case CASE =>
570570
lookAhead()
571571
if (token == CLASS) fuse(CASECLASS)
@@ -601,8 +601,8 @@ object Scanners {
601601
if colonSyntax then observeColonEOL()
602602
case RBRACE | RPAREN | RBRACKET =>
603603
closeIndented()
604-
case EOF if !source.maybeIncomplete =>
605-
closeIndented()
604+
case EOF =>
605+
if !source.maybeIncomplete then closeIndented()
606606
case _ =>
607607
}
608608
}

‎compiler/src/dotty/tools/dotc/transform/Instrumentation.scala

+15
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ class Instrumentation extends MiniPhase { thisPhase =>
7474
private def ok(using Context) =
7575
!ctx.owner.ownersIterator.exists(_.name.toString.startsWith("Stats"))
7676

77+
override def transformDefDef(tree: DefDef)(using Context): Tree =
78+
val sym = tree.symbol
79+
if ctx.settings.YinstrumentDefs.value
80+
&& ok
81+
&& sym.exists
82+
&& !sym.isOneOf(Synthetic | Artifact)
83+
then
84+
def icall = record(i"method/${sym.fullName}", tree)
85+
def rhs1 = tree.rhs match
86+
case rhs @ Block(stats, expr) => cpy.Block(rhs)(icall :: stats, expr)
87+
case _: Match | _: If | _: Try | _: Labeled => cpy.Block(tree.rhs)(icall :: Nil, tree.rhs)
88+
case rhs => rhs
89+
cpy.DefDef(tree)(rhs = rhs1)
90+
else tree
91+
7792
override def transformApply(tree: Apply)(using Context): Tree = tree.fun match {
7893
case Select(nu: New, _) =>
7994
cpy.Block(tree)(record(i"alloc/${nu.tpe}", tree) :: Nil, tree)

‎compiler/src/dotty/tools/dotc/transform/SymUtils.scala

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ object SymUtils {
2222

2323
extension (self: Symbol) {
2424

25-
/** All traits implemented by a class or trait except for those inherited through the superclass. */
25+
/** All traits implemented by a class or trait except for those inherited
26+
* through the superclass. Traits are given in the order they appear in the
27+
* parents clause (which is the reverse of their order in baseClasses)
28+
*/
2629
def directlyInheritedTraits(using Context): List[ClassSymbol] = {
2730
val superCls = self.asClass.superClass
2831
val baseClasses = self.asClass.baseClasses
2932
if (baseClasses.isEmpty) Nil
30-
else baseClasses.tail.takeWhile(_ ne superCls).reverse
33+
else
34+
def recur(bcs: List[ClassSymbol], acc: List[ClassSymbol]): List[ClassSymbol] = bcs match
35+
case bc :: bcs1 => if bc eq superCls then acc else recur(bcs1, bc :: acc)
36+
case nil => acc
37+
recur(baseClasses.tail, Nil)
3138
}
3239

3340
/** All traits implemented by a class, except for those inherited through the superclass.

‎compiler/src/dotty/tools/dotc/typer/Implicits.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,12 @@ object Implicits:
245245
if refs.isEmpty && (!considerExtension || companionRefs.isEmpty) then
246246
Nil
247247
else
248-
val nestedCtx = ctx.fresh.addMode(Mode.TypevarsMissContext)
249248
val candidates = new mutable.ListBuffer[Candidate]
250249
def tryCandidate(extensionOnly: Boolean)(ref: ImplicitRef) =
251-
var ckind = explore(candidateKind(ref.underlyingRef))(using nestedCtx)
250+
var ckind = exploreInFreshCtx { (using ctx: FreshContext) =>
251+
ctx.setMode(ctx.mode | Mode.TypevarsMissContext)
252+
candidateKind(ref.underlyingRef)
253+
}
252254
if extensionOnly then ckind &= Candidate.Extension
253255
if ckind != Candidate.None then
254256
candidates += Candidate(ref, ckind, level)

‎compiler/src/dotty/tools/dotc/typer/RefChecks.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ object RefChecks {
2929

3030
private val defaultMethodFilter = new NameFilter {
3131
def apply(pre: Type, name: Name)(using Context): Boolean = name.is(DefaultGetterName)
32+
def isStable = true
3233
}
3334

3435
/** Only one overloaded alternative is allowed to define default arguments */
@@ -490,10 +491,9 @@ object RefChecks {
490491
*/
491492
def missingTermSymbols: List[Symbol] =
492493
val buf = new mutable.ListBuffer[Symbol]
493-
for bc <- clazz.baseClasses
494-
sym <- bc.info.decls.toList
495-
if sym.is(DeferredTerm) && !isImplemented(sym) && !ignoreDeferred(sym)
496-
do buf += sym
494+
for bc <- clazz.baseClasses; sym <- bc.info.decls.toList do
495+
if sym.is(DeferredTerm) && !isImplemented(sym) && !ignoreDeferred(sym) then
496+
buf += sym
497497
buf.toList
498498

499499
// 2. Check that only abstract classes have deferred members

0 commit comments

Comments
 (0)
Please sign in to comment.