From 695423e14e212930099838102c23dc8ee8ac1558 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 10 Dec 2021 13:18:08 +0200 Subject: [PATCH 1/2] Optimize Environment::resolveTemplate() to be much faster when template overrides do not exist --- src/Environment.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index 894c862cb..3e35b4c52 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -501,6 +501,7 @@ class Environment $names = [$names]; } + $count = \count($names); foreach ($names as $name) { if ($name instanceof Template) { return $name; @@ -509,13 +510,13 @@ class Environment return $name; } - try { - return $this->loadTemplate($name); - } catch (LoaderError $e) { - if (1 === \count($names)) { - throw $e; - } + // Optimization: Avoid throwing an exception when it would be ignored anyway. + if (1 !== $count && !$this->getLoader()->exists($name)) { + continue; } + + // Throws LoaderError: Unable to find template "%s". + return $this->loadTemplate($name); } throw new LoaderError(sprintf('Unable to find one of the following templates: "%s".', implode('", "', $names))); From 3284859a1b160833e8afc2a40d04c33b94191e41 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 17 Dec 2021 08:44:00 -0800 Subject: [PATCH 2/2] Remove unneeded coments --- src/Environment.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index a0cb8c9e4..94e6d225e 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -510,12 +510,10 @@ class Environment return $name; } - // Optimization: Avoid throwing an exception when it would be ignored anyway. if (1 !== $count && !$this->getLoader()->exists($name)) { continue; } - // Throws LoaderError: Unable to find template "%s". return $this->loadTemplate($name); }