From f5e7f2481916d32d5e51b4f01a2deced78b81549 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 4 May 2013 20:09:58 +0200 Subject: [PATCH 1/3] [HttpFoundation] Optimize ServerBag::getHeaders() isset() vs in_array makes it take half the time (1ms/req here) substr() does not have to scan the whole string so it's a wee bit faster --- src/Symfony/Component/HttpFoundation/ServerBag.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/ServerBag.php b/src/Symfony/Component/HttpFoundation/ServerBag.php index fcb41cc5ee..ed807f2eb6 100644 --- a/src/Symfony/Component/HttpFoundation/ServerBag.php +++ b/src/Symfony/Component/HttpFoundation/ServerBag.php @@ -28,12 +28,13 @@ class ServerBag extends ParameterBag public function getHeaders() { $headers = array(); + $contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true); foreach ($this->parameters as $key => $value) { - if (0 === strpos($key, 'HTTP_')) { + if ('HTTP_' === substr($key, 0, 5)) { $headers[substr($key, 5)] = $value; } // CONTENT_* are not prefixed with HTTP_ - elseif (in_array($key, array('CONTENT_LENGTH', 'CONTENT_MD5', 'CONTENT_TYPE'))) { + elseif (isset($contentHeaders[$key])) { $headers[$key] = $value; } } From 997d5498467698a57ff7af8fab2dd5a7737bc60e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 4 May 2013 20:49:25 +0200 Subject: [PATCH 2/3] [HttpFoundation] Avoid a few unnecessary str_replace() calls --- .../Component/HttpFoundation/RequestMatcher.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/RequestMatcher.php b/src/Symfony/Component/HttpFoundation/RequestMatcher.php index b4bc880d4a..769ca66ecd 100644 --- a/src/Symfony/Component/HttpFoundation/RequestMatcher.php +++ b/src/Symfony/Component/HttpFoundation/RequestMatcher.php @@ -136,20 +136,16 @@ class RequestMatcher implements RequestMatcherInterface } foreach ($this->attributes as $key => $pattern) { - if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) { + if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) { return false; } } - if (null !== $this->path) { - $path = str_replace('#', '\\#', $this->path); - - if (!preg_match('#'.$path.'#', rawurldecode($request->getPathInfo()))) { - return false; - } + if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) { + return false; } - if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#i', $request->getHost())) { + if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) { return false; } From ea633f57870cef9df2849c0c23a42fe49f0357c9 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 4 May 2013 20:51:13 +0200 Subject: [PATCH 3/3] [HttpKernel] Avoid updating the context if the request did not change Due to the BC $this->setRequest() call in the onKernelRequest method, the request is set twice every time in Symfony, and RequestContext::fromRequest takes 1ms to run here so if we can skip one call it is a win. --- .../Component/HttpKernel/EventListener/RouterListener.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php index 58ea6f0a54..f68716c144 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php @@ -35,6 +35,7 @@ class RouterListener implements EventSubscriberInterface private $matcher; private $context; private $logger; + private $request; /** * Constructor. @@ -72,9 +73,10 @@ class RouterListener implements EventSubscriberInterface */ public function setRequest(Request $request = null) { - if (null !== $request) { + if (null !== $request && $this->request !== $request) { $this->context->fromRequest($request); } + $this->request = $request; } public function onKernelRequest(GetResponseEvent $event)