From 0fcac38c709aa41c4b0edb3d7e1e1d248dab044c Mon Sep 17 00:00:00 2001 From: uwetews Date: Mon, 16 May 2016 17:57:06 +0200 Subject: [PATCH] - optimization {foreach} compiler and processing --- change_log.txt | 5 ++- libs/Smarty.class.php | 4 +- .../smarty_internal_compile_foreach.php | 38 ++++++++++++------ .../smarty_internal_runtime_foreach.php | 40 ++++++++++++------- 4 files changed, 57 insertions(+), 30 deletions(-) diff --git a/change_log.txt b/change_log.txt index d9559eedc..3a8fefc1d 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,7 +1,10 @@  ===== 3.1.30-dev ===== (xx.xx.xx) + 16.05.2016 + - optimization {foreach} compiler and processing + 15.05.2016 - optimization and cleanup of resource code - + 10.05.2016 - optimization of inheritance processing diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index ebe320ec0..3df382c0d 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.30-dev/69'; + const SMARTY_VERSION = '3.1.30-dev/70'; /** * define variable scopes @@ -1139,7 +1139,7 @@ public function _getTemplateId($template_name, $cache_id = null, $compile_id = n public function _realpath($path, $realpath = null) { $nds = DS == '/' ? '\\' : '/'; - // normalize DS + // normalize DS $path = str_replace($nds, DS, $path); preg_match('%^(?(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?(?:[[:print:]]*))$%', $path, $parts); diff --git a/libs/sysplugins/smarty_internal_compile_foreach.php b/libs/sysplugins/smarty_internal_compile_foreach.php index 73eb68741..923596189 100644 --- a/libs/sysplugins/smarty_internal_compile_foreach.php +++ b/libs/sysplugins/smarty_internal_compile_foreach.php @@ -169,20 +169,28 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ $itemAttr[ 'iteration' ] = true; $itemAttr[ 'total' ] = true; } + if (isset($namedAttr[ 'show' ])) { + $namedAttr[ 'total' ] = true; + } + if (isset($itemAttr[ 'show' ])) { + $itemAttr[ 'total' ] = true; + } $keyTerm = ''; if (isset($itemAttr[ 'key' ])) { $keyTerm = "{$itemVar}->key => "; + unset($itemAttr[ 'key' ]); } elseif (isset($attributes[ 'key' ])) { $keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => "; } if ($this->isNamed) { $foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']"; } - $this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $local, $itemVar, true)); + $needTotal = isset($itemAttr[ 'total' ]); + // Register tag + $this->openTag($compiler, 'foreach', + array('foreach', $compiler->nocache, $local, $itemVar, empty($itemAttr) ? 1 : 2)); // maybe nocache because of nocache variables $compiler->nocache = $compiler->nocache | $compiler->tag_nocache; - $needTotal = isset($itemAttr[ 'show' ]) || isset($itemAttr[ 'total' ]) || isset($namedAttr[ 'total' ]) || - isset($namedAttr[ 'show' ]) || isset($itemAttr[ 'last' ]) || isset($namedAttr[ 'last' ]); // generate output code $output = "smarty->ext->_foreach->init(\$_smarty_tpl, $from, " . @@ -206,8 +214,8 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ if (isset($itemAttr[ 'index' ])) { $output .= "{$itemVar}->index = -1;\n"; } + $output .= "if (\$_from !== null) {\n"; $output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n"; - $output .= "{$itemVar}->_loop = true;\n"; if (isset($attributes[ 'key' ]) && isset($itemAttr[ 'key' ])) { $output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n"; } @@ -237,7 +245,9 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ $output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] == {$foreachVar}->value['total'];\n"; } } - $output .= "{$local}saved = {$itemVar};\n"; + if (!empty($itemAttr)) { + $output .= "{$local}saved = {$itemVar};\n"; + } $output .= "?>"; return $output; @@ -266,12 +276,13 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ // check and get attributes $_attr = $this->getAttributes($compiler, $args); - list($openTag, $nocache, $local, $itemVar, $foo) = $this->closeTag($compiler, array('foreach')); - $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, false)); + list($openTag, $nocache, $local, $itemVar, $restore) = $this->closeTag($compiler, array('foreach')); + $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, 0)); $output = "_loop) {\n?>"; + if ($restore == 2) { + $output .= "{$itemVar} = {$local}saved;\n"; + } + $output .= "}\n} else {\n?>\n"; return $output; } } @@ -305,12 +316,15 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ $this->closeTag($compiler, array('foreach', 'foreachelse')); $output = " 0) { + $output .= "}\n"; + } $output .= "}\n"; $output .= "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl);\n"; - $output .= "?>"; + $output .= "?>\n"; return $output; } } diff --git a/libs/sysplugins/smarty_internal_runtime_foreach.php b/libs/sysplugins/smarty_internal_runtime_foreach.php index 77c42ec0a..6d8737fe2 100644 --- a/libs/sysplugins/smarty_internal_runtime_foreach.php +++ b/libs/sysplugins/smarty_internal_runtime_foreach.php @@ -38,24 +38,35 @@ public function init(Smarty_Internal_Template $tpl, $from, $item, $needTotal = f $properties = array()) { $saveVars = array(); - if (isset($tpl->tpl_vars[ $item ])) { - $saveVars[ $item ] = $tpl->tpl_vars[ $item ]; - } - if ($key) { - if (isset($tpl->tpl_vars[ $key ])) { - $saveVars[ $key ] = $tpl->tpl_vars[ $key ]; - } - $tpl->tpl_vars[ $key ] = new Smarty_Variable(null, $tpl->isRenderingCache); - } if (!is_array($from) && !is_object($from)) { settype($from, 'array'); } - $total = $needTotal ? $this->count($from) : 1; - $tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache); - if ($needTotal) { - $tpl->tpl_vars[ $item ]->total = $total; + $total = ($needTotal || isset($properties[ 'total' ])) ? $this->count($from) : 1; + if (empty($from)) { + $from = null; + $total = 0; + if ($needTotal) { + if (isset($tpl->tpl_vars[ $item ])) { + $saveVars[ $item ] = $tpl->tpl_vars[ $item ]; + } + $tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache); + $tpl->tpl_vars[ $item ]->total = 0; + } + } else { + if (isset($tpl->tpl_vars[ $item ])) { + $saveVars[ $item ] = $tpl->tpl_vars[ $item ]; + } + if ($key) { + if (isset($tpl->tpl_vars[ $key ])) { + $saveVars[ $key ] = $tpl->tpl_vars[ $key ]; + } + $tpl->tpl_vars[ $key ] = new Smarty_Variable(null, $tpl->isRenderingCache); + } + $tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache); + if ($needTotal) { + $tpl->tpl_vars[ $item ]->total = $total; + } } - $tpl->tpl_vars[ $item ]->_loop = false; if ($name) { $namedVar = "__smarty_foreach_{$name}"; if (isset($tpl->tpl_vars[ $namedVar ])) { @@ -77,7 +88,6 @@ public function init(Smarty_Internal_Template $tpl, $from, $item, $needTotal = f $tpl->tpl_vars[ $namedVar ] = new Smarty_Variable($namedProp); } $this->stack[] = $saveVars; - return $from; }