- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
[continuation plugin] Can I make a T to T @cps[Unit] ?
Thu, 2012-01-12, 18:15
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
Thu, 2012-01-12, 19:11
#2
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
>
>
Thu, 2012-01-12, 19:31
#3
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.
But still 100x slower than the normal way.
Thu, 2012-01-12, 19:31
#4
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
Well!
The StackOverflow disappeared too.
Thank you guys.
IL
The StackOverflow disappeared too.
Thank you guys.
IL
Thu, 2012-01-12, 19:51
#5
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
It will be better if ControlContext support unboxing primitive type like Array.
Thu, 2012-01-12, 20:01
#6
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@gmail.com> wrote:
Tiark - Do you think that would work "out of the box" ?
On Thu, Jan 12, 2012 at 1:36 PM, IL <iron9light@gmail.com> wrote:
It will be better if ControlContext support unboxing primitive type like Array.
Fri, 2012-01-13, 15:11
#7
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:
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@gmail.com> wrote:
It will be better if ControlContext support unboxing primitive type like Array.
Fri, 2012-01-13, 17:41
#8
Re: [continuation plugin] Can I make a T to T @cps[Unit] ?
A only.
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@gmail.com> wrote: