Skin: Reduce database queries for footer links on every page
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 8 Oct 2016 01:15:15 +0000 (03:15 +0200)
committerTimo Tijhof <krinklemail@gmail.com>
Wed, 16 Nov 2016 21:00:08 +0000 (13:00 -0800)
When viewing Special:Blankpage there are still 6 database queries
on every page view. 3 of these are from the Skin:

> $tpl->set( 'disclaimer', $this->disclaimerLink() );
> $tpl->set( 'privacy', $this->privacyLink() );
> $tpl->set( 'about', $this->aboutLink() );

In Wikimedia production, Xenon flame graphs (reversed) for index.php
attribute 22% of LinkCache::fetchPageRow calls to Skin::footerLink().

Add them to Skin::preloadExistence() instead.

Change-Id: I61c285be08a2130fb39b75ca717ea83f297c4489

includes/skins/Skin.php

index b60aa10..96812ea 100644 (file)
@@ -217,6 +217,17 @@ abstract class Skin extends ContextSource {
                        }
                }
 
+               // Footer links (used by SkinTemplate::prepareQuickTemplate)
+               foreach ( [
+                       $this->footerLinkTitle( 'privacy', 'privacypage' ),
+                       $this->footerLinkTitle( 'aboutsite', 'aboutpage' ),
+                       $this->footerLinkTitle( 'disclaimers', 'disclaimerpage' ),
+               ] as $title ) {
+                       if ( $title ) {
+                               $titles[] = $title;
+                       }
+               }
+
                Hooks::run( 'SkinPreloadExistence', [ &$titles, $this ] );
 
                if ( $titles ) {
@@ -921,25 +932,34 @@ abstract class Skin extends ContextSource {
         * @return string HTML anchor
         */
        public function footerLink( $desc, $page ) {
-               // if the link description has been set to "-" in the default language,
-               if ( $this->msg( $desc )->inContentLanguage()->isDisabled() ) {
-                       // then it is disabled, for all languages.
+               $title = $this->footerLinkTitle( $desc, $page );
+               if ( !$title ) {
                        return '';
-               } else {
-                       // Otherwise, we display the link for the user, described in their
-                       // language (which may or may not be the same as the default language),
-                       // but we make the link target be the one site-wide page.
-                       $title = Title::newFromText( $this->msg( $page )->inContentLanguage()->text() );
+               }
 
-                       if ( !$title ) {
-                               return '';
-                       }
+               return Linker::linkKnown(
+                       $title,
+                       $this->msg( $desc )->escaped()
+               );
+       }
 
-                       return Linker::linkKnown(
-                               $title,
-                               $this->msg( $desc )->escaped()
-                       );
+       /**
+        * @param string $desc
+        * @param string $page
+        * @return Title|null
+        */
+       private function footerLinkTitle( $desc, $page ) {
+               // If the link description has been set to "-" in the default language,
+               if ( $this->msg( $desc )->inContentLanguage()->isDisabled() ) {
+                       // then it is disabled, for all languages.
+                       return null;
                }
+               // Otherwise, we display the link for the user, described in their
+               // language (which may or may not be the same as the default language),
+               // but we make the link target be the one site-wide page.
+               $title = Title::newFromText( $this->msg( $page )->inContentLanguage()->text() );
+
+               return $title ?: null;
        }
 
        /**