Improve LanguageConverter performance on pages with many HTML tags
authorC. Scott Ananian <cscott@cscott.net>
Tue, 2 Jul 2019 18:02:33 +0000 (14:02 -0400)
committerC. Scott Ananian <cscott@cscott.net>
Tue, 2 Jul 2019 18:24:45 +0000 (14:24 -0400)
We were concatenating a single character to the end of the wikitext
source (which copies the entire string) every time through an inner
loop; when the page was large and the loop count was large this took
an excessive amount of time.

Bug: T223969
Change-Id: Ib80306b0bc6c73b750d492764f0e2dfd3a7a5450

languages/LanguageConverter.php

index c5ff9d6..7fd3631 100644 (file)
@@ -426,8 +426,9 @@ class LanguageConverter {
 
                // We add a marker (\004) at the end of text, to ensure we always match the
                // entire text (Otherwise, pcre.backtrack_limit might cause silent failure)
+               $textWithMarker = $text . "\004";
                while ( $startPos < strlen( $text ) ) {
-                       if ( preg_match( $reg, $text . "\004", $markupMatches, PREG_OFFSET_CAPTURE, $startPos ) ) {
+                       if ( preg_match( $reg, $textWithMarker, $markupMatches, PREG_OFFSET_CAPTURE, $startPos ) ) {
                                $elementPos = $markupMatches[0][1];
                                $element = $markupMatches[0][0];
                                if ( $element === "\004" ) {