From 5c2a2d0dd589fe34925a19df1f3f050d91242a45 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Fri, 17 Mar 2023 17:30:18 -0700 Subject: [PATCH] replace regex with index function for efficiency The index functions are about 3x faster in this critical section. --- lib/Perl/Tidy/Formatter.pm | 134 +++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 64 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 75021add..b6052a62 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -7673,83 +7673,89 @@ sub respace_tokens_inner_loop { # ( $type =~ /^[wit]$/ ) elsif ( $is_wit{$type} ) { - # change '$ var' to '$var' etc - # change '@ ' to '@' - # Examples: <> - my $ord = ord( substr( $token, 1, 1 ) ); - if ( + # index() is several times faster than a regex test with \s here + ## $token =~ /\s/ + if ( index( $token, SPACE ) > 0 || index( $token, "\t" ) > 0 ) { + + # change '$ var' to '$var' etc + # change '@ ' to '@' + # Examples: <> + my $ord = ord( substr( $token, 1, 1 ) ); + if ( - # quick test for possible blank at second char - $ord > 0 && ( $ord < ORD_PRINTABLE_MIN - || $ord > ORD_PRINTABLE_MAX ) - ) - { - my ( $sigil, $word ) = split /\s+/, $token, 2; + # quick test for possible blank at second char + $ord > 0 && ( $ord < ORD_PRINTABLE_MIN + || $ord > ORD_PRINTABLE_MAX ) + ) + { + my ( $sigil, $word ) = split /\s+/, $token, 2; - # $sigil =~ /^[\$\&\%\*\@]$/ ) - if ( $is_sigil{$sigil} ) { - $token = $sigil; - $token .= $word if ( defined($word) ); # fix c104 - $rtoken_vars->[_TOKEN_] = $token; + # $sigil =~ /^[\$\&\%\*\@]$/ ) + if ( $is_sigil{$sigil} ) { + $token = $sigil; + $token .= $word if ( defined($word) ); # fix c104 + $rtoken_vars->[_TOKEN_] = $token; + } } - } - # Trim certain spaces in identifiers - if ( $type eq 'i' && $token =~ /\s/ ) { + # Trim certain spaces in identifiers + if ( $type eq 'i' ) { - if ( $token =~ /$SUB_PATTERN/ ) { + if ( $token =~ /$SUB_PATTERN/ ) { - # -spp = 0 : no space before opening prototype paren - # -spp = 1 : stable (follow input spacing) - # -spp = 2 : always space before opening prototype paren - if ( !defined($rOpts_space_prototype_paren) - || $rOpts_space_prototype_paren == 1 ) - { - ## default: stable - } - elsif ( $rOpts_space_prototype_paren == 0 ) { - $token =~ s/\s+\(/\(/; - } - elsif ( $rOpts_space_prototype_paren == 2 ) { - $token =~ s/\(/ (/; - } + # -spp = 0 : no space before opening prototype paren + # -spp = 1 : stable (follow input spacing) + # -spp = 2 : always space before opening prototype paren + if ( !defined($rOpts_space_prototype_paren) + || $rOpts_space_prototype_paren == 1 ) + { + ## default: stable + } + elsif ( $rOpts_space_prototype_paren == 0 ) { + $token =~ s/\s+\(/\(/; + } + elsif ( $rOpts_space_prototype_paren == 2 ) { + $token =~ s/\(/ (/; + } - # one space max, and no tabs - $token =~ s/\s+/ /g; - $rtoken_vars->[_TOKEN_] = $token; + # one space max, and no tabs + $token =~ s/\s+/ /g; + $rtoken_vars->[_TOKEN_] = $token; - $self->[_ris_special_identifier_token_]->{$token} = 'sub'; + $self->[_ris_special_identifier_token_]->{$token} = + 'sub'; - } + } - # clean up spaces in package identifiers, like - # "package Bob::Dog;" - elsif ( $token =~ /^(package|class)\s/ ) { - $token =~ s/\s+/ /g; - $rtoken_vars->[_TOKEN_] = $token; + # clean up spaces in package identifiers, like + # "package Bob::Dog;" + elsif ( $token =~ /^(package|class)\s/ ) { + $token =~ s/\s+/ /g; + $rtoken_vars->[_TOKEN_] = $token; - $self->[_ris_special_identifier_token_]->{$token} = - 'package'; - } + $self->[_ris_special_identifier_token_]->{$token} = + 'package'; + } - # trim identifiers of trailing blanks which can occur - # under some unusual circumstances, such as if the - # identifier 'witch' has trailing blanks on input here: - # - # sub - # witch - # () # prototype may be on new line ... - # ... - my $ord_ch = ord( substr( $token, -1, 1 ) ); - if ( + # trim identifiers of trailing blanks which can occur + # under some unusual circumstances, such as if the + # identifier 'witch' has trailing blanks on input here: + # + # sub + # witch + # () # prototype may be on new line ... + # ... + my $ord_ch = ord( substr( $token, -1, 1 ) ); + if ( - # quick check for possible ending space - $ord_ch > 0 && ( $ord_ch < ORD_PRINTABLE_MIN - || $ord_ch > ORD_PRINTABLE_MAX ) - ) - { - $token =~ s/\s+$//g; - $rtoken_vars->[_TOKEN_] = $token; + # quick check for possible ending space + $ord_ch > 0 && ( $ord_ch < ORD_PRINTABLE_MIN + || $ord_ch > ORD_PRINTABLE_MAX ) + ) + { + $token =~ s/\s+$//g; + $rtoken_vars->[_TOKEN_] = $token; + } } } } -- 2.39.5