Quantcast

Try out specialized Function1/Function2 in 2.8.0 RC1!

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Try out specialized Function1/Function2 in 2.8.0 RC1!

Rex Kerr-2
For anyone who hasn't played with them yet, the Int/Long/Double specialization of Function1 and Function2 are really nice!

Now this code:

  def specificSum() = {
    var count = 0
    var i = 0
    while (i<1000000000) {
      i += 1
      count += 2*i+1
    }
    count
  }
  specificSum()

runs at the same speed as this code:

  def genericSum(f: Int => Int, s: Int => Int, c: Int => Boolean) = {
    var count = 0
    var i = 0
    while (c(i)) {
      i = s(i)
      count += f(i)
    }
    count
  }
  genericSum(i => 2*i+1, i => i+1, i => i<1000000000)

where it used to run ~130x slower.

At least for me, this opens up some very exciting new possibilities for top-notch performance in functional code.

(Of course, the JVM has to work its JIT magic also to inline the method calls to apply.)

Specialization isn't in much of the library yet, but this is a great start.  My hat is off to the Scala compiler/library team!

  --Rex

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Try out specialized Function1/Function2 in 2.8.0 RC1!

Rodrigo Cano
Will @specialize be applied to the collection api?

On Wed, Apr 14, 2010 at 7:40 PM, Rex Kerr <[hidden email]> wrote:
For anyone who hasn't played with them yet, the Int/Long/Double specialization of Function1 and Function2 are really nice!

Now this code:

  def specificSum() = {
    var count = 0
    var i = 0
    while (i<1000000000) {
      i += 1
      count += 2*i+1
    }
    count
  }
  specificSum()

runs at the same speed as this code:

  def genericSum(f: Int => Int, s: Int => Int, c: Int => Boolean) = {
    var count = 0
    var i = 0
    while (c(i)) {
      i = s(i)
      count += f(i)
    }
    count
  }
  genericSum(i => 2*i+1, i => i+1, i => i<1000000000)

where it used to run ~130x slower.

At least for me, this opens up some very exciting new possibilities for top-notch performance in functional code.

(Of course, the JVM has to work its JIT magic also to inline the method calls to apply.)

Specialization isn't in much of the library yet, but this is a great start.  My hat is off to the Scala compiler/library team!

  --Rex


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Try out specialized Function1/Function2 in 2.8.0 RC1!

Martin Odersky


On Thu, Apr 15, 2010 at 3:02 PM, Rodrigo Cano <[hidden email]> wrote:
Will @specialize be applied to the collection api?


Yes, bit by bit. It will take some time but we'll get there.

Cheers

 -- Martin

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Try out specialized Function1/Function2 in 2.8.0 RC1!

Silvio Bierman
Is the current state of the @specialize implementation able to optimize for-comprehensions on Range[Int] so we no longer need to replace them with while-loops in performance critical places?

Gr. Silvio
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Try out specialized Function1/Function2 in 2.8.0 RC1!

Rex Kerr-2
Range isn't specialized yet.  And even when it is, while loops will still probably have an advantage--for loops are going to have to be polymorphic, which will prevent the JVM from fully optimizing a "for" to the level of a "while" (there will very likely be one or two function calls that can't be inlined).  But that's still an order of magnitude better than having to box primitives, and for many applications where one now has to use a while, that should be enough (if all the implementation details work out favorably, which is not yet assured).

  --Rex



On Mon, Apr 26, 2010 at 5:43 PM, Silvio Bierman <[hidden email]> wrote:

Is the current state of the @specialize implementation able to optimize
for-comprehensions on Range[Int] so we no longer need to replace them with
while-loops in performance critical places?

Gr. Silvio

--
View this message in context: http://scala-programming-language.1934581.n4.nabble.com/Try-out-specialized-Function1-Function2-in-2-8-0-RC1-tp1957121p2065869.html
Sent from the Scala - User mailing list archive at Nabble.com.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Try out specialized Function1/Function2 in 2.8.0 RC1!

Silvio Bierman
Thanks for the answer Rex,

I don't mind loosing some performance to gain code clarity. Just as long as the difference is not so dramatic that the loop construct influences total performance more than the body of the loop (as it did with a small piece of genetic algorithm code I ported from Java to Scala recently).

Gr. Silvio
Loading...