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

feature req: specialized singleton types and valueOf #11489

Closed
lJoublanc opened this issue Apr 16, 2019 · 12 comments
Closed

feature req: specialized singleton types and valueOf #11489

lJoublanc opened this issue Apr 16, 2019 · 12 comments

Comments

@lJoublanc
Copy link

Using 2.13-M5, it's not possible to specialize singleton types.

scala> def twice[@scala.specialized(Int) I <: Int : ValueOf]: Int = valueOf[I] * 2
           ^
       warning: type I is unused or used in non-specializable positions.
twice: [I <: Int](implicit evidence$1: ValueOf[I])Int

I think this is because ValueOf is not specialized.
In my program I can see lots of boxing in a heap trace.
It would be nice if this could indeed be specialized for the primitive types such as Int, Double etc.

@SethTisue
Copy link
Member

SethTisue commented May 28, 2019

labeled as "good first issue" not because I'm already confident it's easily solvable, but because a newcomer could at least investigate

@tiarebalbi
Copy link

Does it make sense to have a new type match on SpecializeTypes.specializedTypeVarsBuffer() to handle Int, Double and etc.?

@chenghsienwen
Copy link

@SethTisue :I am new to handle this issue, I tried to add specialized annotation to ValueOf class, but failed, could you give me some hint about this, thank you

@SethTisue
Copy link
Member

@chenghsienwen where did you get stuck? what changes did you make, what was the resulting error message or unexpected behavior...?

@chenghsienwen
Copy link

@SethTisue
sorry for late reply

what changes did you make:
add @specialized in ValueOf.scala
final class ValueOf[T](@specialized val value: T) extends AnyVal
what was the resulting error message:it can be compiled and can run by ./build/pack/bin/scala
but issue still exists

@SethTisue
Copy link
Member

SethTisue commented Jul 19, 2019

@chenghsienwen okay, so we've learned that it's not as simple as just adding the annotation.

have you investigated any further? do you have any hypothesis about why you're still getting that error message, or what further changes might be needed...? if you want to pursue this further, you'll need to dig deeper.

@chenghsienwen
Copy link

Thank you for advise, I will let it be and proceed others good first issue

@jxnu-liguobin
Copy link
Member

I have conducted some tests on this, hoping to advance the problem. I'm a novice. There may be some inaccuracies.

https://github.com/scala/scala/blob/6e5187e8bca5e0ff57f60efacba48be06cefde27/src/compiler/scala/tools/nsc/transform/SpecializeTypes.scala#L509

def twice[I <: Int](implicit evidence$1: ValueOf[I]): Int
It seems that the specialization of ValueOf[I] is not handled correctly.

So it will true when I is specialized, but now, is false because tp is currently judging the T type parameter of ValueOf.

image

Obviously, ValueOf is a value type and it is impossible to add @specialized to it.
Note: the annotation in value param is invalid, like this: final class ValueOf[T](@specialized() val value: T) extends AnyVal

As above, the constructor's parameters are not used to handle this. Only type parameters were used.

Change to the following, feasible?

if (tp.isSpecialized || arg.typeSymbol.isSpecialized) ....

@SethTisue
Copy link
Member

it's been so long since significant work has been done on specialization that there may not be anybody available who can answer your question. perhaps PR it and let's see if the tests pass?

@jxnu-liguobin
Copy link
Member

it's been so long since significant work has been done on specialization that there may not be anybody available who can answer your question. perhaps PR it and let's see if the tests pass?

Yes. I have this plan.

jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Oct 13, 2021
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Nov 13, 2021
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Sep 27, 2022
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Sep 27, 2022
jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Oct 27, 2022
@SethTisue SethTisue modified the milestones: Backlog, 2.13.11 Oct 27, 2022
@SethTisue
Copy link
Member

SethTisue commented Oct 27, 2022

I see this ticket got a number of thumbs up. It would be helpful if y'all (and/or @lJoublanc himself) could try the PR out and see if it works with whatever use cases you had in mind: scala/scala#10165

The PR validation snapshot is testable with:

resolvers += "pr" at "https://scala-ci.typesafe.com/artifactory/scala-pr-validation-snapshots/"
scalaVersion := "2.13.10-bin-3e42beb-SNAPSHOT"

jxnu-liguobin added a commit to jxnu-liguobin/scala that referenced this issue Oct 27, 2022
The constraint type of the type parameter supports specialization, and there are `specialized` annotation. It should support specialization.

scala/bug#11489
@jxnu-liguobin
Copy link
Member

@SethTisue Although the PR has been submitted and passed the test, after careful investigation, it is regrettable to explain again that since the type parameter of ValueOf cannot be added with specialization annotations, the generation of specialization methods alone cannot reduce unpacking.

 NO PR: jvm code
def twice(implicit evidence$1: Integer): Int = scala.Int.unbox(scala.Predef.valueOf(evidence$1)).*(2); 
After PR:  jvm code
<specialized> def twice$mIc$sp(evidence$1: Integer): Int = scala.Int.unbox(scala.Predef.valueOf(evidence$1)).*(2); 

On the other hand, in order to reduce unpacking, there are two ways: modify the valueOf method or rewrite the implicit evidence$1 parameter. I am not familiar with the second one. The first way:

// But this requires modifying the standard library,
@inline def valueOf[@specialized T](implicit vt: ValueOf[T]): T = vt.value

Then we will get :

    <specialized> def twice$mIc$sp(evidence$1: Integer): Int = scala.Predef.valueOf$mIc$sp(evidence$1).*(2);

After adding @specialized on valueOf method , unittest failed, Caused by: java.lang.RuntimeException: error reading Scala signature of scala.package: error reading Scala signature of scala.collection.package: error reading Scala signature of scala.collection.Iterable: error reading Scal
a signature of scala.Predef: unsafe symbol Nothing (child of package scala) in runtime reflection universe

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

5 participants