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

[continuation plugin] Can I make a T to T @cps[Unit] ?

8 replies
iron9light
Joined: 2009-07-04,
User offline. Last seen 3 years 15 weeks ago.
I have a method
def foo[T]: T@cps[Unit]
but sometimes it just return T. (I get the result form cache, so I do not need to call some cps method)
I use this way to convert T to T@cps[Unit]
def bar[T](x: T) = shift[T, Unit, Unit](_(x))
But this will add some performance overhead.
Is there a better way to tell the compiler not do the cps transform at this run-time condition? (Which means there are two branch of the same logic)
Best regards,
IL

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
Unfortunately, no.
T @cps[Unit] is shorthand for ControlContext[T,Unit,Unit], so although "bar" looks like it isn't doing anything, it *is* creating a new control context.
IIRC, there is an optimisation for "simple" values (such as taking T to T @cps[Unit]) such that the runtime overhead is just an additional object reference, not a closure.
(See http://www.scala-lang.org/api/current/index.html#scala.util.continuations.ControlContext and the isTrivial method).
I think what you want is "shiftUnit" (see http://www.scala-lang.org/api/current/index.html#scala.util.continuations.package)
so... def bar[T](t: T): T @cps[Unit] = shiftUnit(t)
That *should* take the "isTrivial" optimisation path, if my memory serves.

- Josh

On Thu, Jan 12, 2012 at 12:15 PM, IL <iron9light [at] gmail [dot] com> wrote:
I have a method
def foo[T]: T@cps[Unit]
but sometimes it just return T. (I get the result form cache, so I do not need to call some cps method)
I use this way to convert T to T@cps[Unit]
def bar[T](x: T) = shift[T, Unit, Unit](_(x))
But this will add some performance overhead.
Is there a better way to tell the compiler not do the cps transform at this run-time condition? (Which means there are two branch of the same logic)
Best regards,
IL


Tiark Rompf
Joined: 2009-02-18,
User offline. Last seen 42 years 45 weeks ago.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?

def bar[T](x: T): T @cps[Unit] = x

The compiler will insert an implicit shiftUnit(..) around x, which is more efficient than shift(k => k(x)).

There will still be some overhead, though, as the compiler cannot know about runtime conditions and both bar and its callers need to follow the cps calling convention.

Cheers,
- Tiark

On Jan 12, 2012, at 6:15 PM, IL wrote:

> I have a method
>
> def foo[T]: T@cps[Unit]
>
> but sometimes it just return T. (I get the result form cache, so I do not need to call some cps method)
>
> I use this way to convert T to T@cps[Unit]
>
> def bar[T](x: T) = shift[T, Unit, Unit](_(x))
>
> But this will add some performance overhead.
>
> Is there a better way to tell the compiler not do the cps transform at this run-time condition? (Which means there are two branch of the same logic)
>
> Best regards,
>
> IL
>
>

iron9light
Joined: 2009-07-04,
User offline. Last seen 3 years 15 weeks ago.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
Cool! nearly 10x speed about form shift -> shiftUnit
But still 100x slower than the normal way.
iron9light
Joined: 2009-07-04,
User offline. Last seen 3 years 15 weeks ago.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
Well!
The StackOverflow disappeared too.
Thank you guys.
IL
iron9light
Joined: 2009-07-04,
User offline. Last seen 3 years 15 weeks ago.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?

It will be better if ControlContext support unboxing primitive type like Array.

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
You mean via a @specialized parameter?   
Tiark - Do you think that would work "out of the box" ?

On Thu, Jan 12, 2012 at 1:36 PM, IL <iron9light [at] gmail [dot] com> wrote:
It will be better if ControlContext support unboxing primitive type like Array.

Tiark Rompf
Joined: 2009-02-18,
User offline. Last seen 42 years 45 weeks ago.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
I think it should 'work' out of the box, but it will likely take a bit of benchmarking and bytecode reviewing to get it right, i.e. make sure that all important call paths across map, flatMap etc are fully specialized. If not, performance may actually get worse. Also, code size is an issue: specializing ControlContext[A,B,C] for all of A,B,C will produce 1000 implementations so we would want to specialize on A only, or maybe specialize B,C for Unit to cover the @suspendable case.
Cheers,- Tiark
On Jan 12, 2012, at 7:49 PM, Josh Suereth wrote:
You mean via a @specialized parameter?   
Tiark - Do you think that would work "out of the box" ?

On Thu, Jan 12, 2012 at 1:36 PM, IL <iron9light [at] gmail [dot] com> wrote:
It will be better if ControlContext support unboxing primitive type like Array.


iron9light
Joined: 2009-07-04,
User offline. Last seen 3 years 15 weeks ago.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?

A only.

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