Skip to content

Dotty unnecessarily wraps arrays used in place of java repeated #3675

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
allanrenucci opened this issue Dec 16, 2017 · 3 comments
Closed

Dotty unnecessarily wraps arrays used in place of java repeated #3675

allanrenucci opened this issue Dec 16, 2017 · 3 comments

Comments

@allanrenucci
Copy link
Contributor

import java.nio.file._
class Test {
  def test(xs: Array[String]) = {
    val p4 = Paths.get("Hello", xs: _*)
  }
}

The code above is compiled by Dotty to:

public class Test {
    public void test(String[] xs) {
        Path p4 = Paths.get("Hello", (String[])Arrays$.MODULE$.seqToArray((Seq)Predef$.MODULE$.wrapRefArray((Object[])xs), String.class));
    }
}

And scalac:

public class Test {
    public void test(String[] xs) {
        Path p4 = Paths.get("Hello", xs);
    }
}
@allanrenucci
Copy link
Contributor Author

This can be implemented as an optimisation in ElimRepeated.

@vitorsvieira
Copy link
Contributor

Hi Allan,

I'd like to work on it.

-V

@allanrenucci
Copy link
Contributor Author

Sure! The code you need to change is: https://github.com/lampepfl/dotty/blob/c515e7e6eb93bbfe491596c3d2cbe1672685ad60/compiler/src/dotty/tools/dotc/transform/ElimRepeated.scala#L94
If tree is a method call to Predef.wrap{Ref, Int, Double...}Array, you need to replace the call by its argument

abeln pushed a commit to abeln/dotty that referenced this issue Jan 18, 2018
… varargs

When eliminating varargs in a java method call, we need to convert Seqs
into Arrays. However, if the object we're passing is already a wrapped
array, we just need to unwrap it.

Testing
=======

Eliminating varargs in

import java.nio.file._
class Test {
  def test(xs: Array[String]) = {
     val p4 = Paths.get("Hello", xs: _*)
  }
}

now gives

val p4: java.nio.file.Path = java.nio.file.Paths.get("Hello", xs)
abeln pushed a commit to abeln/dotty that referenced this issue Jan 18, 2018
… varargs

When eliminating varargs in a java method call, we need to convert Seqs
into Arrays. However, if the object we're passing is already a wrapped
array, we just need to unwrap it.

Testing
-------

Eliminating varargs in

```
import java.nio.file._
class Test {
  def test(xs: Array[String]) = {
     val p4 = Paths.get("Hello", xs: _*)
  }
}
```

now gives

`val p4: java.nio.file.Path = java.nio.file.Paths.get("Hello", xs)`
allanrenucci added a commit that referenced this issue Jan 23, 2018
Fix #3675: don't unnecessarily wrap arrays when eliminating java varargs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants