optimized twig_get_attribute (avoid calling getSourceContext())

This commit is contained in:
Fabien Potencier
2018-01-30 09:15:26 +01:00
parent d4482c9ab9
commit 89cbb5c4c9
7 changed files with 31 additions and 18 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ class Twig_Node_CheckSecurity extends Twig_Node
->outdent() ->outdent()
->write("} catch (Twig_Sandbox_SecurityError \$e) {\n") ->write("} catch (Twig_Sandbox_SecurityError \$e) {\n")
->indent() ->indent()
->write("\$e->setSourceContext(\$this->getSourceContext());\n\n") ->write("\$e->setSourceContext(\$this->source);\n\n")
->write("if (\$e instanceof Twig_Sandbox_SecurityNotAllowedTagError && isset(\$tags[\$e->getTagName()])) {\n") ->write("if (\$e instanceof Twig_Sandbox_SecurityNotAllowedTagError && isset(\$tags[\$e->getTagName()])) {\n")
->indent() ->indent()
->write("\$e->setTemplateLine(\$tags[\$e->getTagName()]);\n") ->write("\$e->setTemplateLine(\$tags[\$e->getTagName()]);\n")
+1 -1
View File
@@ -23,7 +23,7 @@ class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
public function compile(Twig_Compiler $compiler) public function compile(Twig_Compiler $compiler)
{ {
$compiler->raw('twig_get_attribute($this->env, $this->getSourceContext(), '); $compiler->raw('twig_get_attribute($this->env, $this->source, ');
if ($this->getAttribute('ignore_strict_check')) { if ($this->getAttribute('ignore_strict_check')) {
$this->getNode('node')->setAttribute('ignore_strict_check', true); $this->getNode('node')->setAttribute('ignore_strict_check', true);
+1 -1
View File
@@ -61,7 +61,7 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression
->string($name) ->string($name)
->raw(' does not exist.\', ') ->raw(' does not exist.\', ')
->repr($this->lineno) ->repr($this->lineno)
->raw(', $this->getSourceContext()); })()') ->raw(', $this->source); })()')
->raw(')') ->raw(')')
; ;
} }
+3 -10
View File
@@ -80,16 +80,7 @@ class Twig_Node_Module extends Twig_Node
$this->compileClassHeader($compiler); $this->compileClassHeader($compiler);
if ( $this->compileConstructor($compiler);
count($this->getNode('blocks'))
|| count($this->getNode('traits'))
|| !$this->hasNode('parent')
|| $this->getNode('parent') instanceof Twig_Node_Expression_Constant
|| count($this->getNode('constructor_start'))
|| count($this->getNode('constructor_end'))
) {
$this->compileConstructor($compiler);
}
$this->compileGetParent($compiler); $this->compileGetParent($compiler);
@@ -155,6 +146,7 @@ class Twig_Node_Module extends Twig_Node
->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass())) ->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))
->write("{\n") ->write("{\n")
->indent() ->indent()
->write("private \$source;\n\n")
; ;
} }
@@ -165,6 +157,7 @@ class Twig_Node_Module extends Twig_Node
->indent() ->indent()
->subcompile($this->getNode('constructor_start')) ->subcompile($this->getNode('constructor_start'))
->write("parent::__construct(\$env);\n\n") ->write("parent::__construct(\$env);\n\n")
->write("\$this->source = \$this->getSourceContext();\n\n")
; ;
// parent // parent
+1 -1
View File
@@ -54,7 +54,7 @@ abstract class Twig_Test_NodeTestCase extends TestCase
protected function getAttributeGetter() protected function getAttributeGetter()
{ {
return 'twig_get_attribute($this->env, $this->getSourceContext(), '; return 'twig_get_attribute($this->env, $this->source, ';
} }
} }
+1 -1
View File
@@ -27,7 +27,7 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Test_NodeTestCase
$env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => true)); $env = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => true));
$env1 = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => false)); $env1 = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock(), array('strict_variables' => false));
$output = '(isset($context["foo"]) || array_key_exists("foo", $context) ? $context["foo"] : (function () { throw new Twig_Error_Runtime(\'Variable "foo" does not exist.\', 1, $this->getSourceContext()); })())'; $output = '(isset($context["foo"]) || array_key_exists("foo", $context) ? $context["foo"] : (function () { throw new Twig_Error_Runtime(\'Variable "foo" does not exist.\', 1, $this->source); })())';
return array( return array(
array($node, "// line 1\n".$output, $env), array($node, "// line 1\n".$output, $env),
+23 -3
View File
@@ -48,10 +48,14 @@ class Twig_Tests_Node_ModuleTest extends Twig_Test_NodeTestCase
/* foo.twig */ /* foo.twig */
class __TwigTemplate_%x extends Twig_Template class __TwigTemplate_%x extends Twig_Template
{ {
private \$source;
public function __construct(Twig_Environment \$env) public function __construct(Twig_Environment \$env)
{ {
parent::__construct(\$env); parent::__construct(\$env);
\$this->source = \$this->getSourceContext();
\$this->parent = false; \$this->parent = false;
\$this->blocks = array( \$this->blocks = array(
@@ -71,7 +75,7 @@ class __TwigTemplate_%x extends Twig_Template
public function getDebugInfo() public function getDebugInfo()
{ {
return array ( 19 => 1,); return array ( 23 => 1,);
} }
public function getSourceContext() public function getSourceContext()
@@ -94,10 +98,14 @@ EOF
/* foo.twig */ /* foo.twig */
class __TwigTemplate_%x extends Twig_Template class __TwigTemplate_%x extends Twig_Template
{ {
private \$source;
public function __construct(Twig_Environment \$env) public function __construct(Twig_Environment \$env)
{ {
parent::__construct(\$env); parent::__construct(\$env);
\$this->source = \$this->getSourceContext();
// line 1 // line 1
\$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1); \$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1);
\$this->blocks = array( \$this->blocks = array(
@@ -129,7 +137,7 @@ class __TwigTemplate_%x extends Twig_Template
public function getDebugInfo() public function getDebugInfo()
{ {
return array ( 26 => 1, 24 => 2, 11 => 1,); return array ( 30 => 1, 28 => 2, 15 => 1,);
} }
public function getSourceContext() public function getSourceContext()
@@ -157,6 +165,18 @@ EOF
/* foo.twig */ /* foo.twig */
class __TwigTemplate_%x extends Twig_Template class __TwigTemplate_%x extends Twig_Template
{ {
private \$source;
public function __construct(Twig_Environment \$env)
{
parent::__construct(\$env);
\$this->source = \$this->getSourceContext();
\$this->blocks = array(
);
}
protected function doGetParent(array \$context) protected function doGetParent(array \$context)
{ {
// line 2 // line 2
@@ -183,7 +203,7 @@ class __TwigTemplate_%x extends Twig_Template
public function getDebugInfo() public function getDebugInfo()
{ {
return array ( 17 => 2, 15 => 4, 9 => 2,); return array ( 29 => 2, 27 => 4, 21 => 2,);
} }
public function getSourceContext() public function getSourceContext()