Aude has uploaded a new change for review. https://gerrit.wikimedia.org/r/178356
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(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/56/178356/2 diff --git a/client/includes/scribunto/WikibaseLuaBindings.php b/client/includes/scribunto/WikibaseLuaBindings.php index 3ae0708..8407128 100644 --- a/client/includes/scribunto/WikibaseLuaBindings.php +++ b/client/includes/scribunto/WikibaseLuaBindings.php @@ -89,6 +89,11 @@ private $usageAccumulator; /** + * @var SerializationOptions + */ + private $serializationOptions = null; + + /** * @param EntityIdParser $entityIdParser * @param EntityLookup $entityLookup * @param SiteLinkLookup $siteLinkTable @@ -194,16 +199,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, @@ -213,10 +242,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/178356 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I47a33011c8ac4e05bfd0922c15798f4fd981ef00 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits