Cscott has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/79822


Change subject: Optimize Parser::doQuotes().
......................................................................

Optimize Parser::doQuotes().

Performance improvements to doQuotes(), since it is a hot function.

Co-authored-by: Tyler Anthony Romeo <[email protected]>
Change-Id: If78d4372a2acd78d58b020385da400978716cbf5
---
M includes/parser/Parser.php
1 file changed, 20 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/22/79822/1

diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php
index 2943afa..4561351 100644
--- a/includes/parser/Parser.php
+++ b/includes/parser/Parser.php
@@ -1414,7 +1414,8 @@
         */
        public function doQuotes( $text ) {
                $arr = preg_split( "/(''+)/", $text, -1, 
PREG_SPLIT_DELIM_CAPTURE );
-               if ( count( $arr ) == 1 ) {
+               $countarr = count( $arr );
+               if ( $countarr == 1 ) {
                        return $text;
                }
 
@@ -1423,26 +1424,29 @@
                // of bold and italics mark-ups.
                $numbold = 0;
                $numitalics = 0;
-               for ( $i = 1; $i < count( $arr ); $i+=2 ) {
+               for ( $i = 1; $i < $countarr; $i+=2 ) {
+                       $thislen = strlen( $arr[$i] );
                        // If there are ever four apostrophes, assume the first 
is supposed to
                        // be text, and the remaining three constitute mark-up 
for bold text.
                        // (bug 13227: ''''foo'''' turns into ''' 'foo ''' ')
-                       if ( strlen( $arr[$i] ) == 4 ) {
+                       if ( $thislen == 4 ) {
                                $arr[$i - 1] .= "'";
                                $arr[$i] = "'''";
-                       } elseif ( strlen( $arr[$i] ) > 5 ) {
+                               $thislen = 3;
+                       } elseif ( $thislen > 5 ) {
                                // If there are more than 5 apostrophes in a 
row, assume they're all
                                // text except for the last 5.
                                // (bug 13227: ''''''foo'''''' turns into ''''' 
'foo ''''' ')
-                               $arr[$i - 1] .= str_repeat( "'", strlen( 
$arr[$i] ) - 5 );
+                               $arr[$i - 1] .= str_repeat( "'", $thislen - 5 );
                                $arr[$i] = "'''''";
+                               $thislen = 5;
                        }
                        // Count the number of occurrences of bold and italics 
mark-ups.
-                       if ( strlen( $arr[$i] ) == 2 ) {
+                       if ( $thislen == 2 ) {
                                $numitalics++;
-                       } elseif ( strlen( $arr[$i] ) == 3 ) {
+                       } elseif ( $thislen == 3 ) {
                                $numbold++;
-                       } elseif ( strlen( $arr[$i] ) == 5 ) {
+                       } elseif ( $thislen == 5 ) {
                                $numitalics++;
                                $numbold++;
                        }
@@ -1456,7 +1460,7 @@
                        $firstsingleletterword = -1;
                        $firstmultiletterword = -1;
                        $firstspace = -1;
-                       for ( $i = 1; $i < count( $arr ); $i+=2 ) {
+                       for ( $i = 1; $i < $countarr; $i+=2 ) {
                                if ( strlen( $arr[$i] ) == 3 ) {
                                        $x1 = substr( $arr[$i - 1], -1 );
                                        $x2 = substr( $arr[$i - 1], -2, 1 );
@@ -1467,6 +1471,9 @@
                                        } elseif ( $x2 === ' ' ) {
                                                if ( $firstsingleletterword == 
-1 ) {
                                                        $firstsingleletterword 
= $i;
+                                                       // if 
$firstsingleletterword is set, we don't
+                                                       // look at the other 
options, so we can bail early.
+                                                       break;
                                                }
                                        } else {
                                                if ( $firstmultiletterword == 
-1 ) {
@@ -1506,7 +1513,8 @@
                                        $output .= $r;
                                }
                        } else {
-                               if ( strlen( $r ) == 2 ) {
+                               $thislen = strlen( $r );
+                               if ( $thislen == 2 ) {
                                        if ( $state === 'i' ) {
                                                $output .= '</i>';
                                                $state = '';
@@ -1523,7 +1531,7 @@
                                                $output .= '<i>';
                                                $state .= 'i';
                                        }
-                               } elseif ( strlen( $r ) == 3 ) {
+                               } elseif ( $thislen == 3 ) {
                                        if ( $state === 'b' ) {
                                                $output .= '</b>';
                                                $state = '';
@@ -1540,7 +1548,7 @@
                                                $output .= '<b>';
                                                $state .= 'b';
                                        }
-                               } elseif ( strlen( $r ) == 5 ) {
+                               } elseif ( $thislen == 5 ) {
                                        if ( $state === 'b' ) {
                                                $output .= '</b><i>';
                                                $state = 'i';

-- 
To view, visit https://gerrit.wikimedia.org/r/79822
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If78d4372a2acd78d58b020385da400978716cbf5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to