From: stekkel Date: Sat, 30 Sep 2006 16:24:00 +0000 (+0000) Subject: sqBodywrap was extreme slow on large messages. The reason for that were the X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=17b097be4eae79b8fc7a3588e266fe7f4384a827;p=squirrelmail.git sqBodywrap was extreme slow on large messages. The reason for that were the amount of calls to sq_strpos and sq_substr. Those functions did expensive in_array calls and a few other things that only need to be done once. By using static vars I cached the results with as result my compose screen opens within a second again instead of 10 seconds. git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@11788 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- diff --git a/functions/strings.php b/functions/strings.php index 68ffbe1e..28e9bfa0 100644 --- a/functions/strings.php +++ b/functions/strings.php @@ -1136,15 +1136,32 @@ function sq_str_pad($string, $width, $pad, $padtype, $charset='') { */ function sq_substr($string,$start,$length,$charset='auto') { // use automatic charset detection, if function call asks for it + static $charset_auto, $bUse_mb; + if ($charset=='auto') { - global $default_charset, $squirrelmail_language; - set_my_charset(); - $charset=$default_charset; - if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; + if (!isset($charset_auto)) { + global $default_charset, $squirrelmail_language; + set_my_charset(); + $charset=$default_charset; + if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; + $charset_auto = $charset; + } else { + $charset = $charset_auto; + } } $charset = strtolower($charset); - if (function_exists('mb_internal_encoding') && - in_array($charset,sq_mb_list_encodings())) { + + // in_array call is expensive => do it once and use a static var for + // storing the results + if (!isset($bUse_mb)) { + if (in_array($charset,sq_mb_list_encodings())) { + $bUse_mb = true; + } else { + $bUse_mb = false; + } + } + + if ($bUse_mb) { return mb_substr($string,$start,$length,$charset); } // TODO: add mbstring independent code @@ -1167,15 +1184,31 @@ function sq_substr($string,$start,$length,$charset='auto') { */ function sq_strpos($haystack,$needle,$offset,$charset='auto') { // use automatic charset detection, if function call asks for it + static $charset_auto, $bUse_mb; + if ($charset=='auto') { - global $default_charset, $squirrelmail_language; - set_my_charset(); - $charset=$default_charset; - if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; + if (!isset($charset_auto)) { + global $default_charset, $squirrelmail_language; + set_my_charset(); + $charset=$default_charset; + if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; + $charset_auto = $charset; + } else { + $charset = $charset_auto; + } } $charset = strtolower($charset); - if (function_exists('mb_internal_encoding') && - in_array($charset,sq_mb_list_encodings())) { + + // in_array call is expensive => do it once and use a static var for + // storing the results + if (!isset($bUse_mb)) { + if (in_array($charset,sq_mb_list_encodings())) { + $bUse_mb = true; + } else { + $bUse_mb = false; + } + } + if ($bUse_mb) { return mb_strpos($haystack,$needle,$offset,$charset); } // TODO: add mbstring independent code @@ -1196,15 +1229,33 @@ function sq_strpos($haystack,$needle,$offset,$charset='auto') { */ function sq_strtoupper($string,$charset='auto') { // use automatic charset detection, if function call asks for it + static $charset_auto, $bUse_mb; + if ($charset=='auto') { - global $default_charset,$squirrelmail_language; - set_my_charset(); - $charset=$default_charset; - if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; + if (!isset($charset_auto)) { + global $default_charset, $squirrelmail_language; + set_my_charset(); + $charset=$default_charset; + if ($squirrelmail_language=='ja_JP') $charset='euc-jp'; + $charset_auto = $charset; + } else { + $charset = $charset_auto; + } } $charset = strtolower($charset); - if (function_exists('mb_strtoupper') && - in_array($charset,sq_mb_list_encodings())) { + + // in_array call is expensive => do it once and use a static var for + // storing the results + if (!isset($bUse_mb)) { + if (function_exists('mb_strtoupper') && + in_array($charset,sq_mb_list_encodings())) { + $bUse_mb = true; + } else { + $bUse_mb = false; + } + } + + if ($bUse_mb) { return mb_strtoupper($string,$charset); } // TODO: add mbstring independent code