This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Specialization

1 reply
Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 4 days ago.
I was playing around with specialization and found that some of the corner cases don't specialize as I would expect.

For example, specialization doesn't work on parametrized methods:

  class Test1 {
    def special[@specialized T](x: T): T = x
  }

Also, specialization doesn't seem to apply to methods which don't have the specialized type in their signature:

  class Test2[@specialized T](x: T) {
    def special(): T = x
    def notSpecial(): Unit = {
      val s: T = special()
      ()
    }
  }

So, in that example, s is always boxed.

Are these bugs or just features that haven't been implemented yet?

--j

Iulian Dragos 2
Joined: 2009-02-10,
User offline. Last seen 42 years 45 weeks ago.
Re: Specialization

On Jul 3, 2009, at 10:14 AM, Jorge Ortiz wrote:
> For example, specialization doesn't work on parametrized methods:
>
> class Test1 {
> def special[@specialized T](x: T): T = x
> }

It seems the parser drops the annotation on T. Specialization should
work on parameterized methods as well. I'll look into it.

> Also, specialization doesn't seem to apply to methods which don't
> have the specialized type in their signature:
>
> class Test2[@specialized T](x: T) {
> def special(): T = x
> def notSpecial(): Unit = {
> val s: T = special()
> ()
> }
> }

That's true. We wanted to keep code duplication to a minimum, so
methods that don't mention specialized type parameters in their
signature are not specialized. Of course, code using Test2.special
need not be paremeterized itself to make use of the specialized
variant of 'special', it's just the definition of specialized
subclasses of Test2 that don't override 'notSpecial'.

Now I tend to agree with you that this behavior is unexpected. I'll
change it to generate those methods as well and check the impact on
code size.

Thanks a lot for looking into it!

iulian

--
Iulian Dragos
--
http://lamp.epfl.ch/~dragos

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland