Add LCStore implementation that uses static arrays in PHP files
authorFred Emmott <fe@fb.com>
Tue, 9 Jun 2015 21:24:15 +0000 (14:24 -0700)
committerOri.livneh <ori@wikimedia.org>
Thu, 11 Jun 2015 18:14:51 +0000 (18:14 +0000)
Implementation written by Fred Emmott of Facebook. Quoting Fred:

As well as array access being faster, the main advantage is actually
that this significantly reduces the use of unserialize(), which does a
lot of memcpys when making the strings.

Benchmarks compared to LCStoreCDB:
* HHVM (no repo-auth): ~7% improvement
* HHVM (with repo-auth): ~12% improvement
* PHP7: ~1% improvement

My (Legoktm) brief testing noted that the generated PHP files were
noticiably larger than the CDB ones:
* 1.5M en.l10n.php
* 932K l10n_cache-en.cdb

Bug: T99740
Change-Id: Ib2c5856d40cd928cab4a79cb935b3ce08c598300

autoload.php
includes/DefaultSettings.php
includes/cache/LCStoreStaticArray.php [new file with mode: 0644]
includes/cache/LocalisationCache.php

index a84fefd..26d954a 100644 (file)
@@ -607,6 +607,7 @@ $wgAutoloadLocalClasses = array(
        'LCStoreCDB' => __DIR__ . '/includes/cache/LocalisationCache.php',
        'LCStoreDB' => __DIR__ . '/includes/cache/LocalisationCache.php',
        'LCStoreNull' => __DIR__ . '/includes/cache/LocalisationCache.php',
+       'LCStoreStaticArray' => __DIR__ . '/includes/cache/LCStoreStaticArray.php',
        'LangMemUsage' => __DIR__ . '/maintenance/language/langmemusage.php',
        'Language' => __DIR__ . '/languages/Language.php',
        'LanguageAr' => __DIR__ . '/languages/classes/LanguageAr.php',
index 5c29ff8..d278c59 100644 (file)
@@ -2297,11 +2297,13 @@ $wgAdaptiveMessageCache = false;
  * Localisation cache configuration. Associative array with keys:
  * class:       The class to use. May be overridden by extensions.
  *
- * store:       The location to store cache data. May be 'files', 'db' or
+ * store:       The location to store cache data. May be 'files', 'array', 'db' or
  *              'detect'. If set to "files", data will be in CDB files. If set
  *              to "db", data will be stored to the database. If set to
  *              "detect", files will be used if $wgCacheDirectory is set,
  *              otherwise the database will be used.
+ *              "array" is an experimental option that uses PHP files that
+ *              store static arrays.
  *
  * storeClass:  The class name for the underlying storage. If set to a class
  *              name, it overrides the "store" setting.
diff --git a/includes/cache/LCStoreStaticArray.php b/includes/cache/LCStoreStaticArray.php
new file mode 100644 (file)
index 0000000..862ed67
--- /dev/null
@@ -0,0 +1,140 @@
+<?php
+/**
+ *  Localisation cache storage based on PHP files and static arrays.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * @since 1.26
+ */
+class LCStoreStaticArray implements LCStore {
+       /** @var string|null Current language code. */
+       private $currentLang = null;
+
+       /** @var array Localisation data. */
+       private $data = array();
+
+       /** @var string File name. */
+       private $fname = null;
+
+       /** @var string Directory for cache files. */
+       private $directory;
+
+       public function __construct( $conf = array() ) {
+               global $wgCacheDirectory;
+
+               if ( isset( $conf['directory'] ) ) {
+                       $this->directory = $conf['directory'];
+               } else {
+                       $this->directory = $wgCacheDirectory;
+               }
+       }
+
+       public function startWrite( $code ) {
+               $this->currentLang = $code;
+               $this->fname = $this->directory. '/' . $code . '.l10n.php';
+               $this->data[$code] = array();
+               if ( file_exists( $this->fname ) ) {
+                       $this->data[$code] = require $this->fname;
+               }
+       }
+
+       public function set( $key, $value ) {
+               $this->data[$this->currentLang][$key] = self::encode( $value );
+       }
+
+       /**
+        * Encodes a value into an array format
+        *
+        * @param mixed $value
+        * @return array
+        * @throws RuntimeException
+        */
+       public static function encode( $value ) {
+               if ( is_scalar( $value ) || $value === null ) {
+                       // [V]alue
+                       return array( 'v', $value );
+               }
+               if ( is_object( $value ) ) {
+                       // [S]erialized
+                       return array( 's', serialize( $value ) );
+               }
+               if ( is_array( $value ) ) {
+                       // [A]rray
+                       return array( 'a', array_map( function ( $v ) {
+                               return LCStoreStaticArray::encode( $v );
+                       }, $data ) );
+               }
+
+               throw new RuntimeException( 'Cannot encode ' . var_export( $value, true ) );
+       }
+
+       /**
+        * Decode something that was encoded with encode
+        *
+        * @param array $encoded
+        * @return array|mixed
+        * @throws RuntimeException
+        */
+       public static function decode( array $encoded ) {
+               $type = $encoded[0];
+               $data = $encoded[1];
+
+               switch ( $type ) {
+               case 'v':
+                       return $data;
+               case 's':
+                       return unserialize( $data );
+               case 'a':
+                       return array_map( function ( $v ) {
+                               return LCStoreStaticArray::decode( $v );
+                       }, $data );
+               default:
+                       throw new RuntimeException(
+                               'Unable to decode ' . var_export( $encoded, true ) );
+               }
+       }
+
+       public function finishWrite() {
+               file_put_contents(
+                       $this->fname,
+                       "<?php\n" .
+                       "// Generated by LCStoreStaticArray.php -- do not edit!\n" .
+                       "return " .
+                       var_export( $this->data[$this->currentLang], true ) . ';'
+               );
+               $this->currentLang = null;
+               $this->fname = null;
+       }
+
+       public function get( $code, $key ) {
+               if ( !array_key_exists( $code, $this->data ) ) {
+                       $fname = $this->directory. '/' . $code . '.l10n.php';
+                       if ( !file_exists( $fname ) ) {
+                               return null;
+                       }
+                       $this->data[$code] = require $fname;
+               }
+               $data = $this->data[$code];
+               if ( array_key_exists( $key, $data ) ) {
+                       return self::decode( $data[$key] );
+               }
+               return null;
+       }
+}
index dc5a2eb..febde7a 100644 (file)
@@ -204,6 +204,9 @@ class LocalisationCache {
                                case 'db':
                                        $storeClass = 'LCStoreDB';
                                        break;
+                               case 'array':
+                                       $storeClass = 'LCStoreStaticArray';
+                                       break;
                                case 'detect':
                                        $storeClass = $wgCacheDirectory ? 'LCStoreCDB' : 'LCStoreDB';
                                        break;