Skip to content
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

Support tuple specialization #11114

Closed
odersky opened this issue Jan 13, 2021 · 4 comments · Fixed by #15060
Closed

Support tuple specialization #11114

odersky opened this issue Jan 13, 2021 · 4 comments · Fixed by #15060
Assignees
Milestone

Comments

@odersky
Copy link
Contributor

odersky commented Jan 13, 2021

We currently support function specialization but not tuple specialization. It would be good to support that as well, so that we are on parity with Scala 2 programs that use the features of the standard library.

Detailed description of the Semester Project

Problem

As an example, if you compile the following:

class Foo {
  val x: (Int, Int) = (1, 1)
  val y: Int = x._1
}

then decompile it using cfr, you'll see boxing/unboxing between int and Integer in Scala 3 but not in Scala 2, because Scala 2 uses the specialized class Tuple2$mcII$sp generated by the Scala 2 compiler from the @specialized annotaiton on https://github.com/scala/scala/blob/7c7890b0ba5fb5ffa896bfedcab04e1bacc12ae7/src/library/scala/Tuple2.scala#L24 . Because Scala 3 reuses the Scala 2 standard library (the scala-library jar) we don't need to support generating the specialized classes ourselves and can just assume they exist.
Note that we already have similar logic for dealing with function specialization which is more complex since users can define their own subclass of FunctionN whereas the TupleN classes are final: https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/SpecializeApplyMethods.scala / https://github.com/lampepfl/dotty/blob/master/compiler/src/dotty/tools/dotc/transform/SpecializeFunctions.scala

Solution

The solution is to develop a new miniphase in the compiler that will specialize tuples the same way SpecializeFunctions does it for functions. We can reuse the specialized tuple classes from the Scala 2 standard library as described above.

Roadmap

  • Play with Scala 2 specialized tuple classes in a standalone Scala project to get an idea of how they work.
  • In the standalone project, develop a prototype with the low-level code the tuples must compile to.
  • In the compiler, develop a new miniphase similar to SpecializeFunctions that will generate the code you previously implemented in a standalone project.
@smarter smarter added this to the 3.0.x milestone Mar 31, 2021
@LifeIsStrange
Copy link

Hi, not exactly related to this issue but the JVM will be getting state of the art support for fine grained specialization as is described in this paper - >
https://www.reddit.com/r/java/comments/m2dfb5/parametric_jvm_pdf_how_generic_specialization/

However it won't be available until >= OpenJDK 18

@anatoliykmetyuk anatoliykmetyuk modified the milestones: 3.0.x, 3.0.1-RC1 May 12, 2021
@smarter smarter modified the milestones: 3.0.1-RC1, 3.1.0 May 25, 2021
@nicolasstucki nicolasstucki modified the milestones: 3.1.0, 3.2.0 Aug 24, 2021
@anatoliykmetyuk anatoliykmetyuk added the Semester Project Good project to be done by an MSc or strong Bsc computer science student in one semester label Jan 10, 2022
@odersky
Copy link
Contributor Author

odersky commented Jan 23, 2022

Gaetan Schwartz (@gaetschwartz) would like to take this on as a Bachelor project.

@nicolasstucki
Copy link
Contributor

Hi, not exactly related to this issue but the JVM will be getting state of the art support for fine grained specialization as is described in this paper - >
https://www.reddit.com/r/java/comments/m2dfb5/parametric_jvm_pdf_how_generic_specialization/

However it won't be available until >= OpenJDK 18

Indeed. That is in part why we have not reimplemented @specialiezed.
One issue is that we also need to support older versions of the JVM.

We currently use the standard library compiled with Scala 2 to be backward compatible. That version already produces specialized implementations of tuples. We just want to generate code that takes advantage of these specialized classes given that we already have them.

@anatoliykmetyuk anatoliykmetyuk added the stat:taken This project is already worked on by a student or as part of another volunteership program label Jan 24, 2022
@anatoliykmetyuk anatoliykmetyuk removed the stat:taken This project is already worked on by a student or as part of another volunteership program label Mar 7, 2022
@anatoliykmetyuk anatoliykmetyuk removed the Semester Project Good project to be done by an MSc or strong Bsc computer science student in one semester label Mar 7, 2022
@SethTisue
Copy link
Member

(Dale and I are working on this and making good progress.)

@dwijnand dwijnand linked a pull request Apr 28, 2022 that will close this issue
@Kordyjan Kordyjan modified the milestones: 3.2.0-RC1, 3.2.0 Aug 1, 2023
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.

8 participants