diff --git a/NEWS b/NEWS index 4dd9f32f..c62a5a43 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,4 @@ + - optimized for loops with count() function calls (monte) - add month_value_format attribute to html_select_date plugin (Gary Loescher, Monte) - made it possible to use simple variables inside [] for diff --git a/Smarty.class.php b/Smarty.class.php index c08832c4..4b9f2b42 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -1157,7 +1157,7 @@ function _generate_debug_output() { $this->_config[1]['files'][$file] = true; } } else if ($scope == 'global') - for ($i = 1; $i < count($this->_config); $i++) { + for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++) { if (!isset($this->_config[$i]['files'][$file])) { $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file)); $this->_config[$i]['files'][$file] = true; @@ -1170,7 +1170,7 @@ function _generate_debug_output() { if (count($this->_config) > 0) $this->_config[1]['vars'] = array_merge($this->_config[1]['vars'], $this->_conf_obj->get($file, $section)); } else if ($scope == 'global') - for ($i = 1; $i < count($this->_config); $i++) + for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++) $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file, $section)); } @@ -1194,7 +1194,7 @@ function _generate_debug_output() { $results, $match); list($cached_inserts, $insert_args) = $match; - for ($i = 0; $i < count($cached_inserts); $i++) { + for ($i = 0, $for_max = count($cached_inserts); $i < $for_max; $i++) { if ($this->debugging) { $debug_start_time = $this->_get_microtime(); } diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index 8534a460..3b5ea02d 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -110,11 +110,11 @@ class Smarty_Compiler extends Smarty { $text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $template_source); /* loop through text blocks */ - for ($curr_tb = 0; $curr_tb < count($text_blocks); $curr_tb++) { + for ($curr_tb = 0, $for_max = count($text_blocks); $curr_tb < $for_max; $curr_tb++) { /* match anything within */ if (preg_match_all('!(<\?[^?]*?\?>|)!is', $text_blocks[$curr_tb], $sp_match)) { /* found at least one match, loop through each one */ - for ($curr_sp = 0; $curr_sp < count($sp_match[0]); $curr_sp++) { + for ($curr_sp = 0, $for_max2 = count($sp_match[0]); $curr_sp < $for_max2; $curr_sp++) { if (preg_match('!^(<\?(php\s|\s|=\s)|)!is', $sp_match[0][$curr_sp])) { /* php tag */ if ($this->php_handling == SMARTY_PHP_PASSTHRU) { @@ -140,7 +140,7 @@ class Smarty_Compiler extends Smarty { /* Compile the template tags into PHP code. */ $compiled_tags = array(); - for ($i = 0; $i < count($template_tags); $i++) { + for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) { $this->_current_line_no += substr_count($text_blocks[$i], "\n"); $compiled_tags[] = $this->_compile_tag($template_tags[$i]); $this->_current_line_no += substr_count($template_tags[$i], "\n"); @@ -149,7 +149,7 @@ class Smarty_Compiler extends Smarty { $template_compiled = ''; /* Interleave the compiled contents and text blocks to get the final result. */ - for ($i = 0; $i < count($compiled_tags); $i++) { + for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) { $template_compiled .= $text_blocks[$i].$compiled_tags[$i]; } $template_compiled .= $text_blocks[$i]; @@ -159,7 +159,7 @@ class Smarty_Compiler extends Smarty { $strip_tags = $match[0]; $strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags); $strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified); - for ($i = 0; $i < count($strip_tags); $i++) + for ($i = 0, $for_max = count($strip_tags); $i < $for_max; $i++) $template_compiled = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $this->quote_replace($strip_tags_modified[$i]), $template_compiled, 1); @@ -844,7 +844,7 @@ class Smarty_Compiler extends Smarty { $is_arg_stack = array(); - for ($i = 0; $i < count($tokens); $i++) { + for ($i = 0, $for_max = count($tokens); $i < $for_max; $i++) { $token = &$tokens[$i]; switch ($token) { @@ -1219,7 +1219,7 @@ class Smarty_Compiler extends Smarty { preg_match_all('!\|(@?\w+)((?>:(?:'. $qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $match); list(, $modifiers, $modifier_arg_strings) = $match; - for ($i = 0; $i < count($modifiers); $i++) { + for ($i = 0, $for_max = count($modifiers); $i < $for_max; $i++) { $modifier_name = $modifiers[$i]; preg_match_all('!:(' . $qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$i], $match); $modifier_args = $match[1]; diff --git a/docs/appendixes.sgml b/docs/appendixes.sgml index b3502313..9e8537ed 100644 --- a/docs/appendixes.sgml +++ b/docs/appendixes.sgml @@ -400,7 +400,7 @@ href="mailto:%62%6f%62%40%6d%65%2e%6e%65%74">&#x62;&#x6f;&#x62;&a Smarty's homepage is located at http://www.phpinsider.com/php/code/Smarty/. You can join the mailing list by sending an e-mail to - subscribe-smarty@lists.ispi.net. An archive of the mailing list can be + smarty-general-subscribe@lists.php.net. An archive of the mailing list can be viewed at http://marc.theaimsgroup.com/?l=smarty&r=1&w=2 diff --git a/docs/designers.sgml b/docs/designers.sgml index e34ec955..11f2c55d 100644 --- a/docs/designers.sgml +++ b/docs/designers.sgml @@ -3737,6 +3737,16 @@ OUTPUT: math function will be assigned to this template variable instead of being output to the template. + + Technical Note + + math is an expensive function in performance due to its use of + the php eval() function. Doing the math in PHP is much more + efficient, so whenever possible do the math calculations in PHP + and assign the results to the template. Definately avoid + repetitive math function calls, like within section loops. + + math diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index c08832c4..4b9f2b42 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -1157,7 +1157,7 @@ function _generate_debug_output() { $this->_config[1]['files'][$file] = true; } } else if ($scope == 'global') - for ($i = 1; $i < count($this->_config); $i++) { + for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++) { if (!isset($this->_config[$i]['files'][$file])) { $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file)); $this->_config[$i]['files'][$file] = true; @@ -1170,7 +1170,7 @@ function _generate_debug_output() { if (count($this->_config) > 0) $this->_config[1]['vars'] = array_merge($this->_config[1]['vars'], $this->_conf_obj->get($file, $section)); } else if ($scope == 'global') - for ($i = 1; $i < count($this->_config); $i++) + for ($i = 1, $for_max = count($this->_config); $i < $for_max; $i++) $this->_config[$i]['vars'] = array_merge($this->_config[$i]['vars'], $this->_conf_obj->get($file, $section)); } @@ -1194,7 +1194,7 @@ function _generate_debug_output() { $results, $match); list($cached_inserts, $insert_args) = $match; - for ($i = 0; $i < count($cached_inserts); $i++) { + for ($i = 0, $for_max = count($cached_inserts); $i < $for_max; $i++) { if ($this->debugging) { $debug_start_time = $this->_get_microtime(); } diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index 8534a460..3b5ea02d 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -110,11 +110,11 @@ class Smarty_Compiler extends Smarty { $text_blocks = preg_split("!{$ldq}.*?{$rdq}!s", $template_source); /* loop through text blocks */ - for ($curr_tb = 0; $curr_tb < count($text_blocks); $curr_tb++) { + for ($curr_tb = 0, $for_max = count($text_blocks); $curr_tb < $for_max; $curr_tb++) { /* match anything within */ if (preg_match_all('!(<\?[^?]*?\?>|)!is', $text_blocks[$curr_tb], $sp_match)) { /* found at least one match, loop through each one */ - for ($curr_sp = 0; $curr_sp < count($sp_match[0]); $curr_sp++) { + for ($curr_sp = 0, $for_max2 = count($sp_match[0]); $curr_sp < $for_max2; $curr_sp++) { if (preg_match('!^(<\?(php\s|\s|=\s)|)!is', $sp_match[0][$curr_sp])) { /* php tag */ if ($this->php_handling == SMARTY_PHP_PASSTHRU) { @@ -140,7 +140,7 @@ class Smarty_Compiler extends Smarty { /* Compile the template tags into PHP code. */ $compiled_tags = array(); - for ($i = 0; $i < count($template_tags); $i++) { + for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) { $this->_current_line_no += substr_count($text_blocks[$i], "\n"); $compiled_tags[] = $this->_compile_tag($template_tags[$i]); $this->_current_line_no += substr_count($template_tags[$i], "\n"); @@ -149,7 +149,7 @@ class Smarty_Compiler extends Smarty { $template_compiled = ''; /* Interleave the compiled contents and text blocks to get the final result. */ - for ($i = 0; $i < count($compiled_tags); $i++) { + for ($i = 0, $for_max = count($compiled_tags); $i < $for_max; $i++) { $template_compiled .= $text_blocks[$i].$compiled_tags[$i]; } $template_compiled .= $text_blocks[$i]; @@ -159,7 +159,7 @@ class Smarty_Compiler extends Smarty { $strip_tags = $match[0]; $strip_tags_modified = preg_replace("!{$ldq}/?strip{$rdq}|[\t ]+$|^[\t ]+!m", '', $strip_tags); $strip_tags_modified = preg_replace('![\r\n]+!m', '', $strip_tags_modified); - for ($i = 0; $i < count($strip_tags); $i++) + for ($i = 0, $for_max = count($strip_tags); $i < $for_max; $i++) $template_compiled = preg_replace("!{$ldq}strip{$rdq}.*?{$ldq}/strip{$rdq}!s", $this->quote_replace($strip_tags_modified[$i]), $template_compiled, 1); @@ -844,7 +844,7 @@ class Smarty_Compiler extends Smarty { $is_arg_stack = array(); - for ($i = 0; $i < count($tokens); $i++) { + for ($i = 0, $for_max = count($tokens); $i < $for_max; $i++) { $token = &$tokens[$i]; switch ($token) { @@ -1219,7 +1219,7 @@ class Smarty_Compiler extends Smarty { preg_match_all('!\|(@?\w+)((?>:(?:'. $qstr_regexp . '|[^|]+))*)!', '|' . $modifier_string, $match); list(, $modifiers, $modifier_arg_strings) = $match; - for ($i = 0; $i < count($modifiers); $i++) { + for ($i = 0, $for_max = count($modifiers); $i < $for_max; $i++) { $modifier_name = $modifiers[$i]; preg_match_all('!:(' . $qstr_regexp . '|[^:]+)!', $modifier_arg_strings[$i], $match); $modifier_args = $match[1]; diff --git a/libs/plugins/function.html_options.php b/libs/plugins/function.html_options.php index 969de058..0d91e6e2 100644 --- a/libs/plugins/function.html_options.php +++ b/libs/plugins/function.html_options.php @@ -29,7 +29,7 @@ function smarty_function_html_options($params, &$smarty) } else { settype($output, 'array'); settype($values, 'array'); - for ($i = 0; $i < count($output); $i++) { + for ($i = 0, $for_max = count($output); $i < $for_max; $i++) { /* By default, check value against $selected */ $sel_check = $values[$i]; $html_result .= "