Skip to content

Commit 245095d

Browse files
committed
Optimize enclosingInlineds
inlineContext already normalized most of InlinedCalls, normalize all of it instead. The new code isn't much more complex, the only tricky bit is assuming we don't need to normalize the pre-normalized list. In fact, this can be simplified further!
1 parent 33afaa2 commit 245095d

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,21 +1082,24 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
10821082
/** A key to be used in a context property that tracks enclosing inlined calls */
10831083
private val InlinedCalls = new Property.Key[List[Tree]]
10841084

1085-
override def inlineContext(call: Tree)(implicit ctx: Context): Context =
1086-
ctx.fresh.setProperty(InlinedCalls, call :: enclosingInlineds)
1087-
1088-
/** All enclosing calls that are currently inlined, from innermost to outermost.
1089-
* EmptyTree calls cancel the next-enclosing non-empty call in the list
1090-
*/
1091-
def enclosingInlineds(implicit ctx: Context): List[Tree] = {
1092-
def normalize(ts: List[Tree]): List[Tree] = ts match {
1093-
case t :: (ts1 @ (t1 :: ts2)) if t.isEmpty => normalize(if (t1.isEmpty) ts1 else ts2)
1094-
case t :: ts1 => t :: normalize(ts1)
1095-
case Nil => Nil
1085+
/** Record an enclosing inlined call.
1086+
* EmptyTree calls (for parameters) cancel the next-enclosing non-empty call in the list
1087+
*/
1088+
override def inlineContext(call: Tree)(implicit ctx: Context): Context = {
1089+
// We assume enclosingInlineds is already normalized, and only process the new call with the head.
1090+
val oldIC = enclosingInlineds
1091+
val newIC = (call, oldIC) match {
1092+
case (t, ts1 @ (t1 :: ts2)) if t.isEmpty => if (t1.isEmpty) ts1 else ts2
1093+
case _ => call :: oldIC
10961094
}
1097-
normalize(ctx.property(InlinedCalls).getOrElse(Nil))
1095+
ctx.fresh.setProperty(InlinedCalls, newIC)
10981096
}
10991097

1098+
/** All enclosing calls that are currently inlined, from innermost to outermost.
1099+
*/
1100+
def enclosingInlineds(implicit ctx: Context): List[Tree] =
1101+
ctx.property(InlinedCalls).getOrElse(Nil)
1102+
11001103
/** The source file where the symbol of the `inline` method referred to by `call`
11011104
* is defined
11021105
*/

0 commit comments

Comments
 (0)