From: Kunal Mehta Date: Sat, 18 Aug 2018 00:26:26 +0000 (-0700) Subject: Optimize TitleFormatter::getPrefixedText() to Title performance X-Git-Tag: 1.34.0-rc.0~4340^2 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=d0f56e873d6b06bae7fedbd203f60c4261cb2fbe Optimize TitleFormatter::getPrefixedText() to Title performance ...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 --- diff --git a/includes/Title.php b/includes/Title.php index e74824c9b8..c919b18856 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -134,8 +134,15 @@ class Title implements LinkTarget { /** @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; @@ -1669,12 +1676,12 @@ class Title implements LinkTarget { * @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, '_', ' ' ); - $this->mPrefixedText = $s; + $this->prefixedText = $s; } - return $this->mPrefixedText; + return $this->prefixedText; } /** diff --git a/includes/title/MediaWikiTitleCodec.php b/includes/title/MediaWikiTitleCodec.php index 3c14317e0b..f6a4c06522 100644 --- a/includes/title/MediaWikiTitleCodec.php +++ b/includes/title/MediaWikiTitleCodec.php @@ -186,12 +186,16 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { * @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; } /** diff --git a/includes/title/TitleValue.php b/includes/title/TitleValue.php index 43a399ab1c..698bc4f8be 100644 --- a/includes/title/TitleValue.php +++ b/includes/title/TitleValue.php @@ -59,6 +59,16 @@ class TitleValue implements LinkTarget { */ 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. *