Micro optimization when fetching a magic from cache
authorAntoine Musso <hashar@free.fr>
Wed, 29 May 2013 10:21:42 +0000 (12:21 +0200)
committerNikerabbit <niklas.laxstrom@gmail.com>
Thu, 30 May 2013 15:42:34 +0000 (15:42 +0000)
The Language::getMagic() methods would request a full copy of the magic
words array from the cache.  Instead use LocalisationCache::getSubitem()
to only fetch the magic word we are interested in.

I do not think that makes that much of a difference, but that is
certainly nicer.  Note that getSubItem() returns null on miss instead of
the previous 'false', that still works for the !is_array().

Change-Id: I02824fd313eeecb5e5b26cb79d7b5549f6648f91

languages/Language.php

index ea34363..92ea75c 100644 (file)
@@ -2953,12 +2953,16 @@ class Language {
        }
 
        /**
+        * Get all magic words from cache.
         * @return array
         */
        function getMagicWords() {
                return self::$dataCache->getItem( $this->mCode, 'magicWords' );
        }
 
+       /**
+        * Run the LanguageGetMagic hook once.
+        */
        protected function doMagicHook() {
                if ( $this->mMagicHookDone ) {
                        return;
@@ -2975,17 +2979,16 @@ class Language {
         * @param $mw
         */
        function getMagic( $mw ) {
-               $this->doMagicHook();
+               // Saves a function call
+               if ( ! $this->mMagicHookDone ) {
+                       $this->doMagicHook();
+               }
 
                if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
                        $rawEntry = $this->mMagicExtensions[$mw->mId];
                } else {
-                       $magicWords = $this->getMagicWords();
-                       if ( isset( $magicWords[$mw->mId] ) ) {
-                               $rawEntry = $magicWords[$mw->mId];
-                       } else {
-                               $rawEntry = false;
-                       }
+                       $rawEntry = self::$dataCache->getSubitem(
+                               $this->mCode, 'magicWords', $mw->mId );
                }
 
                if ( !is_array( $rawEntry ) ) {