Skip to content

Optimize splices without captures #4211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1142,10 +1142,14 @@ class TreeUnpickler(reader: TastyReader,
val splice = splices(idx)
val reifiedArgs = args.map(arg => if (arg.isTerm) new TreeExpr(arg) else new TreeType(arg))
if (isType) {
val quotedType = splice.asInstanceOf[Seq[Any] => quoted.Type[_]](reifiedArgs)
val quotedType =
if (reifiedArgs.isEmpty) splice.asInstanceOf[quoted.Type[_]]
else splice.asInstanceOf[Seq[Any] => quoted.Type[_]](reifiedArgs)
PickledQuotes.quotedTypeToTree(quotedType)
} else {
val quotedExpr = splice.asInstanceOf[Seq[Any] => quoted.Expr[_]](reifiedArgs)
val quotedExpr =
if (reifiedArgs.isEmpty) splice.asInstanceOf[quoted.Expr[_]]
else splice.asInstanceOf[Seq[Any] => quoted.Expr[_]](reifiedArgs)
PickledQuotes.quotedExprToTree(quotedExpr)
}
}
Expand Down
40 changes: 25 additions & 15 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import dotty.tools.dotc.core.quoted._
* val x2 = ???
* ...
* ~{ ... '{ ... x1 ... x2 ...} ... }
* ~{ ... /* no references to xi */ ... }
* ...
* }
* ```
Expand All @@ -44,6 +45,7 @@ import dotty.tools.dotc.core.quoted._
* val x2 = ???
* ...
* Hole(0 | x1, x2)
* Hole(1 | )
* ...
* ]],
* List(
Expand All @@ -52,7 +54,8 @@ import dotty.tools.dotc.core.quoted._
* val x2$1 = args(1).asInstanceOf[Expr[T]] // can be asInstanceOf[Type[T]]
* ...
* { ... '{ ... x1$1.unary_~ ... x2$1.unary_~ ...} ... }
* }
* },
* { ... /* no references to xi */ ... } // optimized to not create lambda
* )
* )
* ```
Expand All @@ -66,12 +69,12 @@ import dotty.tools.dotc.core.quoted._
* ```
* to
* ```
* inline def foo[T1, ...](inline x1: X, ..., y1: Y, ....): Seq[Any] => Object = { (args: Seq[Any]) => {
* inline def foo[T1, ...](inline x1: X, ..., y1: Y, ....): Object = { (args: Seq[Any]) => {
* val T1$1 = args(0).asInstanceOf[Type[T1]]
* ...
* val x1$1 = args(0).asInstanceOf[X]
* val x1$1 = args(..).asInstanceOf[X]
* ...
* val y1$1 = args(1).asInstanceOf[Expr[Y]]
* val y1$1 = args(..).asInstanceOf[Expr[Y]]
* ...
* { ... T1$1.unary_~ ... x ... '(y1$1.unary_~) ... }
* }
Expand Down Expand Up @@ -417,6 +420,8 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
* val y$1 = args(1).asInstanceOf[Expr[Any]] // or .asInstanceOf[Type[Any]]
* { ... '{ ... x$1.unary_~ ... y$1.unary_~ ... } ... }
* }
* or if the spliced subexpression has no captures it will be transformed to
* { ... '{ ... x$1.unary_~ ... y$1.unary_~ ... } ... }
*
* See: `capture`
*
Expand All @@ -429,6 +434,19 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
* }
*/
private def makeLambda(tree: Tree)(implicit ctx: Context): Tree = {
var treeWithoutCaptures: Tree = null
def transformWithCapturer(tree: Tree)(capturer: mutable.Map[Symbol, Tree] => Tree => Tree)(implicit ctx: Context): Tree = {
val captured = mutable.LinkedHashMap.empty[Symbol, Tree]
val captured2 = capturer(captured)
outer.enteredSyms.foreach(s => capturers.put(s, captured2))
if (ctx.owner.owner.is(Macro))
outer.enteredSyms.reverse.foreach(s => captured2(ref(s)))
val tree2 = transform(tree)
capturers --= outer.enteredSyms
if (captured.isEmpty)
treeWithoutCaptures = tree2
seq(captured.result().valuesIterator.toList, tree2)
}
def body(arg: Tree)(implicit ctx: Context): Tree = {
var i = 0
transformWithCapturer(tree)(
Expand Down Expand Up @@ -456,18 +474,10 @@ class ReifyQuotes extends MacroTransformWithImplicits with InfoTransformer {
val lambdaOwner = ctx.owner.ownersIterator.find(o => levelOf.getOrElse(o, level) == level).get
val tpe = MethodType(defn.SeqType.appliedTo(defn.AnyType) :: Nil, tree.tpe.widen)
val meth = ctx.newSymbol(lambdaOwner, UniqueName.fresh(nme.ANON_FUN), Synthetic | Method, tpe)
Closure(meth, tss => body(tss.head.head)(ctx.withOwner(meth)).changeOwner(ctx.owner, meth))
}
val closure = Closure(meth, tss => body(tss.head.head)(ctx.withOwner(meth)).changeOwner(ctx.owner, meth))

private def transformWithCapturer(tree: Tree)(capturer: mutable.Map[Symbol, Tree] => Tree => Tree)(implicit ctx: Context): Tree = {
val captured = mutable.LinkedHashMap.empty[Symbol, Tree]
val captured2 = capturer(captured)
outer.enteredSyms.foreach(s => capturers.put(s, captured2))
if (ctx.owner.owner.is(Macro))
outer.enteredSyms.reverse.foreach(s => captured2(ref(s)))
val tree2 = transform(tree)
capturers --= outer.enteredSyms
seq(captured.result().valuesIterator.toList, tree2)
if (treeWithoutCaptures == null || ctx.owner.is(Macro)) closure
else treeWithoutCaptures
}

/** Returns true if this tree will be captured by `makeLambda` */
Expand Down