Optimize TitleFormatter::getPrefixedText() to Title performance
authorKunal Mehta <legoktm@member.fsf.org>
Sat, 18 Aug 2018 00:26:26 +0000 (17:26 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Sat, 18 Aug 2018 00:43:52 +0000 (17:43 -0700)
...by instance caching the result on the LinkTarget object.

Title::getPrefixedText() is an extremely hot function that has been
fully optimized over the years due to its usage basically everywhere in
MediaWiki.

Apply the same treatment to TitleFormatter, but keep the caching as part
of the LinkTarget instance so we can share caches across Title and
TitleFormatter. Once Title is switched over to using TitleFormatter
internally, we can probably centralize the cache again, and get rid of
the public but marked @private member variables.

Bug: T201801
Change-Id: I4ae2d6b176f69f66720473aeae85e39a601a9781

includes/Title.php
includes/title/MediaWikiTitleCodec.php
includes/title/TitleValue.php

index e74824c..c919b18 100644 (file)
@@ -134,8 +134,15 @@ class Title implements LinkTarget {
        /** @var bool Boolean for initialisation on demand */
        public $mRestrictionsLoaded = false;
 
        /** @var bool Boolean for initialisation on demand */
        public $mRestrictionsLoaded = false;
 
-       /** @var string Text form including namespace/interwiki, initialised on demand */
-       protected $mPrefixedText = null;
+       /**
+        * Text form including namespace/interwiki, initialised on demand
+        *
+        * Only public to share cache with TitleFormatter
+        *
+        * @private
+        * @var string
+        */
+       public $prefixedText = null;
 
        /** @var mixed Cached value for getTitleProtection (create protection) */
        public $mTitleProtection;
 
        /** @var mixed Cached value for getTitleProtection (create protection) */
        public $mTitleProtection;
@@ -1669,12 +1676,12 @@ class Title implements LinkTarget {
         * @return string The prefixed title, with spaces
         */
        public function getPrefixedText() {
         * @return string The prefixed title, with spaces
         */
        public function getPrefixedText() {
-               if ( $this->mPrefixedText === null ) {
+               if ( $this->prefixedText === null ) {
                        $s = $this->prefix( $this->mTextform );
                        $s = strtr( $s, '_', ' ' );
                        $s = $this->prefix( $this->mTextform );
                        $s = strtr( $s, '_', ' ' );
-                       $this->mPrefixedText = $s;
+                       $this->prefixedText = $s;
                }
                }
-               return $this->mPrefixedText;
+               return $this->prefixedText;
        }
 
        /**
        }
 
        /**
index 3c14317..f6a4c06 100644 (file)
@@ -186,12 +186,16 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         * @return string
         */
        public function getPrefixedText( LinkTarget $title ) {
         * @return string
         */
        public function getPrefixedText( LinkTarget $title ) {
-               return $this->formatTitle(
-                       $title->getNamespace(),
-                       $title->getText(),
-                       '',
-                       $title->getInterwiki()
-               );
+               if ( !isset( $title->prefixedText ) ) {
+                       $title->prefixedText = $this->formatTitle(
+                               $title->getNamespace(),
+                               $title->getText(),
+                               '',
+                               $title->getInterwiki()
+                       );
+               }
+
+               return $title->prefixedText;
        }
 
        /**
        }
 
        /**
index 43a399a..698bc4f 100644 (file)
@@ -59,6 +59,16 @@ class TitleValue implements LinkTarget {
         */
        protected $interwiki;
 
         */
        protected $interwiki;
 
+       /**
+        * Text form including namespace/interwiki, initialised on demand
+        *
+        * Only public to share cache with TitleFormatter
+        *
+        * @private
+        * @var string
+        */
+       public $prefixedText = null;
+
        /**
         * Constructs a TitleValue.
         *
        /**
         * Constructs a TitleValue.
         *