in reply to Re^2: Iterative vs Recursive Processes
in thread Iterative vs Recursive Processes

Good point.

However even so I would argue against turning it on naively. You see in Perl part of cleaning up a call-frame is destroying temporary lexical variables and undoing invocations of local. When the things destroyed are objects that may have side-effects in their DESTROY methods (see ReleaseAction for an example where this happens) then trying to optimize tail-recursion creates a significant change in what the code does.

Which brings us to another theme. A lot of the theory folks argue for side-effect free programming (this is what "functional programming" really means - as opposed to the bad meme that I started) because (among other things) it gives the compiler far greater opportunity to optimize and reorder how things happen. (Proponents argue that it also makes big programs easier to understand.) However we naive humans find avoiding side-effects a difficult discipline, which makes a lot of theoretical optimizations very hard to apply in practical languages.

As Larry Wall said, optimization is a form of cheating, which is why you always get caught. And the more free-form the language is, the more requirements you put on the compiler, and the easier it is to catch it out when you try to cheat. :-)

  • Comment on Re: Re^2: Iterative vs Recursive Processes

Replies are listed 'Best First'.
Re^4: Iterative vs Recursive Processes
by adrianh (Chancellor) on May 13, 2003 at 16:17 UTC
    However even so I would argue against turning it on naively.

    Oh I agree. It's not something that can be globally switched on for perl 5 (and the lousy speed for subroutine calls makes tail-recursive code less appealing anyway). I just don't think caller is a problem :-)

    When the things destroyed are objects that may have side-effects in their DESTROY methods (see ReleaseAction for an example where this happens) then trying to optimize tail-recursion creates a significant change in what the code does.

    True, but naive scope-related DESTROY code is going to be in trouble when we get proper GC in perl6 anyway :-)

      True, but naive scope-related DESTROY code is going to be in trouble when we get proper GC in perl6 anyway :-)
      True, and did I say that I was happy about this...? :-P