Have you ever enviously eyed Scheme's tail recursion optimization?
No, neither have I. I just write iterative code.

But for the curious, something like it can be done using

goto &sub
although whether it is an optimization is debatable.

More code and info in my response

use Carp qw(cluck); my $cluck = 0; sub _f_tail_rec { if($_[0] == 0) { cluck "Goto optimized stack trace" if $cluck; return $_[1] } $_[1] *= $_[0]; $_[0]--; goto &_f_tail_rec; } sub f_tail_rec { @_ = (@_, 1); goto &_f_tail_rec;} my $n = 10; print "f_tail_rec($n) = ".f_tail_rec($n),"\n";

Replies are listed 'Best First'.
Re: Tail Recursion "Optimising" with goto &sub
by lestrrat (Deacon) on Feb 21, 2003 at 07:03 UTC

    Here's a quickie benchmark, run on perl5.8.0, Athlon XP 1800 linux box with 256MB of memory:

    Benchmark: timing 10000 iterations of goto, iterative, regular... goto: 19 wallclock secs (18.92 usr + 0.00 sys = 18.92 CPU) @ 52 +8.54/s (n=10000) iterative: 6 wallclock secs ( 6.23 usr + 0.00 sys = 6.23 CPU) @ 16 +05.14/s (n=10000) regular: 23 wallclock secs (22.94 usr + 0.00 sys = 22.94 CPU) @ 43 +5.92/s (n=10000) Rate regular goto iterative regular 436/s -- -18% -73% goto 529/s 21% -- -67% iterative 1605/s 268% 204% --

    ...And here's the code. My apologies if I made mistakes, I didn't spend that much time on this ;)

    use strict; use Benchmark qw/ cmpthese /; cmpthese( 10000, { regular => sub{ regrecurse($_) for ( map{10*$_} (1..10) ) }, goto => sub{ gotorecurse($_) for ( map{10*$_} (1..10) ) }, iterative => sub{ iterative($_) for ( map{ 10* $_ } (1..10) ) +} }, ); sub iterative { my $n = shift; my $m = 1; for(1..$n) { $m *= $_; } return $m; } sub regrecurse { my $n = shift; _reghelp(1, $n); } sub _reghelp { my($n, $m) = @_; if($m <= 1) { return $n; } return _reghelp($n * $m, $m - 1); } sub gotorecurse { my $n = shift; @_ = (1, $n); goto &_gotohelp; } sub _gotohelp { if($_[1] <= 1) { return $_[0]; } $_[0] *= $_[1]; $_[1]--; goto &_gotohelp; }
Re: Tail Recursion "Optimising" with goto &sub
by broquaint (Abbot) on Feb 21, 2003 at 10:59 UTC
Re: Tail Recursion "Optimising" with goto &sub
by bsb (Priest) on Feb 21, 2003 at 02:59 UTC
    This idea had been brewing a while when I found a wiki page saying that it couldn't be done in perl.

    Now couldn't can be changed to wouldn't.

    Note that since the subs use the same @_ the guardian sub needs to copy it. Then the values can be modified.

    For more code and avoidance of the recursion warning, readmore

Re: Tail Recursion "Optimising" with goto &sub
by Zaxo (Archbishop) on Feb 21, 2003 at 03:04 UTC

    This is a pretty elaborate way to do it, but it's good to see a demonstration of perl's unapologetic form of goto outside AUTOLOAD subs.

    Have you benchmarked this against a straight iterative implementation?

    After Compline,
    Zaxo

      No, I haven't benchmarked it yet.

      I'm curious about the results vs normal recursion but surely the straight iterative version would be faster.

      Only if you were using a very functional style would this potentially be a useful place to look for speed, but in that case you'd probably want to do it in the compiler/optimizer rather than by hand. (Waffling here...)

Re: Tail Recursion "Optimising" with goto &sub
by diotalevi (Canon) on Feb 21, 2003 at 05:13 UTC