Skip to content

Commit

Permalink
minor #35855 [Routing] Improve localized routes performances (mtarld)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.4 branch.

Discussion
----------

[Routing] Improve localized routes performances

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| License       | MIT

Implementation of the following idea: #35735 (review)

Improve route matching performances by turning dynamic routes with fixed `_locale` to actual static routes.

Commits
-------

8e9eafe [Routing] Improve localized routes performances
  • Loading branch information
nicolas-grekas committed Feb 25, 2020
2 parents 88b96ab + 8e9eafe commit ef95f2e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Component/Routing/RouteCompiler.php
Expand Up @@ -61,6 +61,14 @@ public static function compile(Route $route)
$hostRegex = $result['regex'];
}

$locale = $route->getDefault('_locale');
if (null !== $locale && null !== $route->getDefault('_canonical_route') && preg_quote($locale, self::REGEX_DELIMITER) === $route->getRequirement('_locale')) {
$requirements = $route->getRequirements();
unset($requirements['_locale']);
$route->setRequirements($requirements);
$route->setPath(str_replace('{_locale}', $locale, $route->getPath()));
}

$path = $route->getPath();

$result = self::compilePattern($route, $path, false);
Expand Down
@@ -0,0 +1,19 @@
<?php

/**
* This file has been auto-generated
* by the Symfony Routing Component.
*/

return [
false, // $matchHost
[ // $staticRoutes
'/fr/accueil' => [[['_route' => 'home', '_locale' => 'fr'], null, null, null, false, false, null]],
'/en/home' => [[['_route' => 'home', '_locale' => 'en'], null, null, null, false, false, null]],
],
[ // $regexpList
],
[ // $dynamicRoutes
],
null, // $checkCondition
];
Expand Up @@ -12,6 +12,9 @@
namespace Symfony\Component\Routing\Tests\Matcher\Dumper;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\Matcher\CompiledUrlMatcher;
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper;
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;
Expand Down Expand Up @@ -442,21 +445,34 @@ public function getRouteCollections()
$hostCollection->add('r1', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));
$hostCollection->add('r2', (new Route('abc{foo}'))->setHost('{foo}.exampple.com'));

/* test case 14 */
$fixedLocaleCollection = new RouteCollection();
$routes = new RoutingConfigurator($fixedLocaleCollection, new PhpFileLoader(new FileLocator()), __FILE__, __FILE__);
$routes
->collection()
->prefix('/{_locale}')
->add('home', [
'fr' => 'accueil',
'en' => 'home',
])
;

return [
[new RouteCollection(), 'compiled_url_matcher0.php'],
[$collection, 'compiled_url_matcher1.php'],
[$redirectCollection, 'compiled_url_matcher2.php'],
[$rootprefixCollection, 'compiled_url_matcher3.php'],
[$headMatchCasesCollection, 'compiled_url_matcher4.php'],
[$groupOptimisedCollection, 'compiled_url_matcher5.php'],
[$trailingSlashCollection, 'compiled_url_matcher6.php'],
[$trailingSlashCollection, 'compiled_url_matcher7.php'],
[$unicodeCollection, 'compiled_url_matcher8.php'],
[$hostTreeCollection, 'compiled_url_matcher9.php'],
[$chunkedCollection, 'compiled_url_matcher10.php'],
[$demoCollection, 'compiled_url_matcher11.php'],
[$suffixCollection, 'compiled_url_matcher12.php'],
[$hostCollection, 'compiled_url_matcher13.php'],
[new RouteCollection(), 'compiled_url_matcher0.php'],
[$collection, 'compiled_url_matcher1.php'],
[$redirectCollection, 'compiled_url_matcher2.php'],
[$rootprefixCollection, 'compiled_url_matcher3.php'],
[$headMatchCasesCollection, 'compiled_url_matcher4.php'],
[$groupOptimisedCollection, 'compiled_url_matcher5.php'],
[$trailingSlashCollection, 'compiled_url_matcher6.php'],
[$trailingSlashCollection, 'compiled_url_matcher7.php'],
[$unicodeCollection, 'compiled_url_matcher8.php'],
[$hostTreeCollection, 'compiled_url_matcher9.php'],
[$chunkedCollection, 'compiled_url_matcher10.php'],
[$demoCollection, 'compiled_url_matcher11.php'],
[$suffixCollection, 'compiled_url_matcher12.php'],
[$hostCollection, 'compiled_url_matcher13.php'],
[$fixedLocaleCollection, 'compiled_url_matcher14.php'],
];
}

Expand Down

0 comments on commit ef95f2e

Please sign in to comment.