From e51b0ac4afd12a175254f720283c6a946888a182 Mon Sep 17 00:00:00 2001 From: uwetews Date: Sat, 27 May 2017 11:04:00 +0200 Subject: [PATCH] - performance store flag for already required shared plugin functions in static variable or Smarty's $_cache to improve performance when plugins are often called https://github.com/smarty-php/smarty/commit/51e0d5cd405d764a4ea257d1bac1fb1205f74528#commitcomment-22280086 --- change_log.txt | 3 ++ libs/Smarty.class.php | 2 +- libs/plugins/block.textformat.php | 9 ++-- libs/plugins/function.html_checkboxes.php | 3 +- libs/plugins/function.html_image.php | 3 +- libs/plugins/function.html_options.php | 9 ++-- libs/plugins/function.html_radios.php | 3 +- libs/plugins/function.html_select_date.php | 23 ++++++---- libs/plugins/function.html_select_time.php | 25 +++++++---- libs/plugins/modifier.date_format.php | 24 +++++++++-- libs/plugins/modifier.escape.php | 49 ++++++++++++++++------ libs/plugins/modifier.replace.php | 10 +++-- libs/plugins/modifiercompiler.escape.php | 8 +++- 13 files changed, 126 insertions(+), 45 deletions(-) diff --git a/change_log.txt b/change_log.txt index 3fd017df..68840adb 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,5 +1,8 @@ ===== 3.1.32 - dev === 21.5.2017 + - performance store flag for already required shared plugin functions in static variable or + Smarty's $_cache to improve performance when plugins are often called + https://github.com/smarty-php/smarty/commit/51e0d5cd405d764a4ea257d1bac1fb1205f74528#commitcomment-22280086 - bugfix remove special treatment of classes implementing ArrayAccess in {foreach} https://github.com/smarty-php/smarty/issues/332 - bugfix remove deleted files by clear_cache() and clear_compiled_template() from diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 4c72f8ce..b61c4331 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -108,7 +108,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.32-dev-7'; + const SMARTY_VERSION = '3.1.32-dev-8'; /** * define variable scopes diff --git a/libs/plugins/block.textformat.php b/libs/plugins/block.textformat.php index e2c5e3de..5bdf62db 100644 --- a/libs/plugins/block.textformat.php +++ b/libs/plugins/block.textformat.php @@ -38,8 +38,9 @@ function smarty_block_textformat($params, $content, $template, &$repeat) if (is_null($content)) { return; } - if (Smarty::$_MBSTRING && !is_callable('smarty_mb_wordwrap')) { + if (!isset($template->smarty->_cache[ '_required_smw' ])) { require_once(SMARTY_PLUGINS_DIR . 'shared.mb_wordwrap.php'); + $template->smarty->_cache[ '_required_smw' ] = true; } $style = null; @@ -87,8 +88,10 @@ function smarty_block_textformat($params, $content, $template, &$repeat) } // convert mult. spaces & special chars to single space $_paragraph = - preg_replace(array('!\s+!' . Smarty::$_UTF8_MODIFIER, '!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER), - array(' ', ''), $_paragraph); + preg_replace(array('!\s+!' . Smarty::$_UTF8_MODIFIER, + '!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER), + array(' ', + ''), $_paragraph); // indent first line if ($indent_first > 0) { $_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph; diff --git a/libs/plugins/function.html_checkboxes.php b/libs/plugins/function.html_checkboxes.php index 04ce4573..84b8456d 100644 --- a/libs/plugins/function.html_checkboxes.php +++ b/libs/plugins/function.html_checkboxes.php @@ -45,8 +45,9 @@ */ function smarty_function_html_checkboxes($params, $template) { - if (!is_callable('smarty_function_escape_special_chars')) { + if (!isset($template->smarty->_cache[ '_required_sesc' ])) { require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); + $template->smarty->_cache[ '_required_sesc' ] = true; } $name = 'checkbox'; diff --git a/libs/plugins/function.html_image.php b/libs/plugins/function.html_image.php index 6da8fc54..3218634a 100644 --- a/libs/plugins/function.html_image.php +++ b/libs/plugins/function.html_image.php @@ -38,8 +38,9 @@ */ function smarty_function_html_image($params, $template) { - if (!is_callable('smarty_function_escape_special_chars')) { + if (!isset($template->smarty->_cache[ '_required_sesc' ])) { require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); + $template->smarty->_cache[ '_required_sesc' ] = true; } $alt = ''; diff --git a/libs/plugins/function.html_options.php b/libs/plugins/function.html_options.php index bc8f36e0..fd67d81b 100644 --- a/libs/plugins/function.html_options.php +++ b/libs/plugins/function.html_options.php @@ -28,15 +28,18 @@ * @author Monte Ohrt * @author Ralf Strehle (minor optimization) * - * @param array $params parameters + * @param array $params parameters + * + * @param \Smarty_Internal_Template $template * * @return string * @uses smarty_function_escape_special_chars() */ -function smarty_function_html_options($params) +function smarty_function_html_options($params, Smarty_Internal_Template $template) { - if (!is_callable('smarty_function_escape_special_chars')) { + if (!isset($template->smarty->_cache[ '_required_sesc' ])) { require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); + $template->smarty->_cache[ '_required_sesc' ] = true; } $name = null; diff --git a/libs/plugins/function.html_radios.php b/libs/plugins/function.html_radios.php index 6ef84328..ac12ad71 100644 --- a/libs/plugins/function.html_radios.php +++ b/libs/plugins/function.html_radios.php @@ -45,8 +45,9 @@ */ function smarty_function_html_radios($params, $template) { - if (!is_callable('smarty_function_escape_special_chars')) { + if (!isset($template->smarty->_cache[ '_required_sesc' ])) { require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); + $template->smarty->_cache[ '_required_sesc' ] = true; } $name = 'radio'; diff --git a/libs/plugins/function.html_select_date.php b/libs/plugins/function.html_select_date.php index 3a48da0b..0506c5a9 100644 --- a/libs/plugins/function.html_select_date.php +++ b/libs/plugins/function.html_select_date.php @@ -37,17 +37,21 @@ * @author Monte Ohrt * @author Rodney Rehm * - * @param array $params parameters + * @param array $params parameters + * + * @param \Smarty_Internal_Template $template * * @return string */ -function smarty_function_html_select_date($params) +function smarty_function_html_select_date($params, Smarty_Internal_Template $template) { - if (!is_callable('smarty_function_escape_special_chars')) { + if (!isset($template->smarty->_cache[ '_required_sesc' ])) { require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); + $template->smarty->_cache[ '_required_sesc' ] = true; } - if (!is_callable('smarty_make_timestamp')) { + if (!isset($template->smarty->_cache[ '_required_smt' ])) { require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'); + $template->smarty->_cache[ '_required_smt' ] = true; } // generate timestamps used for month names only static $_month_timestamps = null; @@ -177,7 +181,9 @@ function smarty_function_html_select_date($params) if (isset($params[ 'time' ]) && is_array($params[ 'time' ])) { if (isset($params[ 'time' ][ $prefix . 'Year' ])) { // $_REQUEST[$field_array] given - foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) { + foreach (array('Y' => 'Year', + 'm' => 'Month', + 'd' => 'Day') as $_elementKey => $_elementName) { $_variableName = '_' . strtolower($_elementName); $$_variableName = isset($params[ 'time' ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $prefix . $_elementName ] : @@ -185,7 +191,9 @@ function smarty_function_html_select_date($params) } } elseif (isset($params[ 'time' ][ $field_array ][ $prefix . 'Year' ])) { // $_REQUEST given - foreach (array('Y' => 'Year', 'm' => 'Month', 'd' => 'Day') as $_elementKey => $_elementName) { + foreach (array('Y' => 'Year', + 'm' => 'Month', + 'd' => 'Day') as $_elementKey => $_elementName) { $_variableName = '_' . strtolower($_elementName); $$_variableName = isset($params[ 'time' ][ $field_array ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $field_array ][ $prefix . $_elementName ] : date($_elementKey); @@ -206,7 +214,8 @@ function smarty_function_html_select_date($params) // make syntax "+N" or "-N" work with $start_year and $end_year // Note preg_match('!^(\+|\-)\s*(\d+)$!', $end_year, $match) is slower than trim+substr - foreach (array('start', 'end') as $key) { + foreach (array('start', + 'end') as $key) { $key .= '_year'; $t = $$key; if ($t === null) { diff --git a/libs/plugins/function.html_select_time.php b/libs/plugins/function.html_select_time.php index 89f0406f..c15d38f0 100644 --- a/libs/plugins/function.html_select_time.php +++ b/libs/plugins/function.html_select_time.php @@ -17,18 +17,22 @@ * @author Roberto Berto * @author Monte Ohrt * - * @param array $params parameters + * @param array $params parameters + * + * @param \Smarty_Internal_Template $template * * @return string * @uses smarty_make_timestamp() */ -function smarty_function_html_select_time($params) +function smarty_function_html_select_time($params, Smarty_Internal_Template $template) { - if (!is_callable('smarty_function_escape_special_chars')) { + if (!isset($template->smarty->_cache[ '_required_sesc' ])) { require_once(SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php'); + $template->smarty->_cache[ '_required_sesc' ] = true; } - if (!is_callable('smarty_make_timestamp')) { + if (!isset($template->smarty->_cache[ '_required_smt' ])) { require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'); + $template->smarty->_cache[ '_required_smt' ] = true; } $prefix = "Time_"; $field_array = null; @@ -148,7 +152,9 @@ function smarty_function_html_select_time($params) if (isset($params[ 'time' ]) && is_array($params[ 'time' ])) { if (isset($params[ 'time' ][ $prefix . 'Hour' ])) { // $_REQUEST[$field_array] given - foreach (array('H' => 'Hour', 'i' => 'Minute', 's' => 'Second') as $_elementKey => $_elementName) { + foreach (array('H' => 'Hour', + 'i' => 'Minute', + 's' => 'Second') as $_elementKey => $_elementName) { $_variableName = '_' . strtolower($_elementName); $$_variableName = isset($params[ 'time' ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $prefix . $_elementName ] : @@ -161,7 +167,9 @@ function smarty_function_html_select_time($params) list($_hour, $_minute, $_second) = $time = explode('-', date('H-i-s', $time)); } elseif (isset($params[ 'time' ][ $field_array ][ $prefix . 'Hour' ])) { // $_REQUEST given - foreach (array('H' => 'Hour', 'i' => 'Minute', 's' => 'Second') as $_elementKey => $_elementName) { + foreach (array('H' => 'Hour', + 'i' => 'Minute', + 's' => 'Second') as $_elementKey => $_elementName) { $_variableName = '_' . strtolower($_elementName); $$_variableName = isset($params[ 'time' ][ $field_array ][ $prefix . $_elementName ]) ? $params[ 'time' ][ $field_array ][ $prefix . $_elementName ] : date($_elementKey); @@ -350,7 +358,10 @@ function smarty_function_html_select_time($params) } $_html = ''; - foreach (array('_html_hours', '_html_minutes', '_html_seconds', '_html_meridian') as $k) { + foreach (array('_html_hours', + '_html_minutes', + '_html_seconds', + '_html_meridian') as $k) { if (isset($$k)) { if ($_html) { $_html .= $field_separator; diff --git a/libs/plugins/modifier.date_format.php b/libs/plugins/modifier.date_format.php index bfe363c0..37cf85f2 100644 --- a/libs/plugins/modifier.date_format.php +++ b/libs/plugins/modifier.date_format.php @@ -35,8 +35,12 @@ function smarty_modifier_date_format($string, $format = null, $default_date = '' /** * require_once the {@link shared.make_timestamp.php} plugin */ - if (!is_callable('smarty_make_timestamp')) { - require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'); + static $is_loaded = false; + if (!$is_loaded) { + if (!is_callable('smarty_make_timestamp')) { + require_once(SMARTY_PLUGINS_DIR . 'shared.make_timestamp.php'); + } + $is_loaded = true; } if ($string != '' && $string != '0000-00-00' && $string != '0000-00-00 00:00:00') { $timestamp = smarty_make_timestamp($string); @@ -47,8 +51,20 @@ function smarty_modifier_date_format($string, $format = null, $default_date = '' } if ($formatter == 'strftime' || ($formatter == 'auto' && strpos($format, '%') !== false)) { if (Smarty::$_IS_WINDOWS) { - $_win_from = array('%D', '%h', '%n', '%r', '%R', '%t', '%T'); - $_win_to = array('%m/%d/%y', '%b', "\n", '%I:%M:%S %p', '%H:%M', "\t", '%H:%M:%S'); + $_win_from = array('%D', + '%h', + '%n', + '%r', + '%R', + '%t', + '%T'); + $_win_to = array('%m/%d/%y', + '%b', + "\n", + '%I:%M:%S %p', + '%H:%M', + "\t", + '%H:%M:%S'); if (strpos($format, '%e') !== false) { $_win_from[] = '%e'; $_win_to[] = sprintf('%\' 2d', date('j', $timestamp)); diff --git a/libs/plugins/modifier.escape.php b/libs/plugins/modifier.escape.php index 9c247b93..0bb37705 100644 --- a/libs/plugins/modifier.escape.php +++ b/libs/plugins/modifier.escape.php @@ -25,6 +25,8 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $double_encode = true) { static $_double_encode = null; + static $is_loaded1 = false; + static $is_loaded2 = false; if ($_double_encode === null) { $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>='); } @@ -46,7 +48,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $ // php <5.2.3 - prevent double encoding $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string); $string = htmlspecialchars($string, ENT_QUOTES, $char_set); - $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string); + $string = str_replace(array('%%%SMARTY_START%%%', + '%%%SMARTY_END%%%'), array('&', + ';'), $string); return $string; } @@ -67,7 +71,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $ $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string); $string = htmlspecialchars($string, ENT_QUOTES, $char_set); $string = - str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string); + str_replace(array('%%%SMARTY_START%%%', + '%%%SMARTY_END%%%'), array('&', + ';'), $string); return $string; } @@ -86,7 +92,9 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $ } else { $string = preg_replace('!&(#?\w+);!', '%%%SMARTY_START%%%\\1%%%SMARTY_END%%%', $string); $string = htmlentities($string, ENT_QUOTES, $char_set); - $string = str_replace(array('%%%SMARTY_START%%%', '%%%SMARTY_END%%%'), array('&', ';'), $string); + $string = str_replace(array('%%%SMARTY_START%%%', + '%%%SMARTY_END%%%'), array('&', + ';'), $string); return $string; } @@ -116,8 +124,11 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $ case 'hexentity': $return = ''; if (Smarty::$_MBSTRING) { - if (!is_callable('smarty_mb_to_unicode')) { - require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php'); + if (!$is_loaded1) { + if (!is_callable('smarty_mb_to_unicode')) { + require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php'); + $is_loaded1 = true; + } } $return = ''; foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) { @@ -137,8 +148,11 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $ case 'decentity': $return = ''; if (Smarty::$_MBSTRING) { - if (!is_callable('smarty_mb_to_unicode')) { - require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php'); + if (!$is_loaded1) { + if (!is_callable('smarty_mb_to_unicode')) { + require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php'); + } + $is_loaded1 = true; } $return = ''; foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) { @@ -157,7 +171,11 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $ case 'javascript': // escape quotes and backslashes, newlines, etc. - return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', + return strtr($string, array('\\' => '\\\\', + "'" => "\\'", + '"' => '\\"', + "\r" => '\\r', + "\n" => '\\n', ' '<\/')); case 'mail': @@ -165,17 +183,24 @@ function smarty_modifier_escape($string, $esc_type = 'html', $char_set = null, $ if (!is_callable('smarty_mb_str_replace')) { require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php'); } - return smarty_mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string); + return smarty_mb_str_replace(array('@', + '.'), array(' [AT] ', + ' [DOT] '), $string); } // no MBString fallback - return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string); + return str_replace(array('@', + '.'), array(' [AT] ', + ' [DOT] '), $string); case 'nonstd': // escape non-standard chars, such as ms document quotes $return = ''; if (Smarty::$_MBSTRING) { - if (!is_callable('smarty_mb_to_unicode')) { - require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php'); + if (!$is_loaded1) { + if (!is_callable('smarty_mb_to_unicode')) { + require_once(SMARTY_PLUGINS_DIR . 'shared.mb_unicode.php'); + } + $is_loaded1 = true; } foreach (smarty_mb_to_unicode($string, Smarty::$_CHARSET) as $unicode) { if ($unicode >= 126) { diff --git a/libs/plugins/modifier.replace.php b/libs/plugins/modifier.replace.php index 9dca259d..94450b77 100644 --- a/libs/plugins/modifier.replace.php +++ b/libs/plugins/modifier.replace.php @@ -24,11 +24,15 @@ */ function smarty_modifier_replace($string, $search, $replace) { + static $is_loaded = false; if (Smarty::$_MBSTRING) { - if (!is_callable('smarty_mb_str_replace')) { - require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php'); + if (!$is_loaded) { + if (!is_callable('smarty_mb_str_replace')) { + require_once(SMARTY_PLUGINS_DIR . 'shared.mb_str_replace.php'); + } + $is_loaded = true; } - return smarty_mb_str_replace($search, $replace, $string); + return smarty_mb_str_replace($search, $replace, $string); } return str_replace($search, $replace, $string); diff --git a/libs/plugins/modifiercompiler.escape.php b/libs/plugins/modifiercompiler.escape.php index 11565c2d..1315ac6e 100644 --- a/libs/plugins/modifiercompiler.escape.php +++ b/libs/plugins/modifiercompiler.escape.php @@ -23,8 +23,12 @@ function smarty_modifiercompiler_escape($params, $compiler) { static $_double_encode = null; - if (!is_callable('smarty_literal_compiler_param')) { - require_once(SMARTY_PLUGINS_DIR . 'shared.literal_compiler_param.php'); + static $is_loaded = false; + if (!$is_loaded) { + if (!is_callable('smarty_literal_compiler_param')) { + require_once(SMARTY_PLUGINS_DIR . 'shared.literal_compiler_param.php'); + } + $is_loaded = true; } if ($_double_encode === null) { $_double_encode = version_compare(PHP_VERSION, '5.2.3', '>=');