Skip to content

Array[Double](1) not optimized by compiler #12201

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

Closed
OndrejSpanel opened this issue Oct 22, 2020 · 3 comments · Fixed by scala/scala#9544
Closed

Array[Double](1) not optimized by compiler #12201

OndrejSpanel opened this issue Oct 22, 2020 · 3 comments · Fixed by scala/scala#9544

Comments

@OndrejSpanel
Copy link
Member

OndrejSpanel commented Oct 22, 2020

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

@SethTisue
Copy link
Member

("good first issue" is speculative. a newcomer could investigate, and it's plausible that an easy solution exists, given that the other forms get optimized as expected)

@jxnu-liguobin
Copy link
Member

Hello @SethTisue , what knowledge need to solve this problem? Where is the related code ?

@SethTisue
Copy link
Member

As I commented on the other ticket you asked about, my “You Are a Scala Contributor” video and/or slides are good for getting oriented, as are the README and CONTRIBUTING files in scala/scala.

You should already be very comfortable coding in Scala. And you should only attempt this bug if you want to learn something about Scala compiler internals, starting with https://docs.scala-lang.org/overviews/reflection/symbols-trees-types.html . This is a "good first issue" (we suspect, we hope) for those who have that desire. (Having studied other compilers isn't a prerequisite.)

The optimization in question is implemented at https://github.com/scala/scala/blob/2.13.x/src/compiler/scala/tools/nsc/transform/CleanUp.scala#L595-L609

A few recent PRs closely related to this: scala/scala#9356, scala/scala#9461

jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Mar 18, 2021
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Mar 19, 2021
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Mar 19, 2021
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Mar 19, 2021
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Mar 19, 2021
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Mar 19, 2021
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Mar 24, 2021
@SethTisue SethTisue modified the milestones: Backlog, 2.13.6 Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants