in reply to Re: Sorting, recursion, and tail-call optimizations
in thread Sorting, recursion, and tail-call optimizations

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

  • Comment on Re^2: Sorting, recursion, and tail-call optimizations

Replies are listed 'Best First'.
Re^3: Sorting, recursion, and tail-call optimizations
by Roy Johnson (Monsignor) on Jan 06, 2006 at 19:37 UTC
    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

Re^3: Sorting, recursion, and tail-call optimizations
by kwaping (Priest) on Jan 06, 2006 at 19:02 UTC
    At this point, I throw my hands up.

    Me too, lol.