|
| 1 | +package dotty.tools.dotc.transform.localopt |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.Trees._ |
| 4 | +import dotty.tools.dotc.ast.tpd |
| 5 | +import dotty.tools.dotc.core.Constants.Constant |
| 6 | +import dotty.tools.dotc.core.Contexts.Context |
| 7 | +import dotty.tools.dotc.core.StdNames._ |
| 8 | +import dotty.tools.dotc.core.Symbols._ |
| 9 | +import dotty.tools.dotc.transform.MegaPhase.MiniPhase |
| 10 | + |
| 11 | +/** |
| 12 | + * MiniPhase to transform s and raw string interpolators from using StringContext to string |
| 13 | + * concatenation. Since string concatenation uses the Java String builder, we get a performance |
| 14 | + * improvement in terms of these two interpolators. |
| 15 | + * |
| 16 | + * More info here: |
| 17 | + * https://medium.com/@dkomanov/scala-string-interpolation-performance-21dc85e83afd |
| 18 | + */ |
| 19 | +class StringInterpolatorOpt extends MiniPhase { |
| 20 | + import tpd._ |
| 21 | + |
| 22 | + override def phaseName: String = "stringInterpolatorOpt" |
| 23 | + |
| 24 | + /** Matches a list of constant literals */ |
| 25 | + private object Literals { |
| 26 | + def unapply(tree: SeqLiteral)(implicit ctx: Context): Option[List[Literal]] = { |
| 27 | + tree.elems match { |
| 28 | + case literals if literals.forall(_.isInstanceOf[Literal]) => |
| 29 | + Some(literals.map(_.asInstanceOf[Literal])) |
| 30 | + case _ => None |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private object StringContextApply { |
| 36 | + def unapply(tree: Select)(implicit ctx: Context): Boolean = { |
| 37 | + tree.symbol.eq(defn.StringContextModule_apply) && { |
| 38 | + val qualifier = tree.qualifier |
| 39 | + qualifier.isInstanceOf[Ident] && qualifier.symbol.eq(defn.StringContextModule) |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** Matches an s or raw string interpolator */ |
| 45 | + private object SOrRawInterpolator { |
| 46 | + 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 |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Match trees that resemble s and raw string interpolations. In the case of the s |
| 60 | + * interpolator, escapes the string constants. Exposes the string constants as well as |
| 61 | + * the variable references. |
| 62 | + */ |
| 63 | + private object StringContextIntrinsic { |
| 64 | + def unapply(tree: Apply)(implicit ctx: Context): Option[(List[Literal], List[Tree])] = { |
| 65 | + tree match { |
| 66 | + case SOrRawInterpolator(strs, elems) => |
| 67 | + if (tree.symbol == defn.StringContextRaw) Some(strs, elems) |
| 68 | + else { // tree.symbol == defn.StringContextS |
| 69 | + try { |
| 70 | + val escapedStrs = strs.map { str => |
| 71 | + val escapedValue = StringContext.processEscapes(str.const.stringValue) |
| 72 | + cpy.Literal(str)(Constant(escapedValue)) |
| 73 | + } |
| 74 | + Some(escapedStrs, elems) |
| 75 | + } catch { |
| 76 | + case _: StringContext.InvalidEscapeException => None |
| 77 | + } |
| 78 | + } |
| 79 | + case _ => None |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + override def transformApply(tree: Apply)(implicit ctx: Context): Tree = { |
| 85 | + tree match { |
| 86 | + case StringContextIntrinsic(strs: List[Literal], elems: List[Tree]) => |
| 87 | + val stri = strs.iterator |
| 88 | + val elemi = elems.iterator |
| 89 | + var result: Tree = stri.next |
| 90 | + def concat(tree: Tree): Unit = { |
| 91 | + result = result.select(defn.String_+).appliedTo(tree) |
| 92 | + } |
| 93 | + while (elemi.hasNext) { |
| 94 | + concat(elemi.next) |
| 95 | + val str = stri.next |
| 96 | + if (!str.const.stringValue.isEmpty) concat(str) |
| 97 | + } |
| 98 | + result |
| 99 | + case _ => tree |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments