- optimization of sub-template processing

This commit is contained in:
uwetews
2016-02-14 09:17:55 +01:00
parent 0f7f0d87b6
commit 4b1ba73eea
4 changed files with 57 additions and 53 deletions

View File

@@ -1,6 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)  ===== 3.1.30-dev ===== (xx.xx.xx)
14.02.2016 14.02.2016
- new tag {make_nocache} read NEW_FEATURES.txt https://github.com/smarty-php/smarty/issues/110 - new tag {make_nocache} read NEW_FEATURES.txt https://github.com/smarty-php/smarty/issues/110
- optimization of sub-template processing
11.02.2016 11.02.2016
- improvement added KnockoutJS comments to trimwhitespace outputfilter https://github.com/smarty-php/smarty/issues/82 - improvement added KnockoutJS comments to trimwhitespace outputfilter https://github.com/smarty-php/smarty/issues/82

View File

@@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.30-dev/39'; const SMARTY_VERSION = '3.1.30-dev/40';
/** /**
* define variable scopes * define variable scopes
@@ -1108,6 +1108,7 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public function _getTemplateId($template_name, $cache_id = null, $compile_id = null, $caching = null) public function _getTemplateId($template_name, $cache_id = null, $compile_id = null, $caching = null)
{ {
$template_name = (strpos($template_name, ':') === false) ? "{$this->default_resource_type}:{$template_name}" : $template_name;
$cache_id = $cache_id === null ? $this->cache_id : $cache_id; $cache_id = $cache_id === null ? $this->cache_id : $cache_id;
$compile_id = $compile_id === null ? $this->compile_id : $compile_id; $compile_id = $compile_id === null ? $this->compile_id : $compile_id;
$caching = (int) ($caching === null ? $this->caching : $caching); $caching = (int) ($caching === null ? $this->caching : $caching);

View File

@@ -96,7 +96,15 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
$compiled->includes[ $fullResourceName ] ++; $compiled->includes[ $fullResourceName ] ++;
$cache_tpl = true; $cache_tpl = true;
} else { } else {
$compiled->includes[ $fullResourceName ] = 1; if ("{$compiler->template->source->type}:{$compiler->template->source->name}" ==
$fullResourceName
) {
// recursive call of current template
$compiled->includes[ $fullResourceName ] = 2;
$cache_tpl = true;
} else {
$compiled->includes[ $fullResourceName ] = 1;
}
} }
$fullResourceName = '"' . $fullResourceName . '"'; $fullResourceName = '"' . $fullResourceName . '"';
} }

View File

@@ -239,77 +239,71 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
public function _subTemplateRender($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $scope, public function _subTemplateRender($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $scope,
$forceTplCache, $uid = null, $content_func = null) $forceTplCache, $uid = null, $content_func = null)
{ {
$tpl = clone $this;
$tpl->parent = $this;
$_templateId = $this->smarty->_getTemplateId($template, $cache_id, $compile_id, $caching); $_templateId = $this->smarty->_getTemplateId($template, $cache_id, $compile_id, $caching);
// already in template cache? // recursive call ?
/* @var Smarty_Internal_Template $tpl */ if ($tpl->_getTemplateId() != $_templateId) {
if (isset($this->smarty->_cache[ 'tplObjects' ][ $_templateId ])) { // already in template cache?
// clone cached template object because of possible recursive call if (isset($this->smarty->_cache[ 'tplObjects' ][ $_templateId ])) {
$tpl = clone $this->smarty->_cache[ 'tplObjects' ][ $_templateId ]; // copy data from cached object
// get variables from calling scope $cachedTpl = &$this->smarty->_cache[ 'tplObjects' ][ $_templateId ];
$tpl->tpl_vars = $this->tpl_vars; $tpl->templateId = $cachedTpl->templateId;
$tpl->config_vars = $this->config_vars; $tpl->template_resource = $cachedTpl->template_resource;
$tpl->parent = $this; $tpl->cache_id = $cachedTpl->cache_id;
// get template functions $tpl->compile_id = $cachedTpl->compile_id;
$tpl->tpl_function = $this->tpl_function; $tpl->source = $cachedTpl->source;
// copy inheritance object? // if $caching mode changed the compiled resource is invalid
if (isset($this->ext->_inheritance)) { if ((bool) $tpl->caching !== (bool) $caching) {
$tpl->ext->_inheritance = $this->ext->_inheritance; unset($tpl->compiled);
} elseif (isset($cachedTpl->compiled)) {
$tpl->compiled = $cachedTpl->compiled;
} else {
unset($tpl->compiled);
}
if ($caching != 9999 && isset($cachedTpl->cached)) {
$tpl->cached = $cachedTpl->cached;
} else {
unset($tpl->cached);
}
} else { } else {
unset($tpl->ext->_inheritance);
}
// if $caching mode changed the compiled resource is invalid
if ((bool) $tpl->caching !== (bool) $caching) {
unset($tpl->compiled);
}
} else {
$tpl = clone $this;
$tpl->parent = $this;
if (!isset($tpl->templateId) || $tpl->templateId !== $_templateId) {
$tpl->templateId = $_templateId; $tpl->templateId = $_templateId;
$tpl->template_resource = $template; $tpl->template_resource = $template;
$tpl->cache_id = $cache_id; $tpl->cache_id = $cache_id;
$tpl->compile_id = $compile_id; $tpl->compile_id = $compile_id;
if (isset($uid)) { if (isset($uid)) {
// for inline templates we can get all resource information from file dependency // for inline templates we can get all resource information from file dependency
if (isset($tpl->compiled->file_dependency[ $uid ])) { list($filepath, $timestamp, $type) = $tpl->compiled->file_dependency[ $uid ];
list($filepath, $timestamp, $type) = $tpl->compiled->file_dependency[ $uid ]; $tpl->source =
$tpl->source = new Smarty_Template_Source(isset($tpl->smarty->_cache[ 'resource_handlers' ][ $type ]) ?
new Smarty_Template_Source(isset($tpl->smarty->_cache[ 'resource_handlers' ][ $type ]) ? $tpl->smarty->_cache[ 'resource_handlers' ][ $type ] :
$tpl->smarty->_cache[ 'resource_handlers' ][ $type ] : Smarty_Resource::load($tpl->smarty, $type), $tpl->smarty,
Smarty_Resource::load($tpl->smarty, $type), $tpl->smarty, $filepath, $type, $filepath);
$filepath, $type, $filepath); $tpl->source->filepath = $filepath;
$tpl->source->filepath = $filepath; $tpl->source->timestamp = $timestamp;
$tpl->source->timestamp = $timestamp; $tpl->source->exists = true;
$tpl->source->exists = true; $tpl->source->uid = $uid;
$tpl->source->uid = $uid;
} else {
$tpl->source = null;
}
} else { } else {
$tpl->source = null;
}
if (!isset($tpl->source)) {
$tpl->source = Smarty_Template_Source::load($tpl); $tpl->source = Smarty_Template_Source::load($tpl);
unset($tpl->compiled); unset($tpl->compiled);
} }
unset($tpl->cached); if ($caching != 9999) {
unset($tpl->cached);
}
} }
} else {
// on recursive calls force caching
$forceTplCache = true;
} }
$tpl->caching = $caching; $tpl->caching = $caching;
$tpl->cache_lifetime = $cache_lifetime; $tpl->cache_lifetime = $cache_lifetime;
if ($caching == 9999) {
$tpl->cached = $this->cached;
}
// set template scope // set template scope
$tpl->scope = $scope; $tpl->scope = $scope;
if (!isset($tpl->smarty->_cache[ 'tplObjects' ][ $tpl->templateId ]) && !$tpl->source->handler->recompiled) { if (!isset($tpl->smarty->_cache[ 'tplObjects' ][ $tpl->templateId ]) && !$tpl->source->handler->recompiled) {
// if template is called multiple times set flag to to cache template objects
$forceTplCache = $forceTplCache ||
(isset($tpl->smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ]) &&
$tpl->smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ] > 1);
// check if template object should be cached // check if template object should be cached
if ($tpl->_isParentTemplate() && isset($tpl->smarty->_cache[ 'tplObjects' ][ $tpl->parent->templateId ]) || if ($forceTplCache || (isset($tpl->smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ]) &&
$forceTplCache $tpl->smarty->_cache[ 'subTplInfo' ][ $tpl->template_resource ] > 1) ||
($tpl->_isParentTemplate() && isset($tpl->smarty->_cache[ 'tplObjects' ][ $tpl->parent->templateId ]))
) { ) {
$tpl->smarty->_cache[ 'tplObjects' ][ $tpl->templateId ] = $tpl; $tpl->smarty->_cache[ 'tplObjects' ][ $tpl->templateId ] = $tpl;
} }