Closed
Description
Array is currently optimized by compiler.
Originally posted by @texasbruce in #6247 (comment)
Recently I have hit an issue in a real application - I was surprised to see Double boxing in it. Reduced to the bare minimum the following code produces an array construction which is not optimized:
var el = Array[Double](1)
var el = Array[Double](1.0)
Both produce the following byte code:
0: aload_0
1: invokespecial #27 // Method java/lang/Object."<init>":()V
4: aload_0
5: getstatic #33 // Field scala/Array$.MODULE$:Lscala/Array$;
8: getstatic #38 // Field scala/runtime/ScalaRunTime$.MODULE$:Lscala/runtime/ScalaRunTime$;
11: iconst_1
12: newarray double
14: dup
15: iconst_0
16: dconst_1
17: dastore
18: invokevirtual #42 // Method scala/runtime/ScalaRunTime$.wrapDoubleArray:([D)Lscala/collection/immutable/ArraySeq;
21: getstatic #47 // Field scala/reflect/ClassTag$.MODULE$:Lscala/reflect/ClassTag$;
24: invokevirtual #51 // Method scala/reflect/ClassTag$.Double:()Lscala/reflect/ManifestFactory$DoubleManifest;
27: invokevirtual #55 // Method scala/Array$.apply:(Lscala/collection/immutable/Seq;Lscala/reflect/ClassTag;)Ljava/lang/Object;
30: checkcast #56 // class "[D"
33: putfield #18 // Field el:[D
36: return
A slightly different variants are optimized as expected:
var el = Array(1.0)
var el: Array[Double] = Array(1)
See also https://stackoverflow.com/q/64482385/16673
I see the issue in both Scala 2.12.12 and 2.13.3. I do not see the issue in Dotty 0.27