From 09222174582b02cc842feb7bdb68f325c8abd7a7 Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Sat, 14 Nov 2026 22:05:12 +0100 Subject: [PATCH] Speed up creating filter, function, and test nodes Each `FilterExpression` created 8 identical `NameDeprecation('twig/twig', '3.12')` objects in its constructor, `FunctionExpression` created 7, and `TestExpression` created 4. The object is immutable, so one instance per class is now shared. This only affects compile time. `new FilterExpression` goes from 2136-2175 -> 1517-1525 ns. Compiling a 120-line template with many filters goes from 46.51-47.66 -> 44.40-45.97 ms, and the generated code is identical. --- CHANGELOG | 1 + src/Node/Expression/FilterExpression.php | 21 ++++++++++++--------- src/Node/Expression/FunctionExpression.php | 17 ++++++++++------- src/Node/Expression/TestExpression.php | 11 +++++++---- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index a3653427c97..f72ae2e302a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -21,6 +21,7 @@ * Add a way to mark a `Twig\Markup` instance as safe for some escaping strategies only * Make `Environment::getExtension()` return the runtime of an extension class registered with `AttributeExtension` * Make `Environment::getRuntime()` fall back to the registered extension of the same class + * Speed up creating filter, function, and test nodes # 3.30.0 (2026-09-25) diff --git a/src/Node/Expression/FilterExpression.php b/src/Node/Expression/FilterExpression.php index eb947201d17..cbf1460a748 100644 --- a/src/Node/Expression/FilterExpression.php +++ b/src/Node/Expression/FilterExpression.php @@ -46,15 +46,18 @@ public function __construct(Node $node, TwigFilter|ConstantExpression $filter, N $this->setAttribute('twig_callable', $filter); } - $this->deprecateNode('filter', new NameDeprecation('twig/twig', '3.12')); - - $this->deprecateAttribute('needs_charset', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('needs_environment', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('needs_context', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('arguments', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('callable', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('is_variadic', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('dynamic_name', new NameDeprecation('twig/twig', '3.12')); + static $deprecation; + $deprecation ??= new NameDeprecation('twig/twig', '3.12'); + + $this->deprecateNode('filter', $deprecation); + + $this->deprecateAttribute('needs_charset', $deprecation); + $this->deprecateAttribute('needs_environment', $deprecation); + $this->deprecateAttribute('needs_context', $deprecation); + $this->deprecateAttribute('arguments', $deprecation); + $this->deprecateAttribute('callable', $deprecation); + $this->deprecateAttribute('is_variadic', $deprecation); + $this->deprecateAttribute('dynamic_name', $deprecation); } public function compile(Compiler $compiler): void diff --git a/src/Node/Expression/FunctionExpression.php b/src/Node/Expression/FunctionExpression.php index 5983962dc56..3d42381721a 100644 --- a/src/Node/Expression/FunctionExpression.php +++ b/src/Node/Expression/FunctionExpression.php @@ -39,13 +39,16 @@ public function __construct(TwigFunction|string $function, Node $arguments, int $this->setAttribute('twig_callable', $function); } - $this->deprecateAttribute('needs_charset', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('needs_environment', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('needs_context', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('arguments', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('callable', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('is_variadic', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('dynamic_name', new NameDeprecation('twig/twig', '3.12')); + static $deprecation; + $deprecation ??= new NameDeprecation('twig/twig', '3.12'); + + $this->deprecateAttribute('needs_charset', $deprecation); + $this->deprecateAttribute('needs_environment', $deprecation); + $this->deprecateAttribute('needs_context', $deprecation); + $this->deprecateAttribute('arguments', $deprecation); + $this->deprecateAttribute('callable', $deprecation); + $this->deprecateAttribute('is_variadic', $deprecation); + $this->deprecateAttribute('dynamic_name', $deprecation); } public function enableDefinedTest(): void diff --git a/src/Node/Expression/TestExpression.php b/src/Node/Expression/TestExpression.php index 4d81239414e..d92c5eb4957 100644 --- a/src/Node/Expression/TestExpression.php +++ b/src/Node/Expression/TestExpression.php @@ -48,10 +48,13 @@ public function __construct(Node $node, string|TwigTest $test, ?Node $arguments, $this->setAttribute('twig_callable', $test); } - $this->deprecateAttribute('arguments', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('callable', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('is_variadic', new NameDeprecation('twig/twig', '3.12')); - $this->deprecateAttribute('dynamic_name', new NameDeprecation('twig/twig', '3.12')); + static $deprecation; + $deprecation ??= new NameDeprecation('twig/twig', '3.12'); + + $this->deprecateAttribute('arguments', $deprecation); + $this->deprecateAttribute('callable', $deprecation); + $this->deprecateAttribute('is_variadic', $deprecation); + $this->deprecateAttribute('dynamic_name', $deprecation); } public function compile(Compiler $compiler): void