8000 Optimized dumped router matcher, prevent unneeded function calls. · symfony/symfony@ea8c55a · GitHub
[go: up one dir, main page]

Skip to content

Commit ea8c55a

Browse files
committed
Optimized dumped router matcher, prevent unneeded function calls.
1 parent e58be70 commit ea8c55a

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ public function match(\$pathinfo)
105105
{
106106
\$allow = array();
107107
\$pathinfo = rawurldecode(\$pathinfo);
108+
\$trimmedPathinfo = rtrim(\$pathinfo, '/');
108109
\$context = \$this->context;
109110
\$request = \$this->request;
111+
\$requestMethod = \$context->getMethod();
110112
111113
$code
112114
@@ -133,7 +135,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections)
133135
foreach ($groups as $collection) {
134136
if (null !== $regex = $collection->getAttribute('host_regex')) {
135137
if (!$fetchedHost) {
136-
$code .= " \$host = \$this->context->getHost();\n\n";
138+
$code .= " \$host = \$context->getHost();\n\n";
137139
$fetchedHost = true;
138140
}
139141

@@ -227,7 +229,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
227229

228230
if (!count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#'.(substr($regex, -1) === 'u' ? 'u' : ''), $regex, $m)) {
229231
if ($supportsTrailingSlash && substr($m['url'], -1) === '/') {
230-
$conditions[] = sprintf("rtrim(\$pathinfo, '/') === %s", var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true));
232+
$conditions[] = sprintf("\$trimmedPathinfo === %s", var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true));
231233
$hasTrailingSlash = true;
232234
} else {
233235
$conditions[] = sprintf('$pathinfo === %s', var_export(str_replace('\\', '', $m['url']), true));
@@ -266,7 +268,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
266268
if ($methods) {
267269
if (1 === count($methods)) {
268270
$code .= <<<EOF
269-
if (\$this->context->getMethod() != '$methods[0]') {
271+
if (\$requestMethod != '$methods[0]') {
270272
\$allow[] = '$methods[0]';
271273
goto $gotoname;
272274
}
@@ -276,7 +278,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
276278
} else {
277279
$methods = implode("', '", $methods);
278280
$code .= <<<EOF
279-
if (!in_array(\$this->context->getMethod(), array('$methods'))) {
281+
if (!in_array(\$requestMethod, array('$methods'))) {
280282
\$allow = array_merge(\$allow, array('$methods'));
281283
goto $gotoname;
282284
}
@@ -303,7 +305,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren
303305
$schemes = str_replace("\n", '', var_export(array_flip($schemes), true));
304306
$code .= <<<EOF
305307
\$requiredSchemes = $schemes;
306-
if (!isset(\$requiredSchemes[\$this->context->getScheme()])) {
308+
if (!isset(\$requiredSchemes[\$context->getScheme()])) {
307309
return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes));
308310
}
309311

src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ public function match($pathinfo)
2424
{
2525
$allow = array();
2626
$pathinfo = rawurldecode($pathinfo);
27+
$trimmedPathinfo = rtrim($pathinfo, '/');
2728
$context = $this->context;
2829
$request = $this->request;
30+
$requestMethod = $context->getMethod();
2931

3032
// foo
3133
if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?P<bar>baz|symfony)$#s', $pathinfo, $matches)) {
@@ -35,7 +37,7 @@ public function match($pathinfo)
3537
if (0 === strpos($pathinfo, '/bar')) {
3638
// bar
3739
if (preg_match('#^/bar/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
38-
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
40+
if (!in_array($requestMethod, array('GET', 'HEAD'))) {
3941
$allow = array_merge($allow, array('GET', 'HEAD'));
4042
goto not_bar;
4143
}
@@ -46,7 +48,7 @@ public function match($pathinfo)
4648

4749
// barhead
4850
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
49-
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
51+
if (!in_array($requestMethod, array('GET', 'HEAD'))) {
5052
$allow = array_merge($allow, array('GET', 'HEAD'));
5153
goto not_barhead;
5254
}
@@ -83,7 +85,7 @@ public function match($pathinfo)
8385

8486
// baz5
8587
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
86-
if ($this->context->getMethod() != 'POST') {
88+
if ($requestMethod != 'POST') {
8789
$allow[] = 'POST';
8890
goto not_baz5;
8991
}
@@ -94,7 +96,7 @@ public function match($pathinfo)
9496

9597
// baz.baz6
9698
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
97-
if ($this->context->getMethod() != 'PUT') {
99+
if ($requestMethod != 'PUT') {
98100
$allow[] = 'PUT';
99101
goto not_bazbaz6;
100102
}
@@ -195,7 +197,7 @@ public function match($pathinfo)
195197

196198
}
197199

198-
$host = $this->context->getHost();
200+
$host = $context->getHost();
199201

200202
if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) {
201203
// route1

src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ public function match($pathinfo)
2424
{
2525
$allow = array();
2626
$pathinfo = rawurldecode($pathinfo);
27+
$trimmedPathinfo = rtrim($pathinfo, '/');
2728
$context = $this->context;
2829
$request = $this->request;
30+
$requestMethod = $context->getMethod();
2931

3032
// foo
3133
if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?P<bar>baz|symfony)$#s', $pathinfo, $matches)) {
@@ -35,7 +37,7 @@ public function match($pathinfo)
3537
if (0 === strpos($pathinfo, '/bar')) {
3638
// bar
3739
if (preg_match('#^/bar/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
38-
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
40+
if (!in_array($requestMethod, array('GET', 'HEAD'))) {
3941
$allow = array_merge($allow, array('GET', 'HEAD'));
4042
goto not_bar;
4143
}
@@ -46,7 +48,7 @@ public function match($pathinfo)
4648

4749
// barhead
4850
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
49-
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
51+
if (!in_array($requestMethod, array('GET', 'HEAD'))) {
5052
$allow = array_merge($allow, array('GET', 'HEAD'));
5153
goto not_barhead;
5254
}
@@ -70,7 +72,7 @@ public function match($pathinfo)
7072
}
7173

7274
// baz3
73-
if (rtrim($pathinfo, '/') === '/test/baz3') {
75+
if ($trimmedPathinfo === '/test/baz3') {
7476
if (substr($pathinfo, -1) !== '/') {
7577
return $this->redirect($pathinfo.'/', 'baz3');
7678
}
@@ -91,7 +93,7 @@ public function match($pathinfo)
9193

9294
// baz5
9395
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
94-
if ($this->context->getMethod() != 'POST') {
96+
if ($requestMethod != 'POST') {
9597
$allow[] = 'POST';
9698
goto not_baz5;
9799
}
@@ -102,7 +104,7 @@ public function match($pathinfo)
102104

103105
// baz.baz6
104106
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
105-
if ($this->context->getMethod() != 'PUT') {
107+
if ($requestMethod != 'PUT') {
106108
$allow[] = 'PUT';
107109
goto not_bazbaz6;
108110
}
@@ -174,7 +176,7 @@ public function match($pathinfo)
174176
}
175177

176178
// hey
177-
if (rtrim($pathinfo, '/') === '/multi/hey') {
179+
if ($trimmedPathinfo === '/multi/hey') {
178180
if (substr($pathinfo, -1) !== '/') {
179181
return $this->redirect($pathinfo.'/', 'hey');
180182
}
@@ -207,7 +209,7 @@ public function match($pathinfo)
207209

208210
}
209211

210-
$host = $this->context->getHost();
212+
$host = $context->getHost();
211213

212214
if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) {
213215
// route1
@@ -322,7 +324,7 @@ public function match($pathinfo)
322324
// secure
323325
if ($pathinfo === '/secure') {
324326
$requiredSchemes = array ( 'https' => 0,);
325-
if (!isset($requiredSchemes[$this->context->getScheme()])) {
327+
if (!isset($requiredSchemes[$context->getScheme()])) {
326328
return $this->redirect($pathinfo, 'secure', key($requiredSchemes));
327329
}
328330

@@ -332,7 +334,7 @@ public function match($pathinfo)
332334
// nonsecure
333335
if ($pathinfo === '/nonsecure') {
334336
$requiredSchemes = array ( 'http' => 0,);
335-
if (!isset($requiredSchemes[$this->context->getScheme()])) {
337+
if (!isset($requiredSchemes[$context->getScheme()])) {
336338
return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes));
337339
}
338340

src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ public function match($pathinfo)
2424
{
2525
$allow = array();
2626
$pathinfo = rawurldecode($pathinfo);
27+
$trimmedPathinfo = rtrim($pathinfo, '/');
2728
$context = $this->context;
2829
$request = $this->request;
30+
$requestMethod = $context->getMethod();
2931

3032
if (0 === strpos($pathinfo, '/rootprefix')) {
3133
// static

0 commit comments

Comments
 (0)
0