Skip to content

Commit

Permalink
Merge pull request #9544 from jxnu-liguobin/p12201
Browse files Browse the repository at this point in the history
Recognize calls to generic Array.apply with primitive arguments in CleanUp
  • Loading branch information
lrytz committed Mar 25, 2021
2 parents 5753e28 + d78040a commit 5d025ac
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/compiler/scala/tools/nsc/transform/CleanUp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,13 @@ abstract class CleanUp extends Statics with Transform with ast.TreeDSL {
case Apply(appMeth @ Select(appMethQual, _), elem0 :: Apply(wrapArrayMeth, (rest @ ArrayValue(elemtpt, _)) :: Nil) :: Nil)
if wrapArrayMeth.symbol == wrapVarargsArrayMethod(elemtpt.tpe) && appMeth.symbol == ArrayModule_apply(elemtpt.tpe) && treeInfo.isQualifierSafeToElide(appMethQual) =>
treeCopy.ArrayValue(rest, rest.elemtpt, elem0 :: rest.elems).transform(this)
// See scala/bug#12201, should be rewrite as Primitive Array.
// Match Array
case Apply(appMeth @ Select(appMethQual, _), Apply(wrapRefArrayMeth, StripCast(ArrayValue(elemtpt, elems)) :: Nil) :: _ :: Nil)
if appMeth.symbol == ArrayModule_genericApply && treeInfo.isQualifierSafeToElide(appMethQual) && currentRun.runDefinitions.primitiveWrapArrayMethod.contains(wrapRefArrayMeth.symbol) =>
localTyper.typedPos(elemtpt.pos) {
ArrayValue(TypeTree(elemtpt.tpe), elems)
} transform this
case Apply(appMeth @ Select(appMethQual, _), elem :: (nil: RefTree) :: Nil)
if nil.symbol == NilModule && appMeth.symbol == ArrayModule_apply(elem.tpe.widen) && treeInfo.isExprSafeToInline(nil) && treeInfo.isQualifierSafeToElide(appMethQual) =>
localTyper.typedPos(elem.pos) {
Expand Down
12 changes: 12 additions & 0 deletions src/reflect/scala/reflect/internal/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,18 @@ trait Definitions extends api.StandardDefinitions {
lazy val arrayClassMethod = getMemberMethod(ScalaRunTimeModule, nme.arrayClass)
lazy val wrapVarargsRefArrayMethod = getMemberMethod(ScalaRunTimeModule, nme.wrapRefArray)
lazy val genericWrapVarargsRefArrayMethod = getMemberMethod(ScalaRunTimeModule, nme.genericWrapArray)
lazy val primitiveWrapArrayMethod = Seq[Symbol](
getMemberMethod(ScalaRunTimeModule, nme.wrapBooleanArray),
getMemberMethod(ScalaRunTimeModule, nme.wrapByteArray),
getMemberMethod(ScalaRunTimeModule, nme.wrapCharArray),
getMemberMethod(ScalaRunTimeModule, nme.wrapIntArray),
getMemberMethod(ScalaRunTimeModule, nme.wrapDoubleArray),
getMemberMethod(ScalaRunTimeModule, nme.wrapFloatArray),
getMemberMethod(ScalaRunTimeModule, nme.wrapLongArray),
getMemberMethod(ScalaRunTimeModule, nme.wrapShortArray),
getMemberMethod(ScalaRunTimeModule, nme.wrapUnitArray)
)


lazy val RuntimeStatics_ioobe = getMemberMethod(RuntimeStaticsModule, nme.ioobe)

Expand Down
3 changes: 3 additions & 0 deletions test/files/instrumented/t12201.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Method call statistics:
1 scala/runtime/BoxedUnit.<clinit>()V
1 scala/runtime/BoxedUnit.<init>()V
29 changes: 29 additions & 0 deletions test/files/instrumented/t12201.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import scala.tools.partest.instrumented.Instrumentation._

object Test {
def main(args: Array[String]): Unit = {
startProfiling()

// to optimized
val x = Array[Double](1)
val y = Array[Double](1.0)

// Currently correctly optimized
val i = Array(1.0)
val j: Array[Double] = Array(1)

//others case
val a: Array[Double] = Array[Double](1.0)
val b: Array[Double] = Array[Double](1)
val c: Array[Double] = Array[Double](1: Double)
val d: Array[Double] = Array(1: Double)
val e = Array(1: Double)
val f = Array(1: Int)
val g = Array[Int](1)
val h = Array(1)
val k = Array[Unit](())

stopProfiling()
printStatistics()
}
}

0 comments on commit 5d025ac

Please sign in to comment.