On Tue, 26 Aug 2008, luben wrote:

I have noticed that Rakudo (and NQP) generates different PIR code for implicit and explicit returns.

Example for implicit return:

sub foo($n){
  $n;
}

And example for explicit return:

sub foo($n){
  return $n;
}


Is this on purpose? The implicit return is 4-5 times faster than explicit return.

the implicit return is by definition always at the end of a sub and therefore emits a PIR .return(). it's fast and easy.

the explicit return, in contrast, could be anywhere in a subroutine, including loops, closures, etc. these constructs are also implemented using parrot subs, so using .return() would return control to the sub that *contains* the loop, closure, or whatever construct you're using. that's not what we want.

to get around this, an exception is thrown and caught by the sub that *should* do the actual returning. you can see that in the PIR you generate.

(pmichaud, i hope i explained this right!  ;-)

-jeff

Reply via email to