Skip to content
This repository
Browse code

Only add the return exception handler when there's a return statement…

… in a routine. On Parrot, adding one to a sub that doesn't use it makes the invocation over 3 times more costly; since EXPR and so forth are now wrapped in NQP subs, we really need to save on this.
  • Loading branch information...
commit 910fcac241d39254da0a5e9453cfc54c852bb3f2 1 parent 125d4ab
Jonathan Worthington authored January 23, 2011
12  src/NQP/Actions.pm
@@ -206,10 +206,6 @@ method statement_control:sym<for>($/) {
206 206
     make $past;
207 207
 }
208 208
 
209  
-method statement_control:sym<return>($/) {
210  
-    make PAST::Op.new( $<EXPR>.ast, :pasttype('return'), :node($/) );
211  
-}
212  
-
213 209
 method statement_control:sym<CATCH>($/) {
214 210
     my $block := $<block>.ast;
215 211
     push_block_handler($/, $block);
@@ -553,7 +549,9 @@ method routine_def($/) {
553 549
     else {
554 550
         $past := $<blockoid>.ast;
555 551
         $past.blocktype('declaration');
556  
-        $past.control('return_pir');
  552
+        if $*RETURN_USED {
  553
+            $past.control('return_pir');
  554
+        }
557 555
     }
558 556
     my $block := $past;
559 557
 
@@ -676,7 +674,9 @@ method method_def($/) {
676 674
     else {
677 675
         $past := $<blockoid>.ast;
678 676
         $past.blocktype('declaration');
679  
-        $past.control('return_pir');
  677
+        if $*RETURN_USED {
  678
+            $past.control('return_pir');
  679
+        }
680 680
     }
681 681
 
682 682
     # Always need an invocant.
9  src/NQP/Grammar.pm
@@ -11,9 +11,9 @@ method TOP() {
11 11
     # Package declarator to meta-package mapping.
12 12
     my %*HOW;
13 13
     %*HOW<knowhow> := 'KnowHOW';
14  
-    %*HOW<class> := 'NQPClassHOW';
  14
+    %*HOW<class>   := 'NQPClassHOW';
15 15
     %*HOW<grammar> := 'NQPClassHOW';
16  
-    %*HOW<role> := 'NQPRoleHOW';
  16
+    %*HOW<role>    := 'NQPRoleHOW';
17 17
 
18 18
     # What attribute class to use with what HOW, plus a default.
19 19
     my $*DEFAULT-METAATTR := 'NQPAttribute';
@@ -23,6 +23,7 @@ method TOP() {
23 23
     my $*SCOPE       := '';
24 24
     my $*MULTINESS   := '';
25 25
     my $*INVOCANT_OK := 0;
  26
+    my $*RETURN_USED := 0;
26 27
     my $*PACKAGE-SETUP;
27 28
     self.comp_unit;
28 29
 }
@@ -333,6 +334,7 @@ token routine_declarator:sym<sub>    { <sym> <routine_def> }
333 334
 token routine_declarator:sym<method> { <sym> <method_def> }
334 335
 
335 336
 rule routine_def {
  337
+    :my $*RETURN_USED := 0;
336 338
     [ $<sigil>=['&'?]<deflongname> ]?
337 339
     <.newpad>
338 340
     [ '(' <signature> ')'
@@ -345,6 +347,7 @@ rule routine_def {
345 347
 }
346 348
 
347 349
 rule method_def {
  350
+    :my $*RETURN_USED := 0;
348 351
     :my $*INVOCANT_OK := 1;
349 352
     $<private>=['!'?]
350 353
     <deflongname>?
@@ -627,7 +630,7 @@ token infix:sym<::=>  { <sym>  <O('%assignment, :pasttype<bind>')> }
627 630
 
628 631
 token infix:sym<,>    { <sym>  <O('%comma, :pasttype<list>')> }
629 632
 
630  
-token prefix:sym<return> { <sym> \s <O('%list_prefix, :pasttype<return>')> }
  633
+token prefix:sym<return> { <sym> \s <O('%list_prefix, :pasttype<return>')> { $*RETURN_USED := 1 } }
631 634
 token prefix:sym<make>   { <sym> \s <O('%list_prefix')> }
632 635
 token term:sym<last>     { <sym> }
633 636
 token term:sym<next>     { <sym> }

0 notes on commit 910fcac

Please sign in to comment.
Something went wrong with that request. Please try again.