Skip to content

FIx source of performance regression in #3961 #4480

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
5 changes: 5 additions & 0 deletions bench/tests/power-macro/PowerInlined-1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object PowerInlined {
import PowerMacro._

power(1, 5.0) // 1 quotes to unpickle
}
5 changes: 5 additions & 0 deletions bench/tests/power-macro/PowerInlined.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object PowerInlined {
import PowerMacro._

power(1, 5.0) // 1 quotes to unpickle
}
12 changes: 12 additions & 0 deletions bench/tests/power-macro/PowerMacro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.quoted.Expr

object PowerMacro {

inline def power(inline n: Long, x: Double) = ~powerCode(n, '(x))

def powerCode(n: Long, x: Expr[Double]): Expr[Double] =
if (n == 0) '(1.0)
else if (n % 2 == 0) '{ { val y = ~x * ~x; ~powerCode(n / 2, '(y)) } }
else '{ ~x * ~powerCode(n - 1, x) }

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ class StringInterpolatorOpt extends MiniPhase {
/** Matches an s or raw string interpolator */
private object SOrRawInterpolator {
def unapply(tree: Tree)(implicit ctx: Context): Option[(List[Literal], List[Tree])] = {
if (tree.symbol.eq(defn.StringContextRaw) || tree.symbol.eq(defn.StringContextS)) {
tree match {
case Apply(Select(Apply(StringContextApply(), List(Literals(strs))), _),
List(SeqLiteral(elems, _))) if elems.length == strs.length - 1 =>
Some(strs, elems)
case _ => None
}
} else None
tree match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A minor issue: the object name SOrRawInterpolator doesn't strictly correspond to what is in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not logic I made, I did not choose that name. Do you want me to change it to something else?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It used to reflect what was in the code. Not anymore. I would just inline this extractor in StringContextIntrinsic

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the change, it's. After the symbolic equality checks are removed, it wasn't. But as we are going to remove local optimisations, it's fine to keep it as it is now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liufengyun This is not part of the local optimisations that will be removed. Scalac does something similar

case Apply(Select(Apply(StringContextApply(), List(Literals(strs))), _),
List(SeqLiteral(elems, _))) if elems.length == strs.length - 1 =>
Some(strs, elems)
case _ => None
}
}
}

Expand Down Expand Up @@ -82,6 +80,15 @@ class StringInterpolatorOpt extends MiniPhase {
}

override def transformApply(tree: Apply)(implicit ctx: Context): Tree = {
val sym = tree.symbol
val isInterpolatedMethod = // Test names first to avoid loading scala.StringContext if not used
(sym.name == nme.raw_ && sym.eq(defn.StringContextRaw)) ||
(sym.name == nme.s && sym.eq(defn.StringContextS))
if (isInterpolatedMethod) transformInterpolator(tree)
else tree
}

private def transformInterpolator(tree: Tree)(implicit ctx: Context): Tree = {
tree match {
case StringContextIntrinsic(strs: List[Literal], elems: List[Tree]) =>
val stri = strs.iterator
Expand Down