merged branch bschussek/phpengine_cache_escape (PR #4942)

Commits
-------

0d0a968 [Templating] Cached the result of escape() in order to improve performance (+470ms)

Discussion
----------

[Templating] Cached the result of escape() in order to improve performance

Bug fix: no
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

This improvement gains **400ms** of rendering speed on [this particular example page](http://advancedform.gpserver.dk/app_dev.php/taxclasses/1).

---------------------------------------------------------------------------

by lsmith77 at 2012-07-16T17:36:50Z

i guess we don't have to be concerned with increased memory usage here .. if at all we could offer a clear cache method in case someone is f.e. using this to generate tons of messages in a cron job.

---------------------------------------------------------------------------

by henrikbjorn at 2012-07-17T06:39:52Z

The example form is broken.

---------------------------------------------------------------------------

by bschussek at 2012-07-17T07:21:26Z

The source code for the form can be found [here](https://github.com/stof/symfony-standard/blob/twig_forms/src/AdvancedForm/CoreBundle/Form/TaxClassType.php).

---------------------------------------------------------------------------

by henrikbjorn at 2012-07-17T07:28:11Z

But i am guessing this is only for php not twig :P

---------------------------------------------------------------------------

by bschussek at 2012-07-17T07:41:07Z

Obviously..
This commit is contained in:
Fabien Potencier 2012-07-17 22:45:12 +02:00
commit 34c2bf75e0
1 changed files with 14 additions and 0 deletions

View File

@ -38,6 +38,7 @@ class PhpEngine implements EngineInterface, \ArrayAccess
protected $charset;
protected $cache;
protected $escapers;
protected $escaperCache;
protected $globals;
protected $parser;
@ -334,6 +335,16 @@ class PhpEngine implements EngineInterface, \ArrayAccess
*/
public function escape($value, $context = 'html')
{
// If we deal with a scalar value, we can cache the result to increase
// the performance when the same value is escaped multiple times (e.g. loops)
if (is_scalar($value)) {
if (!isset($this->escaperCache[$context][$value])) {
$this->escaperCache[$context][$value] = call_user_func($this->getEscaper($context), $value);
}
return $this->escaperCache[$context][$value];
}
return call_user_func($this->getEscaper($context), $value);
}
@ -372,6 +383,7 @@ class PhpEngine implements EngineInterface, \ArrayAccess
public function setEscaper($context, $escaper)
{
$this->escapers[$context] = $escaper;
$this->escaperCache[$context] = array();
}
/**
@ -489,6 +501,8 @@ class PhpEngine implements EngineInterface, \ArrayAccess
return $value;
},
);
$this->escaperCache = array();
}
/**