- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
[continuation plugin] Can I make a T to T @cps[Unit] ?
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
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










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
>
>
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
But still 100x slower than the normal way.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
The StackOverflow disappeared too.
Thank you guys.
IL
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
It will be better if ControlContext support unboxing primitive type like Array.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
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:
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
Cheers,- Tiark
On Jan 12, 2012, at 7:49 PM, Josh Suereth wrote:
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
A only.
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
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: