switch from a regex to hash test
authorSteve Hancock <perltidy@users.sourceforge.net>
Sat, 21 Jan 2023 23:37:32 +0000 (15:37 -0800)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sat, 21 Jan 2023 23:37:32 +0000 (15:37 -0800)
On my standard test case, this reduced the run time of
an if test from about 40 ms to 4 ms.

lib/Perl/Tidy/Formatter.pm

index 236d88f00e0ece61e124a36ed9a4a6f393944f89..370ef2f5e85918b098adf57e9c7a134e753e88f2 100644 (file)
@@ -477,6 +477,7 @@ BEGIN {
         _radjusted_levels_          => $i++,
         _this_batch_                => $i++,
 
+        _ris_special_identifier_token_    => $i++,
         _last_output_short_opening_token_ => $i++,
 
         _last_line_leading_type_       => $i++,
@@ -911,6 +912,7 @@ sub new {
     $self->[_this_batch_] = [];
 
     # Memory of processed text...
+    $self->[_ris_special_identifier_token_]    = {};
     $self->[_last_last_line_leading_level_]    = 0;
     $self->[_last_line_leading_level_]         = 0;
     $self->[_last_line_leading_type_]          = '#';
@@ -7684,6 +7686,9 @@ sub respace_tokens_inner_loop {
                     # one space max, and no tabs
                     $token =~ s/\s+/ /g;
                     $rtoken_vars->[_TOKEN_] = $token;
+
+                    $self->[_ris_special_identifier_token_]->{$token} = 'sub';
+
                 }
 
                 # clean up spaces in package identifiers, like
@@ -7693,6 +7698,10 @@ sub respace_tokens_inner_loop {
                 {
                     $token =~ s/\s+/ /g;
                     $rtoken_vars->[_TOKEN_] = $token;
+
+                    $self->[_ris_special_identifier_token_]->{$token} =
+                      'package';
+
                 }
 
                 # trim identifiers of trailing blanks which can occur
@@ -7864,6 +7873,7 @@ EOM
         $self->store_token($rtoken_vars);
 
     }    # End token loop
+
     return;
 } ## end sub respace_tokens_inner_loop
 
@@ -16917,30 +16927,28 @@ EOM
 
             # blank lines before subs except declarations and one-liners
             elsif ( $leading_type eq 'i' ) {
-                if (
-
-                    #FIXME: fix to work optimally for 'method'
-                    # quick check
-                    (
-                        substr( $leading_token, 0, 3 ) eq 'sub'
-                        || $rOpts_sub_alias_list
-                    )
-
-                    # slow check
-                    && $leading_token =~ /$SUB_PATTERN/
-                  )
-                {
-                    $blank_count = $rOpts->{'blank-lines-before-subs'}
-                      if ( terminal_type_i( $imin, $imax ) !~ /^[\;\}\,]$/ );
-                }
+                my $special_identifier =
+                  $self->[_ris_special_identifier_token_]->{$leading_token};
+                if ($special_identifier) {
+                    ##   $leading_token =~ /$SUB_PATTERN/
+                    if ( $special_identifier eq 'sub' ) {
+
+                        $blank_count = $rOpts->{'blank-lines-before-subs'}
+                          if (
+                            terminal_type_i( $imin, $imax ) !~ /^[\;\}\,]$/ );
+                    }
 
-                # break before all package declarations
-                elsif ( substr( $leading_token, 0, 8 ) eq 'package ' ) {
+                    # break before all package declarations
+                    ##      substr( $leading_token, 0, 8 ) eq 'package '
+                    elsif ( $special_identifier eq 'package' ) {
 
-                    # ... except in a very short eval block
-                    my $pseqno = $parent_seqno_to_go[$imin];
-                    $blank_count = $rOpts->{'blank-lines-before-packages'}
-                      if ( !$self->[_ris_short_broken_eval_block_]->{$pseqno} );
+                        # ... except in a very short eval block
+                        my $pseqno = $parent_seqno_to_go[$imin];
+                        $blank_count = $rOpts->{'blank-lines-before-packages'}
+                          if (
+                            !$self->[_ris_short_broken_eval_block_]->{$pseqno}
+                          );
+                    }
                 }
             }