jenkins-bot has submitted this change and it was merged.

Change subject: Use one instance of SerializationOptions in WikibaseLuaBindings
......................................................................


Use one instance of SerializationOptions in WikibaseLuaBindings

Creating a SerializationOptions object involves a bit of
overhead and performance cost, for example, creating a new
LanguageFallbackChain object for all the ~400 languages,
and validating language codes numerous times.

For one of my client pages with a bunch of lua usage,
LanguageWithConversion::validateLanguageCode was called
3967 times, taking a total of 452 ms second. Now it
is called 795 times (could be further optimized together with
parser function) and takes 93 ms.

LanguageFallbackChainFactory::newFromLanguageCode was called
2011 times, taking 271 ms. With this patch, it's now
called 403 times, 61 ms.

and a bunch of other slow methods are called much less.

* note that with full xdebug and memory tracing enabled, these numbers
are a bit slower than one would see otherwise.

Change-Id: I47a33011c8ac4e05bfd0922c15798f4fd981ef00
---
M client/includes/scribunto/WikibaseLuaBindings.php
1 file changed, 37 insertions(+), 6 deletions(-)

Approvals:
  Aude: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/client/includes/scribunto/WikibaseLuaBindings.php 
b/client/includes/scribunto/WikibaseLuaBindings.php
index d1b4d8a..cb20c03 100644
--- a/client/includes/scribunto/WikibaseLuaBindings.php
+++ b/client/includes/scribunto/WikibaseLuaBindings.php
@@ -88,6 +88,11 @@
        private $usageAccumulator;
 
        /**
+        * @var SerializationOptions
+        */
+       private $serializationOptions = null;
+
+       /**
         * @param EntityIdParser $entityIdParser
         * @param EntityLookup $entityLookup
         * @param SiteLinkLookup $siteLinkTable
@@ -193,16 +198,40 @@
         * @return Serializer
         */
        private function getEntitySerializer( EntityDocument $entityObject, 
$lowerCaseIds ) {
-               $opt = new SerializationOptions();
-               $serializerFactory = new SerializerFactory( $opt, 
$this->dataTypeLookup );
+               $options = $this->getSerializationOptions( $lowerCaseIds );
+               $serializerFactory = new SerializerFactory( $options, 
$this->dataTypeLookup );
+
+               return $serializerFactory->newSerializerForObject( 
$entityObject, $options );
+       }
+
+       /**
+        * @param bool $lowerCaseIds
+        *
+        * @return SerializationOptions
+        */
+       private function getSerializationOptions( $lowerCaseIds ) {
+               if ( $this->serializationOptions === null ) {
+                       $this->serializationOptions = 
$this->newSerializationOptions();
+               }
 
                // Using "ID_KEYS_BOTH" here means that all lists of Snaks or 
Claims will be listed
                // twice, once with a lower case key and once with an upper 
case key.
                // This is a B/C hack to allow existing lua code to use 
hardcoded IDs
                // in both lower (legacy) and upper case.
                if ( $lowerCaseIds ) {
-                       $opt->setIdKeyMode( SerializationOptions::ID_KEYS_BOTH 
);
+                       $this->serializationOptions->setIdKeyMode( 
SerializationOptions::ID_KEYS_BOTH );
+               } else {
+                       $this->serializationOptions->setIdKeyMode( 
SerializationOptions::ID_KEYS_UPPER );
                }
+
+               return $this->serializationOptions;
+       }
+
+       /**
+        * @return SerializationOptions
+        */
+       private function newSerializationOptions() {
+               $options = new SerializationOptions();
 
                // See mw.wikibase.lua. This is the only way to inject values 
into mw.wikibase.label( ),
                // so any customized Lua modules can access labels of another 
entity written in another variant,
@@ -212,10 +241,12 @@
                        LanguageFallbackChainFactory::FALLBACK_SELF | 
LanguageFallbackChainFactory::FALLBACK_VARIANTS
                );
 
-               // SerializationOptions accepts mixed types of keys happily.
-               $opt->setLanguages( $this->languageCodes + array( 
$this->language->getCode() => $chain ) );
+               $languages = $this->languageCodes + array( 
$this->language->getCode() => $chain );
 
-               return $serializerFactory->newSerializerForObject( 
$entityObject, $opt );
+               // SerializationOptions accepts mixed types of keys happily.
+               $options->setLanguages( $languages );
+
+               return $options;
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/180066
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I47a33011c8ac4e05bfd0922c15798f4fd981ef00
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: wmf/1.25wmf12c
Gerrit-Owner: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to