merged branch char101/optimize-strict-vars-php54 (PR #893)

This PR was squashed before being merged into the master branch (closes #893).

Commits
-------

f397a8f Use ternary operator with PHP 5.4 when strict_variables is true

Discussion
----------

Use ternary operator with PHP 5.4 when strict_variables is true

When strict variables is true, and using PHP 5.4, we can still use the ternary operator to optimize performance.
This commit is contained in:
Fabien Potencier
2012-11-08 07:21:48 +01:00
2 changed files with 8 additions and 5 deletions
+7 -4
View File
@@ -44,15 +44,18 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
// remove the non-PHP 5.4 version when PHP 5.3 support is dropped
// as the non-optimized version is just a workaround for slow ternary operator
// when the context has a lot of variables
if (version_compare(phpversion(), '5.4.0RC1', '>=') && ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables())) {
if (version_compare(phpversion(), '5.4.0RC1', '>=')) {
// PHP 5.4 ternary operator performance was optimized
$compiler
->raw('(isset($context[')
->string($name)
->raw(']) ? $context[')
->string($name)
->raw('] : null)')
;
->string($name);
if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
$compiler->raw('] : null)');
} else {
$compiler->raw('] : $this->getContext($context, ')->string($name)->raw('))');
}
} else {
$compiler
->raw('$this->getContext($context, ')
+1 -1
View File
@@ -40,7 +40,7 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase
$env1 = new Twig_Environment(null, array('strict_variables' => false));
return array(
array($node, '$this->getContext($context, "foo")', $env),
version_compare(PHP_VERSION, '5.4.0') >= 0 ? array($node, '(isset($context["foo"]) ? $context["foo"] : $this->getContext($context, "foo"))', $env) : array($node, '$this->getContext($context, "foo")', $env),
array($node, $this->getVariableGetter('foo'), $env1),
array($self, '$this'),
array($context, '$context'),