« Return to Thread: Tail calls via trampolining and an explicit instruction

Re: Tail calls via trampolining and an explicit instruction

by csar :: Rate this Message:

Reply to Author | View in Thread

I agree, slower is better than stack-overflow.

Compare
(defn recount [n]
  (if (zero? n) nil
    (recount (dec n))))

with
(defn recount [n]
  (if (zero? n) nil
    (recur (dec n))))

The later is as well faster, thus I am asking if trampolines are a workaround rather than a solution.

/C

On Wed, Nov 26, 2008 at 6:00 PM, Randall R Schulz <rschulz@...> wrote:
On Wednesday 26 November 2008 08:48, Carsten Saager wrote:
> Impressive:
>
> user=> (time (trampoline #(foo 1000)) )
> "Elapsed time: 0.547556 msecs"
>
> user=> (time (foo 1000))
> "Elapsed time: 0.375466 msecs"
>
> Looks like the penalty is not that high, but after the JIT kicked in
> the trampolined version takes twice as long as the recusive version -
> hard to call this a TCO when the tail-call takes actually longer.
> ...

The point is not to optimize CPU time, but to keep stack consumption
independent of the input size in mutually recursive sets of function
and to do so using the stock JVM's ordinary call stack.


> /Carsten
>
> On Wed, Nov 26, 2008 at 4:08 PM, James Iry <jamesiry@...> wrote:
> > http://groups.google.com/group/clojure/msg/3addf875319c5c10


Randall Schulz

 « Return to Thread: Tail calls via trampolining and an explicit instruction