From 04a9d80e53f64ae97d75e746377f83f8519e416d Mon Sep 17 00:00:00 2001 From: lwelling Date: Thu, 2 May 2013 20:34:25 -0400 Subject: [PATCH] Cache result of Language::isValidCode() to avoid regex processing The function can be called over 2000 times in generating a page. This way is significantly faster even for random invalid codes. For real use it should avoid the regex most of the time with no change in behavior. Change-Id: I9fcbae1770be0d3f405d3d12254c11943b0d5f46 --- languages/Language.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/languages/Language.php b/languages/Language.php index b9201d7ed4..84e7f37db8 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -326,13 +326,19 @@ class Language { * @return bool */ public static function isValidCode( $code ) { - return - // People think language codes are html safe, so enforce it. - // Ideally we should only allow a-zA-Z0-9- - // but, .+ and other chars are often used for {{int:}} hacks - // see bugs 37564, 37587, 36938 + static $cache = array(); + if( isset( $cache[$code] ) ) { + return $cache[$code]; + } + // People think language codes are html safe, so enforce it. + // Ideally we should only allow a-zA-Z0-9- + // but, .+ and other chars are often used for {{int:}} hacks + // see bugs 37564, 37587, 36938 + $return = strcspn( $code, ":/\\\000&<>'\"" ) === strlen( $code ) && !preg_match( Title::getTitleInvalidRegex(), $code ); + $cache[ $code ] = $return; + return $return; } /** -- 2.20.1