Skip to content

Commit

Permalink
- optimization {foreach} compiler and processing
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed May 16, 2016
1 parent a62557e commit 0fcac38
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
5 changes: 4 additions & 1 deletion change_log.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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('%^(?<root>(?:[[:alpha:]]:[\\\\]|/|[\\\\]{2}[[:alpha:]]+|[[:print:]]{2,}:[/]{2}|[\\\\])?)(?<path>(?:[[:print:]]*))$%',
$path, $parts);
Expand Down
38 changes: 26 additions & 12 deletions libs/sysplugins/smarty_internal_compile_foreach.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<?php\n";
$output .= "\$_from = \$_smarty_tpl->smarty->ext->_foreach->init(\$_smarty_tpl, $from, " .
Expand All @@ -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";
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "<?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;
}
}
Expand Down Expand Up @@ -305,12 +316,15 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
$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;
}
}
40 changes: 25 additions & 15 deletions libs/sysplugins/smarty_internal_runtime_foreach.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ])) {
Expand All @@ -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;
}

Expand Down

0 comments on commit 0fcac38

Please sign in to comment.