Added a cache for extension freshness test

Freshness of each extension was been checked everytime a template was loaded.
Projects with many templates and extension benefit noticeably with this cache.
Only environments with debug or autoload set are affected.
This commit is contained in:
David Stone
2015-10-06 20:10:54 -06:00
committed by Fabien Potencier
parent e624d0849e
commit 77c94ea205
+10 -5
View File
@@ -48,6 +48,7 @@ class Twig_Environment
private $originalCache; private $originalCache;
private $bcWriteCacheFile = false; private $bcWriteCacheFile = false;
private $bcGetCacheFilename = false; private $bcGetCacheFilename = false;
private $lastModifiedExtension = 0;
/** /**
* Constructor. * Constructor.
@@ -454,14 +455,16 @@ class Twig_Environment
*/ */
public function isTemplateFresh($name, $time) public function isTemplateFresh($name, $time)
{ {
foreach ($this->extensions as $extension) { if(0 === $this->lastModifiedExtension) {
$r = new ReflectionObject($extension); foreach ($this->extensions as $extension) {
if (filemtime($r->getFileName()) > $time) { $r = new ReflectionObject($extension);
return false; if (($extensionTime = filemtime($r->getFileName())) > $this->lastModifiedExtension) {
$this->lastModifiedExtension = $extensionTime;
}
} }
} }
return $this->getLoader()->isFresh($name, $time); return $this->lastModifiedExtension <= $time && $this->getLoader()->isFresh($name, $time);
} }
/** /**
@@ -767,6 +770,8 @@ class Twig_Environment
throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $extension->getName())); throw new LogicException(sprintf('Unable to register extension "%s" as extensions have already been initialized.', $extension->getName()));
} }
$this->lastModifiedExtension = 0;
$this->extensions[$extension->getName()] = $extension; $this->extensions[$extension->getName()] = $extension;
} }