Skip to content

Commit 940a2c1

Browse files
authored
Merge pull request #5748 from dotty-staging/try-optimize-1
Reduce some allocations
2 parents 2d16d9f + 2ee9994 commit 940a2c1

28 files changed

+211
-75
lines changed

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
477477
// unrelated change.
478478
ctx.base.settings.YnoGenericSig.value
479479
|| sym.is(Flags.Artifact)
480-
|| sym.is(Flags.allOf(Flags.Method, Flags.Lifted))
480+
|| sym.is(Flags.LiftedMethod)
481481
|| sym.is(Flags.Bridge)
482482
)
483483

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class Compiler {
9797
List(new Constructors, // Collect initialization code in primary constructors
9898
// Note: constructors changes decls in transformTemplate, no InfoTransformers should be added after it
9999
new FunctionalInterfaces, // Rewrites closures to implement @specialized types of Functions.
100+
new Instrumentation, // Count closure allocations under -Yinstrument-closures
100101
new GetClass) :: // Rewrites getClass calls on primitive types.
101102
List(new LinkScala2Impls, // Redirect calls to trait methods defined by Scala 2.x, so that they now go to their implementations
102103
new LambdaLift, // Lifts out nested functions to class scope, storing free variables in environments

compiler/src/dotty/tools/dotc/ast/Positioned.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
5151
else {
5252
val newpd: this.type =
5353
if (mySpan.isSynthetic) {
54-
if (!mySpan.exists && span.exists)
54+
if (!mySpan.exists && span.exists) {
5555
envelope(source, span.startPos) // fill in children spans
56+
() // Note: the `()` is there to prevent some inefficient code from being generated.
57+
// Without it we get an allocation of a span here since the result type of the `if`
58+
// is `Any`, the lub of `Span` and `Unit`.
59+
}
5660
this
5761
}
5862
else cloneIn(source)

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,10 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
978978
}
979979

980980
implicit class ListOfTreeDecorator(val xs: List[tpd.Tree]) extends AnyVal {
981-
def tpes: List[Type] = xs map (_.tpe)
981+
def tpes: List[Type] = xs match {
982+
case x :: xs1 => x.tpe :: xs1.tpes
983+
case nil => Nil
984+
}
982985
}
983986

984987
/** A trait for loaders that compute trees. Currently implemented just by DottyUnpickler. */

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ class ScalaSettings extends Settings.SettingGroup {
149149

150150
val YnoDecodeStacktraces: Setting[Boolean] = BooleanSetting("-Yno-decode-stacktraces", "Show raw StackOverflow stacktraces, instead of decoding them into triggering operations.")
151151

152+
val YinstrumentClosures: Setting[Boolean] = BooleanSetting("-Yinstrument-closures", "Add instrumentation code that counts closure creations.")
153+
val YinstrumentAllocations: Setting[Boolean] = BooleanSetting("-Yinstrument-allocations", "Add instrumentation code that counts allocations.")
154+
152155
/** Dottydoc specific settings */
153156
val siteRoot: Setting[String] = StringSetting(
154157
"-siteroot",

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,21 @@ object Decorators {
151151
* exact meaning of "contains" here.
152152
*/
153153
implicit class PhaseListDecorator(val names: List[String]) extends AnyVal {
154-
def containsPhase(phase: Phase): Boolean = phase match {
155-
case phase: MegaPhase => phase.miniPhases.exists(containsPhase)
156-
case _ =>
157-
names exists { name =>
158-
name == "all" || {
159-
val strippedName = name.stripSuffix("+")
160-
val logNextPhase = name != strippedName
161-
phase.phaseName.startsWith(strippedName) ||
162-
(logNextPhase && phase.prev.phaseName.startsWith(strippedName))
163-
}
154+
def containsPhase(phase: Phase): Boolean =
155+
names.nonEmpty && {
156+
phase match {
157+
case phase: MegaPhase => phase.miniPhases.exists(containsPhase)
158+
case _ =>
159+
names exists { name =>
160+
name == "all" || {
161+
val strippedName = name.stripSuffix("+")
162+
val logNextPhase = name != strippedName
163+
phase.phaseName.startsWith(strippedName) ||
164+
(logNextPhase && phase.prev.phaseName.startsWith(strippedName))
165+
}
166+
}
164167
}
165-
}
168+
}
166169
}
167170

168171
implicit class genericDeco[T](val x: T) extends AnyVal {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,9 @@ class Definitions {
732732
lazy val ValueOfType: TypeRef = ctx.requiredClassRef("scala.ValueOf")
733733
def ValueOfClass(implicit ctx: Context): ClassSymbol = ValueOfType.symbol.asClass
734734

735+
lazy val StatsModule = ctx.requiredModule("dotty.tools.dotc.util.Stats")
736+
def Stats_doRecord(implicit ctx: Context): TermSymbol = StatsModule.requiredMethod("doRecord")
737+
735738
lazy val XMLTopScopeModuleRef: TermRef = ctx.requiredModuleRef("scala.xml.TopScope")
736739

737740
lazy val TupleTypeRef: TypeRef = ctx.requiredClassRef("scala.Tuple")

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ object Flags {
701701
/** Labeled protected[this] */
702702
final val ProtectedLocal: FlagConjunction = allOf(Protected, Local)
703703

704+
final val LiftedMethod: FlagConjunction = allOf(Lifted, Method)
705+
704706
/** Java symbol which is `protected` and `static` */
705707
final val StaticProtected: FlagConjunction = allOf(JavaDefined, Protected, JavaStatic)
706708

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ case class Mode(val bits: Int) extends AnyVal {
1212

1313
override def toString: String =
1414
(0 until 31).filter(i => (bits & (1 << i)) != 0).map(modeName).mkString("Mode(", ",", ")")
15+
16+
def ==(that: Mode): Boolean = this.bits == that.bits
17+
def !=(that: Mode): Boolean = this.bits != that.bits
1518
}
1619

1720
object Mode {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ object Names {
570570
*/
571571
def termName(cs: Array[Char], offset: Int, len: Int): SimpleName = synchronized {
572572
util.Stats.record("termName")
573-
val h = hashValue(cs, offset, len) & (table.size - 1)
573+
val h = hashValue(cs, offset, len) & (table.length - 1)
574574

575575
/** Make sure the capacity of the character array is at least `n` */
576576
def ensureCapacity(n: Int) =

0 commit comments

Comments
 (0)