|
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 |
|
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! |
|
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 |
|
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 |
|
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:
|
|
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 |
| Powered by Nabble | Edit this page |
