optimize compiled code of {foreach}

This commit is contained in:
Uwe Tews
2015-02-15 16:58:42 +01:00
parent 8082bc7471
commit 464b37c053
3 changed files with 116 additions and 50 deletions

View File

@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.22-dev/8'; const SMARTY_VERSION = '3.1.22-dev/9';
/** /**
* define variable scopes * define variable scopes
@@ -1493,6 +1493,14 @@ class Smarty extends Smarty_Internal_TemplateBase
$this->auto_literal = $auto_literal; $this->auto_literal = $auto_literal;
} }
/**
* @param boolean $force_compile
*/
public function setForceCompile($force_compile)
{
$this->force_compile = $force_compile;
}
/** /**
* @param boolean $merge_compiled_includes * @param boolean $merge_compiled_includes
*/ */

View File

@@ -64,103 +64,151 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
$key = null; $key = null;
} }
$this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $item, $key)); $this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $item, $key, true));
// maybe nocache because of nocache variables // maybe nocache because of nocache variables
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache; $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
if (isset($_attr['name'])) { if (isset($_attr['name'])) {
$name = $_attr['name']; $name = trim($_attr['name'], '\'"');
$has_name = true; $has_name = true;
$SmartyVarName = '$smarty.foreach.' . trim($name, '\'"') . '.'; $SmartyVarName = "\$smarty.foreach.{$name}.";
} else { } else {
$name = null;
$has_name = false; $has_name = false;
} }
$ItemVarName = '$' . trim($item, '\'"') . '@'; $ItemVarName = '$' . trim($item, '\'"') . '@';
// evaluates which Smarty variables and properties have to be computed // evaluates which Smarty variables and properties have to be computed
if ($has_name) { if ($has_name) {
$usesSmartyFirst = strpos($compiler->lex->data, $SmartyVarName . 'first') !== false; $useSmartyForeach = $usesSmartyFirst = strpos($compiler->lex->data, $SmartyVarName . 'first') !== false;
$usesSmartyLast = strpos($compiler->lex->data, $SmartyVarName . 'last') !== false; $useSmartyForeach = ($usesSmartyLast = strpos($compiler->lex->data, $SmartyVarName . 'last') !== false) || $useSmartyForeach;
$usesSmartyIndex = strpos($compiler->lex->data, $SmartyVarName . 'index') !== false; $useSmartyForeach = ($usesSmartyIndex = strpos($compiler->lex->data, $SmartyVarName . 'index') !== false) || $useSmartyForeach;
$usesSmartyIteration = strpos($compiler->lex->data, $SmartyVarName . 'iteration') !== false; $useSmartyForeach = ($usesSmartyIteration = (!$usesSmartyIndex && ($usesSmartyFirst || $usesSmartyLast)) || strpos($compiler->lex->data, $SmartyVarName . 'iteration') !== false) || $useSmartyForeach;
$usesSmartyShow = strpos($compiler->lex->data, $SmartyVarName . 'show') !== false; $useSmartyForeach = ($usesSmartyShow = strpos($compiler->lex->data, $SmartyVarName . 'show') !== false) || $useSmartyForeach;
$usesSmartyTotal = strpos($compiler->lex->data, $SmartyVarName . 'total') !== false; $useSmartyForeach = ($usesSmartyTotal = $usesSmartyLast ||strpos($compiler->lex->data, $SmartyVarName . 'total') !== false) || $useSmartyForeach;
} else { } else {
$usesSmartyFirst = false; $usesSmartyFirst = false;
$usesSmartyLast = false; $usesSmartyLast = false;
$usesSmartyTotal = false; $usesSmartyTotal = false;
$usesSmartyShow = false; $usesSmartyShow = false;
$useSmartyForeach = false;
} }
$usesPropFirst = $usesSmartyFirst || strpos($compiler->lex->data, $ItemVarName . 'first') !== false; $usesPropKey = strpos($compiler->lex->data, $ItemVarName . 'key') !== false;
$usesPropLast = $usesSmartyLast || strpos($compiler->lex->data, $ItemVarName . 'last') !== false; $usesPropFirst = strpos($compiler->lex->data, $ItemVarName . 'first') !== false;
$usesPropIndex = $usesPropFirst || strpos($compiler->lex->data, $ItemVarName . 'index') !== false; $usesPropLast = strpos($compiler->lex->data, $ItemVarName . 'last') !== false;
$usesPropIteration = $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'iteration') !== false; $usesPropIndex = strpos($compiler->lex->data, $ItemVarName . 'index') !== false;
$usesPropIteration = (!$usesPropIndex && ($usesPropFirst || $usesPropLast)) || strpos($compiler->lex->data, $ItemVarName . 'iteration') !== false;
$usesPropShow = strpos($compiler->lex->data, $ItemVarName . 'show') !== false; $usesPropShow = strpos($compiler->lex->data, $ItemVarName . 'show') !== false;
$usesPropTotal = $usesSmartyTotal || $usesSmartyShow || $usesPropShow || $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'total') !== false; $usesPropTotal = $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'total') !== false;
// generate output code
$output = "<?php "; $keyTerm = '';
$output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable; \$_smarty_tpl->tpl_vars[$item]->_loop = false;\n"; if ($usesPropKey) {
if ($key != null) { $keyTerm = "\$_smarty_tpl->tpl_vars[$item]->key => ";
$output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n"; } elseif ($key != null) {
$keyTerm = "\$_smarty_tpl->tpl_vars[$key]->value => ";
}
// generate output code
$output = "<?php\n";
$output .= "\$_from = $from;\n";
$output .= "if (!is_array(\$_from) && !is_object(\$_from)) {\n";
$output .= "settype(\$_from, 'array');\n";
$output .= "}\n";
$output .= "\$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
$output .= "\$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
if ($key != null) {
$output .= "\$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
} }
$output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
if ($usesPropTotal) { if ($usesPropTotal) {
$output .= " \$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n"; $output .= "\$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
} }
if ($usesPropIteration) { if ($usesPropIteration) {
$output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n"; $output .= "\$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
} }
if ($usesPropIndex) { if ($usesPropIndex) {
$output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n"; $output .= "\$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
} }
if ($usesPropShow) { if ($usesPropShow) {
$output .= " \$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n"; if ($usesPropTotal) {
$output .= "\$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->_count(\$_from) > 0);\n";
}
} }
if ($has_name) { if ($has_name) {
$prop = array();
if ($usesSmartyTotal) { if ($usesSmartyTotal) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n"; $prop['total'] = "'total' => ";
$prop['total'] .= $usesSmartyShow ? '$total = ' : '';
$prop['total'] .= '$_smarty_tpl->_count($_from)';
} }
if ($usesSmartyIteration) { if ($usesSmartyIteration) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n"; $prop['iteration'] = "'iteration' => 0";
} }
if ($usesSmartyIndex) { if ($usesSmartyIndex) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n"; $prop['index'] = "'index' => -1";
} }
if ($usesSmartyShow) { if ($usesSmartyShow) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['show']=(\$_smarty_tpl->tpl_vars[$item]->total > 0);\n"; $prop['show'] = "'show' => ";
if ($usesSmartyTotal) {
$prop['show'] .= "(\$total > 0)";
} else {
$prop['show'] .= "(\$_smarty_tpl->_count(\$_from) > 0)";
}
}
if ($useSmartyForeach) {
$_vars = 'array(' . join(', ', $prop) . ')';
$foreachVar = "'__foreach_{$name}'";
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar] = new Smarty_Variable({$_vars});\n";
} }
} }
$output .= "foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value) {\n\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n"; $output .= "foreach (\$_from as {$keyTerm}\$_smarty_tpl->tpl_vars[$item]->value) {\n";
if ($key != null) { $output .= "\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n";
$output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n"; if ($key != null && $usesPropKey) {
$output .= "\$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
} }
if ($usesPropIteration) { if ($usesPropIteration) {
$output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n"; $output .= "\$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
} }
if ($usesPropIndex) { if ($usesPropIndex) {
$output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n"; $output .= "\$_smarty_tpl->tpl_vars[$item]->index++;\n";
} }
if ($usesPropFirst) { if ($usesPropFirst) {
$output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n"; if ($usesPropIndex) {
$output .= "\$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index == 0;\n";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->iteration == 1;\n";
}
} }
if ($usesPropLast) { if ($usesPropLast) {
$output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n"; if ($usesPropIndex) {
$output .= "\$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->index + 1 == \$_smarty_tpl->tpl_vars[$item]->total;\n";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration == \$_smarty_tpl->tpl_vars[$item]->total;\n";
}
} }
if ($has_name) { if ($has_name) {
if ($usesSmartyFirst) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
}
if ($usesSmartyIteration) { if ($usesSmartyIteration) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n"; $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration']++;\n";
} }
if ($usesSmartyIndex) { if ($usesSmartyIndex) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n"; $output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['index']++;\n";
}
if ($usesSmartyFirst) {
if ($usesSmartyIndex) {
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] == 0;\n";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == 1;\n";
}
} }
if ($usesSmartyLast) { if ($usesSmartyLast) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n"; if ($usesSmartyIndex) {
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] + 1 == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
} else {
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
}
} }
} }
$output .= "\$foreachItemSav = \$_smarty_tpl->tpl_vars[$item];\n";
$output .= "?>"; $output .= "?>";
return $output; return $output;
@@ -189,10 +237,13 @@ class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
// check and get attributes // check and get attributes
$_attr = $this->getAttributes($compiler, $args); $_attr = $this->getAttributes($compiler, $args);
list($openTag, $nocache, $item, $key) = $this->closeTag($compiler, array('foreach')); list($openTag, $nocache, $item, $key, $foo) = $this->closeTag($compiler, array('foreach'));
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key)); $this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key, false));
$output = "<?php\n";
return "<?php }\nif (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>"; $output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreachItemSav;\n";
$output .= "}\n";
$output .= "if (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
return $output;
} }
} }
@@ -222,8 +273,13 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
$compiler->tag_nocache = true; $compiler->tag_nocache = true;
} }
list($openTag, $compiler->nocache, $item, $key) = $this->closeTag($compiler, array('foreach', 'foreachelse')); list($openTag, $compiler->nocache, $item, $key, $restore) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
$output = "<?php\n";
if ($restore) {
$output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreachItemSav;\n";
}
$output .= "}\n?>";
return "<?php } ?>"; return $output;
} }
} }

View File

@@ -33,7 +33,9 @@ class Smarty_Internal_Compile_Private_Special_Variable extends Smarty_Internal_C
if (!isset($compiler->smarty->security_policy) || $compiler->smarty->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)) { if (!isset($compiler->smarty->security_policy) || $compiler->smarty->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)) {
switch ($variable) { switch ($variable) {
case 'foreach': case 'foreach':
return "\$_smarty_tpl->getVariable('smarty')->value$parameter"; $name = trim($_index[1], "'");
$foreachVar = "'__foreach_{$name}'";
return "isset(\$_smarty_tpl->tpl_vars[$foreachVar]->value[{$_index[2]}]) ? \$_smarty_tpl->tpl_vars[$foreachVar]->value[{$_index[2]}] : null";
case 'section': case 'section':
return "\$_smarty_tpl->getVariable('smarty')->value$parameter"; return "\$_smarty_tpl->getVariable('smarty')->value$parameter";
case 'capture': case 'capture':