- optimization {foreach} compiler and processing

This commit is contained in:
uwetews
2016-05-16 17:57:06 +02:00
parent a62557e817
commit 0fcac38c70
4 changed files with 57 additions and 30 deletions

View File

@@ -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

View File

@@ -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 @@ class Smarty extends Smarty_Internal_TemplateBase
public function _realpath($path, $realpath = null)
{
$nds = DS == '/' ? '\\' : '/';
// normalize DS
// normalize DS
$path = str_replace($nds, DS, $path);
preg_match('%^(?<root>(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?<path>(?:[[:print:]]*))$%',
$path, $parts);

View File

@@ -169,20 +169,28 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
$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 = "<?php\n";
$output .= "\$_from = \$_smarty_tpl->smarty->ext->_foreach->init(\$_smarty_tpl, $from, " .
@@ -206,8 +214,8 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
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 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
$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 @@ class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
// 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 = "<?php\n";
$output .= "{$itemVar} = {$local}saved;\n";
$output .= "}\n";
$output .= "if (!{$itemVar}->_loop) {\n?>";
if ($restore == 2) {
$output .= "{$itemVar} = {$local}saved;\n";
}
$output .= "}\n} else {\n?>\n";
return $output;
}
}
@@ -305,12 +316,15 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
$this->closeTag($compiler, array('foreach', 'foreachelse'));
$output = "<?php\n";
if ($restore) {
if ($restore == 2) {
$output .= "{$itemVar} = {$local}saved;\n";
}
if ($restore > 0) {
$output .= "}\n";
}
$output .= "}\n";
$output .= "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl);\n";
$output .= "?>";
$output .= "?>\n";
return $output;
}
}

View File

@@ -38,24 +38,35 @@ class Smarty_Internal_Runtime_Foreach
$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 @@ class Smarty_Internal_Runtime_Foreach
$tpl->tpl_vars[ $namedVar ] = new Smarty_Variable($namedProp);
}
$this->stack[] = $saveVars;
return $from;
}