- optimization of {assign}, {if} and {while} compiled code

This commit is contained in:
uwetews
2015-12-31 02:20:47 +01:00
parent 7eb56ac5e3
commit c60cdb3e64
10 changed files with 113 additions and 86 deletions

View File

@@ -1,4 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)  ===== 3.1.30-dev ===== (xx.xx.xx)
31.12.2015
- optimization of {assign}, {if} and {while} compiled code
30.12.2015 30.12.2015
- bugfix plugin names starting with "php" did not compile https://github.com/smarty-php/smarty/issues/147 - bugfix plugin names starting with "php" did not compile https://github.com/smarty-php/smarty/issues/147

View File

@@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.30-dev/13'; const SMARTY_VERSION = '3.1.30-dev/14';
/** /**
* define variable scopes * define variable scopes

View File

@@ -58,7 +58,8 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase
if (isset($_attr[ 'scope' ])) { if (isset($_attr[ 'scope' ])) {
$_attr[ 'scope' ] = trim($_attr[ 'scope' ], "'\""); $_attr[ 'scope' ] = trim($_attr[ 'scope' ], "'\"");
if (!isset($this->valid_scopes[ $_attr[ 'scope' ] ])) { if (!isset($this->valid_scopes[ $_attr[ 'scope' ] ])) {
$compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null, true); $compiler->trigger_template_error("illegal value '{$_attr['scope']}' for \"scope\" attribute", null,
true);
} }
if ($_attr[ 'scope' ] != 'local') { if ($_attr[ 'scope' ] != 'local') {
if ($_attr[ 'scope' ] == 'parent') { if ($_attr[ 'scope' ] == 'parent') {
@@ -72,27 +73,29 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase
} elseif ($_attr[ 'scope' ] == 'tpl_root') { } elseif ($_attr[ 'scope' ] == 'tpl_root') {
$_scope = Smarty::SCOPE_TPL_ROOT; $_scope = Smarty::SCOPE_TPL_ROOT;
} }
$_scope += (isset($_attr['bubble_up']) && $_attr['bubble_up'] == 'false') ? 0 : Smarty::SCOPE_BUBBLE_UP; $_scope += (isset($_attr[ 'bubble_up' ]) && $_attr[ 'bubble_up' ] == 'false') ? 0 :
Smarty::SCOPE_BUBBLE_UP;
} }
} }
// compiled output
if (isset($parameter[ 'smarty_internal_index' ])) { if (isset($parameter[ 'smarty_internal_index' ])) {
$output = $output = "<?php \$_smarty_tpl->_createLocalArrayVariable({$_attr['var']}, {$_nocache});\n";
"<?php \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, $_attr[var], $_nocache);\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value$parameter[smarty_internal_index] = $_attr[value];"; $output .= "\$_smarty_tpl->tpl_vars[{$_attr['var']}]->value{$parameter['smarty_internal_index']} = {$_attr['value']};\n";
if ($_scope != Smarty::SCOPE_LOCAL) {
$output .= "\$_smarty_tpl->ext->_updateScope->updateScope(\$_smarty_tpl, {$_attr['var']}, {$_scope});\n";
} else { } else {
// implement Smarty2's behaviour of variables assigned by reference $output .= "if (\$_smarty_tpl->scope & Smarty::SCOPE_BUBBLE_UP) {\n";
if ($compiler->template->smarty instanceof SmartyBC) { $output .= "\$_smarty_tpl->ext->_updateScope->updateScope(\$_smarty_tpl, {$_attr['var']});\n}\n";
$output =
"<?php if (isset(\$_smarty_tpl->tpl_vars[$_attr[var]])) {\$_smarty_tpl->tpl_vars[$_attr[var]] = clone \$_smarty_tpl->tpl_vars[$_attr[var]];";
$output .= "\n\$_smarty_tpl->tpl_vars[$_attr[var]]->value = $_attr[value]; \$_smarty_tpl->tpl_vars[$_attr[var]]->nocache = $_nocache;";
$output .= "\n} else \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_Variable($_attr[value], $_nocache);";
} else {
$output = "<?php \$_smarty_tpl->tpl_vars[$_attr[var]] = new Smarty_Variable($_attr[value], $_nocache);";
} }
}
$output .= "\n\$_smarty_tpl->ext->_updateScope->updateScope(\$_smarty_tpl, $_attr[var], $_scope);";
$output .= '?>'; $output .= '?>';
} else {
if ($compiler->template->smarty instanceof SmartyBC) {
$_smartyBC = 'true';
} else {
$_smartyBC = 'false';
}
$output =
"<?php \$_smarty_tpl->_assignInScope({$_attr['var']}, {$_attr['value']}, {$_nocache}, {$_scope}, {$_smartyBC});\n?>";
}
return $output; return $output;
} }
} }

View File

@@ -58,7 +58,7 @@ class Smarty_Internal_Compile_If extends Smarty_Internal_CompileBase
if (is_array($parameter['if condition']['var'])) { if (is_array($parameter['if condition']['var'])) {
$_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . $_output = "<?php if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
"]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " . $parameter['if condition']['var']['var'] . "]->value)) \$_smarty_tpl->_createLocalArrayVariable(" . $parameter['if condition']['var']['var'] .
"$_nocache);\n"; "$_nocache);\n";
$_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
$parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['var']['smarty_internal_index'] . " = " .
@@ -162,7 +162,7 @@ class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase
$_output = "<?php } else { if (!isset(\$_smarty_tpl->tpl_vars[" . $_output = "<?php } else { if (!isset(\$_smarty_tpl->tpl_vars[" .
$parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" .
$parameter['if condition']['var']['var'] . $parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " . "]->value)) \$_smarty_tpl->_createLocalArrayVariable(" .
$parameter['if condition']['var']['var'] . "$_nocache);\n"; $parameter['if condition']['var']['var'] . "$_nocache);\n";
$_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
$parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['var']['smarty_internal_index'] . " = " .
@@ -195,7 +195,7 @@ class Smarty_Internal_Compile_Elseif extends Smarty_Internal_CompileBase
$parameter['if condition']['var']['var'] . $parameter['if condition']['var']['var'] .
"]) || !is_array(\$_smarty_tpl->tpl_vars[" . "]) || !is_array(\$_smarty_tpl->tpl_vars[" .
$parameter['if condition']['var']['var'] . $parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " . "]->value)) \$_smarty_tpl->_createLocalArrayVariable(" .
$parameter['if condition']['var']['var'] . "$_nocache);\n"); $parameter['if condition']['var']['var'] . "$_nocache);\n");
$_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $_output .= "if (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
$parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['var']['smarty_internal_index'] . " = " .

View File

@@ -60,7 +60,7 @@ class Smarty_Internal_Compile_While extends Smarty_Internal_CompileBase
if (is_array($parameter['if condition']['var'])) { if (is_array($parameter['if condition']['var'])) {
$_output .= "if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . $_output .= "if (!isset(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
"]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]) || !is_array(\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] .
"]->value)) \$_smarty_tpl->smarty->ext->_var->createLocalArrayVariable(\$_smarty_tpl, " . $parameter['if condition']['var']['var'] . "]->value)) \$_smarty_tpl->_createLocalArrayVariable(" . $parameter['if condition']['var']['var'] .
"$_nocache);\n"; "$_nocache);\n";
$_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" . $_output .= "while (\$_smarty_tpl->tpl_vars[" . $parameter['if condition']['var']['var'] . "]->value" .
$parameter['if condition']['var']['smarty_internal_index'] . " = " . $parameter['if condition']['var']['smarty_internal_index'] . " = " .

View File

@@ -12,7 +12,6 @@
* *
* @property Smarty_Internal_Runtime_Inheritance $_inheritance * @property Smarty_Internal_Runtime_Inheritance $_inheritance
* @property Smarty_Internal_Runtime_TplFunction $_tplFunction * @property Smarty_Internal_Runtime_TplFunction $_tplFunction
* @property Smarty_Internal_Runtime_Var $_var
* @property Smarty_Internal_Runtime_Foreach $_foreach * @property Smarty_Internal_Runtime_Foreach $_foreach
* @property Smarty_Internal_Runtime_WriteFile $_writeFile * @property Smarty_Internal_Runtime_WriteFile $_writeFile
* @property Smarty_Internal_Runtime_CodeFrame $_codeFrame * @property Smarty_Internal_Runtime_CodeFrame $_codeFrame

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /**
* Runtime Methods updateScope * Runtime Extension updateScope
* *
* @package Smarty * @package Smarty
* @subpackage PluginsInternal * @subpackage PluginsInternal
@@ -13,23 +13,34 @@ class Smarty_Internal_Runtime_UpdateScope
/** /**
* Update new assigned template variable in other effected scopes * Update new assigned template variable in other effected scopes
* *
* @param \Smarty_Internal_Template $tpl template object * @param \Smarty_Internal_Data $tpl data object
* @param string $varName variable name * @param string $varName variable name
* @param int $scope scope to which bubble up variable value * @param int $scope scope to which bubble up variable value
*/ */
public function updateScope(Smarty_Internal_Template $tpl, $varName, $scope = Smarty::SCOPE_LOCAL) public function updateScope(Smarty_Internal_Data $tpl, $varName, $scope = Smarty::SCOPE_LOCAL)
{ {
if (!$scope && !$tpl->scope) { $scopes = array();
if ($scope) {
$scopes[] = $scope;
}
if ($tpl->scope) {
$scopes[] = $tpl->scope;
}
if (empty($scopes)) {
return; return;
} }
foreach (array($scope, $tpl->scope) as $s) { /* @var Smarty_Internal_Data $ptr */
$ptr = null;
foreach ($scopes as $s) {
$s = ($bubble_up = $s >= Smarty::SCOPE_BUBBLE_UP) ? $s - Smarty::SCOPE_BUBBLE_UP : $s; $s = ($bubble_up = $s >= Smarty::SCOPE_BUBBLE_UP) ? $s - Smarty::SCOPE_BUBBLE_UP : $s;
if ($bubble_up && $s) { if ($bubble_up && $s) {
if (isset($tpl->parent)) {
$ptr = $tpl->parent; $ptr = $tpl->parent;
if (isset($ptr)) {
$ptr->tpl_vars[ $varName ] = $tpl->tpl_vars[ $varName ]; $ptr->tpl_vars[ $varName ] = $tpl->tpl_vars[ $varName ];
if (isset($ptr->parent)) {
$ptr = $ptr->parent; $ptr = $ptr->parent;
} }
}
if ($s == Smarty::SCOPE_PARENT) { if ($s == Smarty::SCOPE_PARENT) {
continue; continue;
} }

View File

@@ -1,33 +0,0 @@
<?php
/**
* Runtime Methods createLocalArrayVariable
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*
**/
class Smarty_Internal_Runtime_Var
{
/**
* Template code runtime function to create a local Smarty variable for array assignments
*
* @param \Smarty_Internal_Template $tpl template object
* @param string $varName template variable name
* @param bool $nocache cache mode of variable
*/
public function createLocalArrayVariable(Smarty_Internal_Template $tpl, $varName, $nocache = false)
{
if (!isset($tpl->tpl_vars[$varName])) {
$tpl->tpl_vars[$varName] = new Smarty_Variable(array(), $nocache);
} else {
$tpl->tpl_vars[$varName] = clone $tpl->tpl_vars[$varName];
if (!(is_array($tpl->tpl_vars[$varName]->value) ||
$tpl->tpl_vars[$varName]->value instanceof ArrayAccess)
) {
settype($tpl->tpl_vars[$varName]->value, 'array');
}
}
}
}

View File

@@ -393,6 +393,51 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
return isset($this->parent) && $this->parent->_objType == 2; return isset($this->parent) && $this->parent->_objType == 2;
} }
/**
* Assign variable in scope
*
* @param string $varName variable name
* @param mixed $value value
* @param bool $nocache nocache flag
* @param int $scope scope into which variable shall be assigned
* @param bool $smartyBC true if called in Smarty bc class
*
* @throws \SmartyException
*/
public function _assignInScope($varName, $value, $nocache, $scope, $smartyBC)
{
if ($smartyBC && isset($this->tpl_vars[ $varName ])) {
$this->tpl_vars[ $varName ] = clone $this->tpl_vars[ $varName ];
$this->tpl_vars[ $varName ]->value = $value;
$this->tpl_vars[ $varName ]->nocache = $nocache;
} else {
$this->tpl_vars[ $varName ] = new Smarty_Variable($value, $nocache);
}
if ($scope || $this->scope & Smarty::SCOPE_BUBBLE_UP) {
$this->ext->_updateScope->updateScope($this, $varName, $scope);
}
}
/**
* Template code runtime function to create a local Smarty variable for array assignments
*
* @param string $varName template variable name
* @param bool $nocache cache mode of variable
*/
public function _createLocalArrayVariable($varName, $nocache = false)
{
if (!isset($this->tpl_vars[ $varName ])) {
$this->tpl_vars[ $varName ] = new Smarty_Variable(array(), $nocache);
} else {
$this->tpl_vars[ $varName ] = clone $this->tpl_vars[ $varName ];
if (!(is_array($this->tpl_vars[ $varName ]->value) ||
$this->tpl_vars[ $varName ]->value instanceof ArrayAccess)
) {
settype($this->tpl_vars[ $varName ]->value, 'array');
}
}
}
/** /**
* This function is executed automatically when a compiled or cached template file is included * This function is executed automatically when a compiled or cached template file is included
* - Decode saved properties from compiled template and cache files * - Decode saved properties from compiled template and cache files

View File

@@ -481,7 +481,6 @@ class Smarty_Internal_TestInstall
'smarty_internal_runtime_tplfunction.php' => true, 'smarty_internal_runtime_tplfunction.php' => true,
'smarty_internal_runtime_updatecache.php' => true, 'smarty_internal_runtime_updatecache.php' => true,
'smarty_internal_runtime_updatescope.php' => true, 'smarty_internal_runtime_updatescope.php' => true,
'smarty_internal_runtime_var.php' => true,
'smarty_internal_runtime_writefile.php' => true, 'smarty_internal_runtime_writefile.php' => true,
'smarty_internal_smartytemplatecompiler.php' => true, 'smarty_internal_smartytemplatecompiler.php' => true,
'smarty_internal_template.php' => true, 'smarty_internal_template.php' => true,