in reply to Sorting, recursion, and tail-call optimizations

Forgive me if you already knew this, but simply removing the word "goto" allows it to execute properly. I suggest we start exploring that path.

Update:

Okay, this might be something here...

From "perldoc -f goto":
The "goto-&NAME" form is quite different from the other forms
of "goto".  In fact, it isn't a goto in the normal sense at
all, and doesn't have the stigma associated with other gotos.
Instead, it exits the current subroutine.
Now from "perldoc -f sort":
You also cannot exit out of the sort block or subroutine using
any of the loop control operators described in perlsyn or with
"goto".
  • Comment on Re: Sorting, recursion, and tail-call optimizations

Replies are listed 'Best First'.
Re^2: Sorting, recursion, and tail-call optimizations
by Limbic~Region (Chancellor) on Jan 06, 2006 at 18:48 UTC
    kwaping,
    I suggest we start exploring that path.

    That is the whole point of the question. I am using the special form of goto for an optimization. I am wondering why it breaks. I am even more interesting in why changing it from one sort prototype to a different sort prototype, it starts working. In case you were wondering, the prototypes I am referring to are for sort and not the user-defined comparator sub you referred to elsewhere in this thread.

    With regards to your update. I think that is referring to goto label where you are actually trying to exit the routine. Using the special goto &sub you are immediately resuming execution after replacing the current call stack. At least that's my understanding of it. If that wasn't the case then neither example should work. At this point, I throw my hands up.

    Cheers - L~R

      It's not particularly special, and it doesn't optimize. I'll explain:
      When you use goto &sub, you are doing a real goto. You're not calling a sub, you're just identifying a location. So the sub you leave never returns. Instead, the sub you went to returns.

      The only optimization that buys you, relative to calling &sub, is that you don't have multiple returns. In my testing, I found that the use of goto more than offsets those gains.


      Caution: Contents may have been coded under pressure.
        Roy Johnson,
        It is a shame that Perl doesn't benefit from this optimization then. In other languages, the optimization would involve re-using the same stack-frame with no pushing and popping required. In addition to the thread I already mentioned, this external article was also interesting.

        Cheers - L~R

      At this point, I throw my hands up.

      Me too, lol.