|
13 | 13 | package scala
|
14 | 14 |
|
15 | 15 | import scala.collection.generic._
|
16 |
| -import scala.collection.{ mutable, immutable } |
17 |
| -import mutable.{ ArrayBuilder, ArraySeq } |
18 |
| -import scala.reflect.ClassTag |
19 |
| -import scala.runtime.ScalaRunTime.{ array_apply, array_update } |
| 16 | +import scala.collection.{immutable, mutable} |
| 17 | +import mutable.{ArrayBuilder, ArraySeq} |
| 18 | +import scala.reflect.{ClassTag, classTag} |
| 19 | +import scala.runtime.ScalaRunTime |
| 20 | +import scala.runtime.ScalaRunTime.{array_apply, array_update} |
20 | 21 |
|
21 | 22 | /** Contains a fallback builder for arrays when the element type
|
22 | 23 | * does not have a class tag. In that case a generic array is built.
|
@@ -194,10 +195,20 @@ object Array extends FallbackArrayBuilding {
|
194 | 195 | // Subject to a compiler optimization in Cleanup.
|
195 | 196 | // Array(e0, ..., en) is translated to { val a = new Array(3); a(i) = ei; a }
|
196 | 197 | def apply[T: ClassTag](xs: T*): Array[T] = {
|
197 |
| - val array = new Array[T](xs.length) |
198 |
| - var i = 0 |
199 |
| - for (x <- xs.iterator) { array(i) = x; i += 1 } |
200 |
| - array |
| 198 | + val len = xs.length |
| 199 | + xs match { |
| 200 | + case wa: mutable.WrappedArray[_] if wa.elemTag == classTag[T] => |
| 201 | + // We get here in test/files/run/sd760a.scala, `Array[T](t)` for |
| 202 | + // a specialized type parameter `T`. While we still pay for two |
| 203 | + // copies of the array it is better than before when we also boxed |
| 204 | + // each element when populating the result. |
| 205 | + ScalaRunTime.array_clone(wa.array).asInstanceOf[Array[T]] |
| 206 | + case _ => |
| 207 | + val array = new Array[T](len) |
| 208 | + var i = 0 |
| 209 | + for (x <- xs.iterator) { array(i) = x; i += 1 } |
| 210 | + array |
| 211 | + } |
201 | 212 | }
|
202 | 213 |
|
203 | 214 | /** Creates an array of `Boolean` objects */
|
|
0 commit comments