- cache template object of {include} if same template is included several times

This commit is contained in:
uwetews
2015-08-18 02:55:53 +02:00
parent e8252906ba
commit 457a0486f5
6 changed files with 60 additions and 8 deletions

View File

@@ -2,6 +2,7 @@
xx.xx.2015 xx.xx.2015
- introduce Smarty::$resource_cache_mode and cache template object of {include} inside loop - introduce Smarty::$resource_cache_mode and cache template object of {include} inside loop
- load seldom used Smarty API methods dynamically to reduce memory footprint - load seldom used Smarty API methods dynamically to reduce memory footprint
- cache template object of {include} if same template is included several times
06.08.2015 06.08.2015
- avoid possible circular object references caused by parser/lexer objects - avoid possible circular object references caused by parser/lexer objects

View File

@@ -70,6 +70,14 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
// save possible attributes // save possible attributes
$include_file = $_attr['file']; $include_file = $_attr['file'];
if ($compiler->has_variable_string ||
!((substr_count($include_file, '"') == 2 || substr_count($include_file, "'") == 2)) ||
substr_count($include_file, '(') != 0 || substr_count($include_file, '$_smarty_tpl->') != 0
) {
$variable_template = true;
} else {
$variable_template = false;
}
if (isset($_attr['assign'])) { if (isset($_attr['assign'])) {
// output will be stored in a smarty variable instead of being displayed // output will be stored in a smarty variable instead of being displayed
@@ -89,7 +97,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
} }
// //
if ($compiler->loopNesting > 0) { if ($variable_template || $compiler->loopNesting > 0) {
$_cache_tpl = 'true'; $_cache_tpl = 'true';
} else { } else {
$_cache_tpl = 'false'; $_cache_tpl = 'false';
@@ -115,10 +123,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
if ($merge_compiled_includes && $_attr['inline'] !== true) { if ($merge_compiled_includes && $_attr['inline'] !== true) {
// variable template name ? // variable template name ?
if ($compiler->has_variable_string || if ($variable_template) {
!((substr_count($include_file, '"') == 2 || substr_count($include_file, "'") == 2)) ||
substr_count($include_file, '(') != 0 || substr_count($include_file, '$_smarty_tpl->') != 0
) {
$merge_compiled_includes = false; $merge_compiled_includes = false;
if ($compiler->template->caching) { if ($compiler->template->caching) {
// must use individual cache file // must use individual cache file
@@ -262,6 +267,8 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
} else { } else {
$_vars = 'array()'; $_vars = 'array()';
} }
$this->logInclude($compiler, $include_file, $variable_template);
$update_compile_id = $compiler->template->caching && !$compiler->tag_nocache && !$compiler->nocache && $update_compile_id = $compiler->template->caching && !$compiler->tag_nocache && !$compiler->nocache &&
$_compile_id != '$_smarty_tpl->compile_id'; $_compile_id != '$_smarty_tpl->compile_id';
if ($has_compiled_template && !$call_nocache) { if ($has_compiled_template && !$call_nocache) {
@@ -306,4 +313,26 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
$_output .= "?>\n"; $_output .= "?>\n";
return $_output; return $_output;
} }
/**
* log include count
*
* @param \Smarty_Internal_SmartyTemplateCompiler $compiler
* @param string $include_file
* @param bool $variable_template
*/
private function logInclude(Smarty_Internal_SmartyTemplateCompiler $compiler, $include_file, $variable_template)
{
if ($variable_template) {
return;
}
list($name, $type) = Smarty_Resource::parseResourceName(trim($include_file, '\'"'), $compiler->template->smarty->default_resource_type);
if (in_array($type, array('eval', 'string'))) {
return;
}
$include_name = $type . ':' . $name;
$compiled = $compiler->parent_compiler->template->compiled;
$compiled->includes[$include_name] = isset($compiled->includes[$include_name]) ? $compiled->includes[$include_name] +
1 : 1;
}
} }

View File

@@ -34,6 +34,7 @@ class Smarty_Internal_Extension_CodeFrame
} }
if (!$cache) { if (!$cache) {
$properties['file_dependency'] = $_template->compiled->file_dependency; $properties['file_dependency'] = $_template->compiled->file_dependency;
$properties['includes'] = $_template->compiled->includes;
} else { } else {
$properties['file_dependency'] = $_template->cached->file_dependency; $properties['file_dependency'] = $_template->cached->file_dependency;
$properties['cache_lifetime'] = $_template->cache_lifetime; $properties['cache_lifetime'] = $_template->cache_lifetime;

View File

@@ -660,6 +660,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
} else { } else {
$this->mustCompile = !$is_valid; $this->mustCompile = !$is_valid;
$resource = $this->compiled; $resource = $this->compiled;
$resource->includes = $properties['includes'];
} }
if ($is_valid) { if ($is_valid) {
$resource->unifunc = $properties['unifunc']; $resource->unifunc = $properties['unifunc'];

View File

@@ -19,6 +19,7 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
*/ */
public $nocache_hash = null; public $nocache_hash = null;
/** /**
* create Compiled Object container * create Compiled Object container
*/ */
@@ -156,6 +157,18 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$_template->smarty->compile_check = $compileCheck; $_template->smarty->compile_check = $compileCheck;
} }
} }
if (!$_template->source->isConfig && !isset($_template->smarty->template_objects[$_template->templateId]) &&
$_template->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC &&
$_template->parent instanceof Smarty_Internal_Template && isset($_template->parent->compiled)
) {
foreach ($_template->parent->compiled->includes as $key => $count) {
$_template->compiled->includes[$key] = isset($_template->compiled->includes[$key]) ? $_template->compiled->includes[$key] +
$count : $count;
}
if (!in_array($_template->source->type, array('eval', 'string')) && $_template->compiled->includes[$_template->source->type . ':' . $_template->source->name] > 1) {
$_template->smarty->template_objects[$_template->templateId] = $_template;
}
}
$this->processed = true; $this->processed = true;
} }

View File

@@ -87,6 +87,13 @@ abstract class Smarty_Template_Resource_Base
*/ */
public $tpl_function = array(); public $tpl_function = array();
/**
* Included subtemplates
*
* @var array
*/
public $includes = array();
/** /**
* Process resource * Process resource
* *