-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Avoid unnecessary wrapping and array copy when using Array.apply (and potentially List.apply, ...) #502
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
Comments
"early inlining" is one of the things that I want to do in optimizer, to have smaller trees for methods(and less methods, due to cheap DCL). I'm not sure if we want to cope with all this in the compiler itself. But I will indeed lock into this in optimizer. |
When you say optimizer do you mean the backend optimizer or a hypothetical link-time optimizer? |
Link-time optimizer. |
Also, |
class Test {
val a = Array(1, 2, 3)
val b = Array[Int](1, 2, 3)
} Before cleanup: Test.this.a = scala.Array.apply(1, scala.this.Predef.wrapIntArray(Array[Int]{2, 3}));
Test.this.b = scala.Array.apply(scala.this.Predef.wrapIntArray(Array[Int]{1, 2, 3}), (ClassTag.Int(): scala.reflect.ClassTag)).$asInstanceOf[Array[Int]](); Strictly speaking, cleanup is not be free to rewrite the second case without extra knowledge that the val b1 = Array[Int](1, 2, 3)(null)
val b2 = Array[Int](1, 2, 3)({???; implicitly[reflect.ClassTag[Int]]}) |
Augmenting class tag materialization to attach a marker tree annotation to its result might be a robust way to distinguish the cases. |
Fix #502: Optimize `Array.apply([...])` to `[...]`
Currently, the following code:
Is transformed to:
Which is as inefficient as it looks.
scalac
has rewriting rules inCleanup
to deal with this: https://github.com/scala/scala/blob/d030172d7ef807d85391d32c5456b1b97b15a402/src/compiler/scala/tools/nsc/transform/CleanUp.scala#L536-L545We could do something similar, keeping in mind the following:
scalac
to deal with all possible cases, for examplescalac
optimizesArray(1,2,3)
andArray[String]("foo", "bar")
but does nothing withArray[Int](1,2,3)
.Erasure
, so that it does not need to take into account whatever crazy encoding we'll come up with for arrays of value classes.The alternative would be to do this and other optimizations like
List(1,2,3)
using macros, see https://groups.google.com/forum/#!topic/scala-internals/5oRYBYBUqMY/discussion and SI-6247, is this the direction we want to go in?The text was updated successfully, but these errors were encountered: