Skip to content

Commit

Permalink
Merge pull request #779 from refried/eval-defer-heap-safety
Browse files Browse the repository at this point in the history
factor out a static inner method from #769 for better GC
  • Loading branch information
adelbertc committed Jan 10, 2016
2 parents 0529841 + 2a6e52a commit a64cd76
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions core/src/main/scala/cats/Eval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,21 @@ object Eval extends EvalInstances {
*/
sealed abstract class Call[A](val thunk: () => Eval[A]) extends Eval[A] {
def memoize: Eval[A] = new Later(() => value)
def value: A = {
def loop(fa: Eval[A]): Eval[A] = fa match {
case call: Eval.Call[A] =>
loop(call.thunk())
case compute: Eval.Compute[A] =>
new Eval.Compute[A] {
type Start = compute.Start
val start: () => Eval[Start] = () => compute.start()
val run: Start => Eval[A] = s => loop(compute.run(s))
}
case other => other
}

loop(this).value
def value: A = Call.loop(this).value
}

object Call {
/** Collapse the call stack for eager evaluations */
private def loop[A](fa: Eval[A]): Eval[A] = fa match {
case call: Eval.Call[A] =>
loop(call.thunk())
case compute: Eval.Compute[A] =>
new Eval.Compute[A] {
type Start = compute.Start
val start: () => Eval[Start] = () => compute.start()
val run: Start => Eval[A] = s => loop(compute.run(s))
}
case other => other
}
}

Expand Down

0 comments on commit a64cd76

Please sign in to comment.