in reply to Why is goto &sub slow?

tilly posed the same question on p5p. That  goto &sub is slow is a known issue at perl5porters. And in a subsequent message , it was claimed that "the C<goto &NAME> construct is not for tail recursion, but is for fooling the C<caller> function. ". I think someone also claimed that goto LABEL does not have the speed penalty of  goto &sub. A further message notes that some memory leaks associated with  goto &sub are cleaned up in 5.8.1.

This does not answer your question: why is  goto &sub slow? I don't know a definitive answer, but I gather it takes time to rearrange the stack and clean up the lexical pad before jumping to the new routine.

-Mark

Replies are listed 'Best First'.
Re: Re: Why is goto &sub slow?
by dragonchild (Archbishop) on Mar 29, 2004 at 01:44 UTC
    That doesn't gibe. Here's the two sequences, as I understand them.

    Standard recursion:

    1. First call to func(). (Create first frame on the stack)
    2. Second call to func(). (Create second frame on the stack)
    3. Return from second call. (Destroy second frame on the stack)
    4. Return from first call. (Destory first frame on the stack)

    goto &sub;

    1. First call to func(). (Create first frame on the stack)
    2. goto &sub; (Destory first frame, create first frame)
    3. Return from goto call. (Destory first frame)

    I count two creations and two destructions of a stack frame. What am I missing? Plus, since goto &sub; doesn't have such a large stack, it takes less memory and lookups take less time. Right?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      From L-R's updated benchmark and from your benchmark, recursion is only 2-10 percent faster than goto &sub. As you say above, both recursion and goto &sub create and destroy two frames. So the results seem consistent to me. Unless you are refering to that 10 percent slowdown for goto &sub? I don't know what is happening there.

      -Mark