From 0cf603ca9df14412b666b475e1269ae8d3b873dd Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Mon, 20 Mar 2017 13:59:23 +1100 Subject: [PATCH] Make the maximum language cache size configurable Make the language cache size configurable, and increase the default from 20 to 30. It needs to be fairly small on default installations, but can be essentially unlimited if $wgLocalisationCacheConf['manualRecache'] is true. Bug: T85461 Change-Id: Idb17691b30b0d2565a1624e5159df7d9b795764d --- Scribunto.php | 8 ++++++++ common/Base.php | 11 +++++++++++ engines/LuaCommon/LanguageLibrary.php | 6 +++--- tests/engines/LuaCommon/LuaEngineTestBase.php | 2 ++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Scribunto.php b/Scribunto.php index 15a4c597..39b15bc3 100644 --- a/Scribunto.php +++ b/Scribunto.php @@ -168,6 +168,13 @@ $wgScribuntoEngineConf = array( // implemented in the future, and there is no guarantee that a // simulation of setfenv() and getfenv() will be provided. 'allowEnvFuncs' => false, + + // The maximum number of languages about which data can be requested. + // The cost is about 1.5MB of memory usage per language on default + // installations (during recache), but if recaching is disabled with + // $wgLocalisationCacheConf['manualRecache'] = false + // then memory usage is perhaps 10x smaller. + 'maxLangCacheSize' => 30, ), 'luastandalone' => array( 'class' => 'Scribunto_LuaStandaloneEngine', @@ -182,6 +189,7 @@ $wgScribuntoEngineConf = array( 'memoryLimit' => 50 * 1024 * 1024, 'cpuLimit' => 7, 'allowEnvFuncs' => false, + 'maxLangCacheSize' => 30, ), ); diff --git a/common/Base.php b/common/Base.php index 180c375a..1329f830 100644 --- a/common/Base.php +++ b/common/Base.php @@ -122,6 +122,17 @@ abstract class ScribuntoEngineBase { return $this->title; } + /** + * Get an element from the configuration array + * + * @param string $optionName + * @return mixed + */ + public function getOption( $optionName ) { + return isset( $this->options[$optionName] ) + ? $this->options[$optionName] : null; + } + /** * @param string $message * @param $params array diff --git a/engines/LuaCommon/LanguageLibrary.php b/engines/LuaCommon/LanguageLibrary.php index dbf205e7..92ddfefd 100644 --- a/engines/LuaCommon/LanguageLibrary.php +++ b/engines/LuaCommon/LanguageLibrary.php @@ -2,15 +2,15 @@ // @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps class Scribunto_LuaLanguageLibrary extends Scribunto_LuaLibraryBase { - const MAX_LANG_CACHE_SIZE = 20; - public $langCache = array(); public $timeCache = array(); + public $maxLangCacheSize; function register() { // Pre-populate the language cache global $wgContLang; $this->langCache[$wgContLang->getCode()] = $wgContLang; + $this->maxLangCacheSize = $this->getEngine()->getOption( 'maxLangCacheSize' ); $statics = array( 'getContLangCode', @@ -111,7 +111,7 @@ class Scribunto_LuaLanguageLibrary extends Scribunto_LuaLibraryBase { $name = strval( $name ); $code = array_shift( $args ); if ( !isset( $this->langCache[$code] ) ) { - if ( count( $this->langCache ) > self::MAX_LANG_CACHE_SIZE ) { + if ( count( $this->langCache ) > $this->maxLangCacheSize ) { throw new Scribunto_LuaError( 'too many language codes requested' ); } try { diff --git a/tests/engines/LuaCommon/LuaEngineTestBase.php b/tests/engines/LuaCommon/LuaEngineTestBase.php index 048f2a7e..e438769f 100644 --- a/tests/engines/LuaCommon/LuaEngineTestBase.php +++ b/tests/engines/LuaCommon/LuaEngineTestBase.php @@ -16,6 +16,7 @@ abstract class Scribunto_LuaEngineTestBase extends MediaWikiLangTestCase { 'memoryLimit' => 50000000, 'cpuLimit' => 30, 'allowEnvFuncs' => true, + 'maxLangCacheSize' => 30, ), 'LuaStandalone' => array( 'errorFile' => null, @@ -23,6 +24,7 @@ abstract class Scribunto_LuaEngineTestBase extends MediaWikiLangTestCase { 'memoryLimit' => 50000000, 'cpuLimit' => 30, 'allowEnvFuncs' => true, + 'maxLangCacheSize' => 30, ), );