Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd720ed

Browse files
bug #30058 [Routing] fix perf issue when dumping large number of routes (nicolas-grekas)
This PR was merged into the 4.2 branch. Discussion ---------- [Routing] fix perf issue when dumping large number of routes | Q | A | ------------- | --- | Branch? | 4.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29918 | License | MIT | Doc PR | - In my reproducer, dumping 12k routes goes from 40s to 3s without xdebug, and from 50s to 12s with xdebug. There is a lower level issue which is that `strpos` is called 16M times, but that's still a lot faster than calling `preg_match` 16M times. Reducing the number of checks is certainly possible, but that would be more involving. This could happen on master if someone is up to dig into it. Commits ------- 872efe5 [Routing] fix perf issue when dumping large number of routes
2 parents 0bb0c7f + 872efe5 commit fd720ed

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ private function groupStaticRoutes(RouteCollection $collection): array
159159

160160
foreach ($collection->all() as $name => $route) {
161161
$compiledRoute = $route->compile();
162+
$staticPrefix = rtrim($compiledRoute->getStaticPrefix(), '/');
162163
$hostRegex = $compiledRoute->getHostRegex();
163164
$regex = $compiledRoute->getRegex();
164165
if ($hasTrailingSlash = '/' !== $route->getPath()) {
@@ -173,17 +174,17 @@ private function groupStaticRoutes(RouteCollection $collection): array
173174
if ($hasTrailingSlash) {
174175
$url = substr($url, 0, -1);
175176
}
176-
foreach ($dynamicRegex as list($hostRx, $rx)) {
177-
if (preg_match($rx, $url) && (!$host || !$hostRx || preg_match($hostRx, $host))) {
178-
$dynamicRegex[] = [$hostRegex, $regex];
177+
foreach ($dynamicRegex as list($hostRx, $rx, $prefix)) {
178+
if (('' === $prefix || 0 === strpos($url, $prefix)) && preg_match($rx, $url) && (!$host || !$hostRx || preg_match($hostRx, $host))) {
179+
$dynamicRegex[] = [$hostRegex, $regex, $staticPrefix];
179180
$dynamicRoutes->add($name, $route);
180181
continue 2;
181182
}
182183
}
183184

184185
$staticRoutes[$url][$name] = [$route, $hasTrailingSlash];
185186
} else {
186-
$dynamicRegex[] = [$hostRegex, $regex];
187+
$dynamicRegex[] = [$hostRegex, $regex, $staticPrefix];
187188
$dynamicRoutes->add($name, $route);
188189
}
189190
}

0 commit comments

Comments
 (0)