develooper Front page | perl.perl5.porters | Postings from January 2008

Re: For 5.12: tail calls for methods

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
January 18, 2008 07:26
Subject:
Re: For 5.12: tail calls for methods
Message ID:
20080118152619.GX23703@plum.flirble.org
On Fri, Jan 18, 2008 at 10:15:58AM -0500, Stevan Little wrote:
> 
> On Jan 18, 2008, at 6:53 AM, Dave Mitchell wrote:
> 
> >On Thu, Jan 17, 2008 at 01:46:50PM -0500, Stevan Little wrote:
> >>>And how do you want to detect that?
> >>
> >>Simple, if the last call is to the same function, then it tail- 
> >>recurses.
> >
> >That implies adding an extra test into every sub call, which we try to
> >make as fast as possible.
> 
> Actually I would suspect this is something that can be flagged during  
> the compilation of the sub. Of course I know little about perl-guts  
> so I may be totally off here, but it seems to me that is /should/ be  
> possible anyway.

It still involves adding a C level if() check and a bit more code, which is
another straw on the pp_entersub camel's back.

Also, I'm not certain if it is "correct" to do this automatically, given:

$ cat ~/test/redefine.pl
#!perl -wl

sub fib {
    my $val = shift;
    return 1 unless $val;
    return fib($val - 1)
}

print fib (3);

my $orig = *fib{CODE};

*fib = sub {
    warn "Wrapper called with $_[0]";
    goto &$orig;
};

print fib (3);
__END__
$ perl ~/test/redefine.pl 
1
Subroutine main::fib redefined at /home/nick/test/redefine.pl line 16.
Wrapper called with 3 at /home/nick/test/redefine.pl line 14.
Wrapper called with 2 at /home/nick/test/redefine.pl line 14.
Wrapper called with 1 at /home/nick/test/redefine.pl line 14.
Wrapper called with 0 at /home/nick/test/redefine.pl line 14.
1


I believe that attempting to automatically generate tail calls would change
this behaviour, which (at least) some people thing is desirable in the
general case.

It feels somewhat like the arms race between overloading or tied values, or
inside out objects, and the code that tries to introspect them.

Nicholas Clark

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About