Skip to content

Commit 2df97c4

Browse files
committed
Avoid loading StringContext if not needed
1 parent eb4d2c8 commit 2df97c4

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object PowerInlined {
2+
import PowerMacro._
3+
4+
power(1, 5.0) // 1 quotes to unpickle
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object PowerInlined {
2+
import PowerMacro._
3+
4+
power(1, 5.0) // 1 quotes to unpickle
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.quoted.Expr
2+
3+
object PowerMacro {
4+
5+
inline def power(inline n: Long, x: Double) = ~powerCode(n, '(x))
6+
7+
def powerCode(n: Long, x: Expr[Double]): Expr[Double] =
8+
if (n == 0) '(1.0)
9+
else if (n % 2 == 0) '{ { val y = ~x * ~x; ~powerCode(n / 2, '(y)) } }
10+
else '{ ~x * ~powerCode(n - 1, x) }
11+
12+
}

compiler/src/dotty/tools/dotc/transform/localopt/StringInterpolatorOpt.scala

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,12 @@ class StringInterpolatorOpt extends MiniPhase {
4444
/** Matches an s or raw string interpolator */
4545
private object SOrRawInterpolator {
4646
def unapply(tree: Tree)(implicit ctx: Context): Option[(List[Literal], List[Tree])] = {
47-
if (tree.symbol.eq(defn.StringContextRaw) || tree.symbol.eq(defn.StringContextS)) {
48-
tree match {
49-
case Apply(Select(Apply(StringContextApply(), List(Literals(strs))), _),
50-
List(SeqLiteral(elems, _))) if elems.length == strs.length - 1 =>
51-
Some(strs, elems)
52-
case _ => None
53-
}
54-
} else None
47+
tree match {
48+
case Apply(Select(Apply(StringContextApply(), List(Literals(strs))), _),
49+
List(SeqLiteral(elems, _))) if elems.length == strs.length - 1 =>
50+
Some(strs, elems)
51+
case _ => None
52+
}
5553
}
5654
}
5755

@@ -82,6 +80,15 @@ class StringInterpolatorOpt extends MiniPhase {
8280
}
8381

8482
override def transformApply(tree: Apply)(implicit ctx: Context): Tree = {
83+
val sym = tree.symbol
84+
val isInterpolatedMethod = // Test names first to avoid loading scala.StringContext if not used
85+
(sym.name == nme.raw_ && sym.eq(defn.StringContextRaw)) ||
86+
(sym.name == nme.s && sym.eq(defn.StringContextS))
87+
if (isInterpolatedMethod) transformInterpolator(tree)
88+
else tree
89+
}
90+
91+
private def transformInterpolator(tree: Tree)(implicit ctx: Context): Tree = {
8592
tree match {
8693
case StringContextIntrinsic(strs: List[Literal], elems: List[Tree]) =>
8794
val stri = strs.iterator

0 commit comments

Comments
 (0)