Skip to content

Commit 6e6df18

Browse files
committed
Avoid creating most trees in GenBCode
GenBCode generates trees when it desugars Idents based on their type. We now cache the generated tree, avoiding generating the same tree several times. In the Dotty bootstrap, this reduced generated trees in backend from 606K (about 20% of total) to 49K.
1 parent 3db8041 commit 6e6df18

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -424,18 +424,26 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
424424
}.bits
425425

426426
def isQualifierSafeToElide(qual: Tree): Boolean = tpd.isIdempotentExpr(qual)
427+
428+
private val desugared = new java.util.IdentityHashMap[Type, tpd.Select]
429+
427430
def desugarIdent(i: Ident): Option[tpd.Select] = {
428-
i.tpe match {
429-
case TermRef(prefix: TermRef, name) =>
430-
Some(tpd.ref(prefix).select(i.symbol))
431-
case TermRef(prefix: ThisType, name) =>
432-
Some(tpd.This(prefix.cls).select(i.symbol))
433-
case TermRef(NoPrefix, name) =>
434-
if (i.symbol is Flags.Method) Some(This(i.symbol.topLevelClass).select(i.symbol)) // workaround #342 todo: remove after fixed
435-
else None
436-
case _ => None
431+
var found = desugared.get(i.tpe)
432+
if (found == null) {
433+
i.tpe match {
434+
case TermRef(prefix: TermRef, name) =>
435+
found = tpd.ref(prefix).select(i.symbol)
436+
case TermRef(prefix: ThisType, name) =>
437+
found = tpd.This(prefix.cls).select(i.symbol)
438+
case TermRef(NoPrefix, name) =>
439+
if (i.symbol is Flags.Method) found = This(i.symbol.topLevelClass).select(i.symbol) // workaround #342 todo: remove after fixed
440+
case _ =>
441+
}
442+
if (found != null) desugared.put(i.tpe, found)
437443
}
444+
if (found == null) None else Some(found)
438445
}
446+
439447
def getLabelDefOwners(tree: Tree): Map[Tree, List[LabelDef]] = {
440448
// for each rhs of a defdef returns LabelDefs inside this DefDef
441449
val res = new collection.mutable.HashMap[Tree, List[LabelDef]]()
@@ -892,8 +900,6 @@ class DottyBackendInterface(outputDirectory: AbstractFile, val superCallsMap: Ma
892900
def parents: List[Type] = tp.parents
893901
}
894902

895-
896-
897903
object Assign extends AssignDeconstructor {
898904
def _1: Tree = field.lhs
899905
def _2: Tree = field.rhs

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ class Run(comp: Compiler, ictx: Context) {
128128
}
129129
}
130130
ctx.informTime(s"$phase ", start)
131+
Stats.record(s"total trees at end of $phase", ast.Trees.ntrees)
132+
for (unit <- units)
133+
Stats.record(s"retained typed trees at end of $phase", unit.tpdTree.treeSize)
131134
}
132135
if (!ctx.reporter.hasErrors) Rewrites.writeBack()
133-
for (unit <- units)
134-
Stats.record("retained typed trees at end", unit.tpdTree.treeSize)
135-
Stats.record("total trees at end", ast.Trees.ntrees)
136136
}
137137

138138
private sealed trait PrintedTree

0 commit comments

Comments
 (0)